使用/时无法映射到Spring控制器,但可以使用/ *

时间:2014-01-07 08:53:50

标签: java spring spring-mvc

我正在尝试通过扩展AbstractAnnotationConfigDispatcherServletInitializerWebMvcConfigurerAdapter来仅使用java配置来配置Spring MVC,但是我遇到了将请求正确映射到我的应用程序的问题。这似乎是getServletMappings()中servlet映射的一个微妙问题。如果我指定“/”,我根本无法访问我的控制器类;没有错误。如果我使用“/ *”然后我可以到达控制器,但视图没有正确解析(可能与附加的上下文路径有关):

WARN  [org.springframework.web.servlet.PageNotFound] (http-localhost-127.0.0.1-8080-1) No mapping found for HTTP request with URI [/my-project/WEB-INF/views/welcome.jsp] in DispatcherServlet with name 'dispatcher'

我正在使用网址:

http://localhost:8080/my-project/welcome

以下是我的课程:

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { RootConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { WebMvcConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/*" }; // Using only "/" doesn't appear to work
    }

    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        return new Filter[] { characterEncodingFilter };
    }
}

MVC配置:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.myproject.controller" })
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

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

控制器:

@Controller
public class BaseController {

    @RequestMapping(value = { "/", "/welcome" }, method = RequestMethod.GET)
    public String welcome(ModelMap model) {
        // welcome.jsp
        return "welcome";
    }
}

服务器启动日志:(我正在使用JBoss 7.1)

09:37:24,440 INFO  [org.springframework.web.context.ContextLoader] (MSC service thread 1-10) Root WebApplicationContext: initialization started
09:37:24,441 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (MSC service thread 1-10) Refreshing Root WebApplicationContext: startup date [Tue Jan 07 09:37:24 CET 2014]; root of context hierarchy
09:37:24,474 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (MSC service thread 1-10) Registering annotated classes: [class com.myproject.configuration.WebSecurityConfiguration,class com.myproject.configuration.RootConfig]
09:37:24,632 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (MSC service thread 1-10) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
09:37:25,750 INFO  [stdout] (MSC service thread 1-10) Error :  91

09:37:26,753 INFO  [stdout] (MSC service thread 1-10) Error :  91

09:37:26,847 INFO  [org.springframework.security.web.DefaultSecurityFilterChain] (MSC service thread 1-10) Creating filter chain: Ant [pattern='/resources/**'], []
09:37:26,891 INFO  [org.springframework.security.web.DefaultSecurityFilterChain] (MSC service thread 1-10) Creating filter chain: org.springframework.security.web.util.matcher.AnyRequestMatcher@1, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@2adf45ef, org.springframework.security.web.context.SecurityContextPersistenceFilter@79a4f1de, org.springframework.security.web.header.HeaderWriterFilter@3036c16f, org.springframework.security.web.csrf.CsrfFilter@52f9c83e, org.springframework.security.web.authentication.logout.LogoutFilter@2ad7d56d, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@21d18342, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@2a83402, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@3626e2d2, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@286863e6, org.springframework.security.web.session.SessionManagementFilter@7480c96b, org.springframework.security.web.access.ExceptionTranslationFilter@4ff59f9e, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@4ba78535]
09:37:26,911 INFO  [org.springframework.web.context.ContextLoader] (MSC service thread 1-10) Root WebApplicationContext: initialization completed in 2470 ms
09:37:26,917 INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/my-project]] (MSC service thread 1-10) Initializing Spring FrameworkServlet 'dispatcher'
09:37:26,918 INFO  [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-10) FrameworkServlet 'dispatcher': initialization started
09:37:26,922 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (MSC service thread 1-10) Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Tue Jan 07 09:37:26 CET 2014]; parent: Root WebApplicationContext
09:37:26,926 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (MSC service thread 1-10) Registering annotated classes: [class com.myproject.configuration.WebMvcConfig]
09:37:26,973 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (MSC service thread 1-10) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
09:37:27,041 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-10) Mapped "{[/ || /welcome],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.myproject.controller.BaseController.welcome(org.springframework.ui.ModelMap,java.lang.String)
09:37:27,043 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-10) Mapped "{[/bindcheck/{dn}/{pwd}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.myproject.controller.BaseController.canUserBind(java.lang.String,java.lang.String,org.springframework.ui.ModelMap)
09:37:27,045 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-10) Mapped "{[/usersearch/{userId}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.myproject.controller.BaseController.usersearch(java.lang.String,org.springframework.ui.ModelMap)
09:37:27,066 INFO  [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-10) Mapped URL path [/resources/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
09:37:27,149 INFO  [org.hibernate.validator.util.Version] (MSC service thread 1-10) Hibernate Validator 4.2.0.Final
09:37:27,231 INFO  [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-10) FrameworkServlet 'dispatcher': initialization completed in 312 ms
09:37:27,238 INFO  [org.jboss.web] (MSC service thread 1-10) JBAS018210: Registering web context: /my-project
09:37:27,243 INFO  [org.jboss.as] (MSC service thread 1-13) JBAS015951: Admin console listening on http://127.0.0.1:9990
09:37:27,243 INFO  [org.jboss.as] (MSC service thread 1-13) JBAS015874: JBoss AS 7.1.1.Final "Brontes" started in 5294ms - Started 226 of 303 services (76 services are passive or on-demand)
09:37:27,279 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "my-project.war"
09:37:33,221 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) null

1 个答案:

答案 0 :(得分:1)

使用普通的, /或/ *可以在服务器端正常工作,如果你使用的是Extjs,或者任何其他方式只是像这样使用,

在web-xml中,

<servlet-mapping>
    <servlet-name>Welcome</servlet-name>    
    <url-pattern>*.action</url-pattern>
</servlet-mapping>

在控制器类

@RequestMapping(value = "/Welcome.action", method = RequestMethod.POST)

因为* .action会调用所有文件.. 你也可以用 的* .htm *。做