2010-07-22 8 views
7

इसलिए मैं एक्सएमएल के बिना अपना वेब ऐप कॉन्फ़िगर करने और सभी एनोटेटेड रूट पर जाने की कोशिश कर रहा हूं। मेरे पास @ कॉन्फ़िगरेशन और @ कॉम्पोनेंट के साथ एनोटेटेड कुछ कक्षाएं हैं जो स्वचालित रूप से उठाई जा रही हैं, लेकिन किसी कारण से मेरे @ नियंत्रक एनोटेशन को पहचाना नहीं जा रहा है और उनके संबंधित @RequestMapping मानों में मैप किया गया है।स्प्रिंग 3 एनोटेटेड कॉन्फ़िगरेशन पिक अप @ कॉन्फ़िगरेशन और @ कॉम्पोनेंट लेकिन @ कंट्रोलर

मेरे web.xml फ़ाइल तो दिखाई देता है:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app id="com-timbuk2-webapp-compositor" 
     version="3.0" 
     xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" > 

    <display-name>timbuk2-webapp-Compositor</display-name> 

    <!-- Context Parameters --> 
    <context-param> 
     <param-name>log4jConfigLocation</param-name> 
     <param-value>/WEB-INF/conf/log4j-config.xml</param-value> 
    </context-param> 

    <context-param> 
     <param-name>contextClass</param-name> 
     <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 
    </context-param> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>com.company.webapp</param-value> 
    </context-param> 

    <!-- Listeners --> 
    <listener> 
     <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
    </listener> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Filters --> 
    <filter> 
     <filter-name>characterEncodingFilter</filter-name> 
     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
     <init-param> 
      <param-name>encoding</param-name> 
      <param-value>UTF-8</param-value> 
     </init-param> 
     <init-param> 
      <param-name>forceEncoding</param-name> 
      <param-value>true</param-value> 
     </init-param> 
    </filter> 

    <filter> 
     <filter-name>urlRewriteFilter</filter-name> 
     <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> 
     <init-param> 
      <param-name>logLevel</param-name> 
      <param-value>commons</param-value> 
     </init-param> 
     <init-param> 
      <param-name>confPath</param-name> 
      <param-value>/WEB-INF/conf/urlrewrite-config.xml</param-value> 
     </init-param> 
    </filter> 

    <!-- Filter Mappings --> 
    <filter-mapping> 
     <filter-name>characterEncodingFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

    <filter-mapping> 
     <filter-name>urlRewriteFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

    <!-- Servlets --> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    </servlet> 

    <!-- Servlet mappings --> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/app/*</url-pattern> 
    </servlet-mapping> 


</web-app> 

मेरा मुख्य @Configuration वर्ग तो दिखाई देता है:

package com.company.webapp.config; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.support.ReloadableResourceBundleMessageSource; 
import org.springframework.web.servlet.view.InternalResourceViewResolver; 
import org.springframework.web.servlet.view.JstlView; 

@Configuration 
public class ApplicationConfiguration 
{  
    @Bean 
    public ReloadableResourceBundleMessageSource messageSource() 
    { 
     ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); 
     messageSource.setBasename("/WEB-INF/resources/messages"); 
     messageSource.setCacheSeconds(0); 
     return messageSource; 
    } 

    @Bean 
    public InternalResourceViewResolver viewResolver() 
    { 
     InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
     viewResolver.setViewClass(JstlView.class); 
     viewResolver.setPrefix("/WEB-INF/views/"); 
     viewResolver.setSuffix(".jsp"); 
     return viewResolver; 
    } 
} 

मेरे @Controller और @Component कक्षाएं सभी एक ही पैकेज में रहते हैं। ,

package com.company.webapp.controller; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.stereotype.Controller; 
import org.springframework.validation.BindingResult; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 

@Controller 
@RequestMapping(value = "/render") 
public class RenderController 
{ 
    private final Logger logger = LoggerFactory.getLogger(getClass()); 

    @RequestMapping(method = RequestMethod.GET) 
    public ModelAndView handleGet(BindingResult bindingResult) 
    { 
     ... eventually return a ModelAndView object... 
    } 
} 

तो दोहराना करने के लिए अपने @Controller कक्षाएं यूआरएल मैं निर्दिष्ट करने कर रहा हूँ करने के लिए मैप नहीं किया जा रहा है: उदाहरण के लिए, यहाँ मेरी app से एक @Controller क्लास है। जब ऐप मेरे स्थानीय टॉमकैट पर्यावरण में शुरू होता है या फिर से लोड हो जाता है तो मुझे सामान्य "मैप किए गए यूआरएल"/प्रस्तुत करने के लिए "कंसोल आउटपुट" नहीं दिख रहा है।

क्या कोई मुझे बता सकता है कि मुझे क्या याद आ रहा है जो मेरे @ नियंत्रक एनोटेटेड कक्षाओं को ढूंढ और पंजीकृत नहीं कर सकता है?

+0

आप एक ही basename जोड़ें: messageSource.setBasename ("/ WEB-INF/संसाधन/संदेश"); MessageSource.setBasenames() के साथ एकाधिक बेसनाम कैसे जोड़ें? यह बेसनाम के सरणी कहता है, लेकिन मुझे यकीन नहीं है कि पैरा में सरणी कैसे डालें। – Rihards

उत्तर

5

मुझे लगता है कि यह इसलिए होता है क्योंकि @Controller रों अपने स्वयं के आवेदन संदर्भ (जो डिफ़ॉल्ट रूप से <servletname>-servlet.xml से भरी हुई है), जब आपके contextClass और contextConfigLocation जड़ संदर्भ (ContextLoaderListener के एक, पर लागू होने वाली DispatcherServlet द्वारा उठाया जाना चाहिए जो डिफ़ॉल्ट रूप से applicationContext.xml से लोड किया गया है)।

DispatcherServlet आवेदन संदर्भ कॉन्फ़िगर करने के लिए आप contextClass और contextConfigLocation सर्वलेट का init-param रों के रूप में स्थापित करना चाहिए।

संपादित करें: यह व्यवहार DefaultAnnotationHandlerMapping की detectHandlersInAncestorContexts संपत्ति द्वारा नियंत्रित किया जाता है, तो वैकल्पिक दृष्टिकोण विन्यास के दौरान true के लिए सेट करने के लिए है:

@Bean 
public DefaultAnnotationHandlerMapping mapping() { 
    DefaultAnnotationHandlerMapping m = new DefaultAnnotationHandlerMapping(); 
    m.setDetectHandlersInAncestorContexts(true); 
    return m; 
} 
+0

मैंने वेबपैप के समान वैल्यू पर प्रेषक सर्वलेट के संदर्भ क्लास और संदर्भ कॉन्फ़िगर स्थान पैरामीटर सेट किए हैं। सब ठीक है, मुझे लगता है कि अब मैं थोड़ा उलझन में हूँ। क्या अब दो संदर्भ चल रहे हैं? इससे पहले, जब मैं सभी एक्सएमएल कॉन्फ़िगरेशन कर रहा था, तो जब मैं वेब ऐप शुरू या पुनः लोड करता हूं तो यूआरएल मैपिंग पंजीकृत होगा। अब वे पहले आने वाले अनुरोध किए जाने तक मैप नहीं किए जाते हैं। –

+0

1) हां, आपके पास दो अनुप्रयोग संदर्भ हैं - रूट संदर्भ और डिस्पैचर सर्वलेट का संदर्भ, जो रूट एक बच्चा है। यह स्प्रिंग एमवीसी अनुप्रयोगों के लिए विशिष्ट है, http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-servlet देखें। हालांकि, उन्हें एक ही पैकेज से एनोटेशन-आधारित कॉन्फ़िगरेशन लोड करने का अच्छा विचार नहीं है - सेम डुप्लिकेट किए जाएंगे। 2) डिस्प्लेर सर्लेट का संदर्भ प्रारंभ होता है जब सर्वलेट लोड होता है, ताकि आप '' के साथ नियंत्रण कर सकें। 3) मैंने वैकल्पिक समाधान जोड़ा। – axtavt

+0

अह्ह्ह ... मुझे लगता है कि मैं अंततः समझता हूं। बहुत बहुत धन्यवाद। –

1

अपने MVC डिस्पैचर सर्वलेट xml फ़ाइल जहां में इस जोड़े पैकेज @ नियंत्रक या @ कॉम्पोनेंट के लिए स्कैन किए जाने वाले सभी नियंत्रकों के लिए होना चाहिए।

<context:component-scan 
     base-package="packagesseperatedbycomma" /> 
+0

उन्होंने विशेष रूप से कहा कि वह xml-कम कॉन्फ़िगरेशन चाहता है – Levancho

1

मैं इस मुद्दे लेकिन मेरे web.xml के लिए निम्न कोड जोड़ने तय:

<servlet> 
     <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextClass</param-name> 
      <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 
     </init-param> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>org.xxxx.inquiry.config, org.xxxx.inquiry.controller</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

लेकिन यह कर मैं यह बता जहाँ मेरे नियंत्रकों के लिए देखने के लिए