所以我正在尝试在没有XML的情况下配置我的Web应用程序并进入所有带注释的路径。我有一些使用@Configuration和@Component注释的类被自动拾取,但由于某种原因,我的@Controller注释未被识别并映射到相应的@RequestMapping值。
我的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类都存在于同一个包中。例如,这是我的应用程序中的@Controller类:
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类没有映射到我指定的URL。当应用程序启动或在我的本地tomcat环境中重新加载时,我没有看到通常的“映射URL”/渲染“到...”控制台输出。
有人能告诉我我错过了什么可能会阻止我的@Controller注释类无法找到并注册?
答案 0 :(得分:5)
我想这是因为@Controller
应该由DispatcherServlet
从其自己的应用程序上下文(默认情况下从<servletname>-servlet.xml
加载)中挑选contextClass
和contextConfigLocation
ContextLoaderListener
应用于根上下文(applicationContext.xml
的一个,默认情况下从DispatcherServlet
加载)。
要配置contextClass
应用程序上下文,您应将contextConfigLocation
和init-param
设置为servlet的detectHandlersInAncestorContexts
。
修改强>
此行为由DefaultAnnotationHandlerMapping
的属性true
控制,因此备用方法是在配置期间将其设置为@Bean
public DefaultAnnotationHandlerMapping mapping() {
DefaultAnnotationHandlerMapping m = new DefaultAnnotationHandlerMapping();
m.setDetectHandlersInAncestorContexts(true);
return m;
}
:
{{1}}
答案 1 :(得分:1)
在你的mvc dispatcher servlet xml文件中添加它,其中包应该是为@controller或@Component扫描的所有控制器。
<context:component-scan
base-package="packagesseperatedbycomma" />
答案 2 :(得分: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>
但这样做我告诉它在哪里寻找我的控制器