Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用/时无法映射到Spring控制器,但它可以与/*_Java_Spring_Spring Mvc - Fatal编程技术网

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

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

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

我正在使用URL:

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 };
    }
}
控制员:

@Controller
public class BaseController {

    @RequestMapping(value = { "/", "/welcome" }, method = RequestMethod.GET)
    public String welcome(ModelMap model) {
        // welcome.jsp
        return "welcome";
    }
}
服务器启动日志:(我正在使用JBoss7.1)

用一个普通的, /或者/*可以很好地用于服务器端,如果您使用Extjs或任何其他方式,只需像这样使用

在web xml中

<servlet-mapping>
    <servlet-name>Welcome</servlet-name>    
    <url-pattern>*.action</url-pattern>
</servlet-mapping>
因为*.action将调用所有文件。。 你也可以使用 *.htm
*.do

在您的
WebMvcConfig
中覆盖
configureDefaultServletHandling
并调用传入的
configurer
上的
enable
。这将把无法解析的任何内容委托给默认servlet。这也应该使
/
起作用。它不会使
/
起作用,但当使用
/*
时,它允许JSP被服务,但就好像它是一个静态文件一样,所以不是真正有用。它应该委托给呈现JSP的默认servlet。这可能是一个JBoss特有的东西,但它不起作用,就像Tomcat或Jetty的魅力一样。事实上,我认为这确实是一个服务器特有的东西——或者至少是一个部署问题。当我使用Maven插件在tomcat7上运行应用程序时(MVNTomcat7:run),一切都正常工作,即使使用
/
)。相反,当我把它作为war文件放到Tomcat webapps文件夹中时,它就不起作用了。但至少这是可以继续下去的。。。谢谢
@Controller
public class BaseController {

    @RequestMapping(value = { "/", "/welcome" }, method = RequestMethod.GET)
    public String welcome(ModelMap model) {
        // welcome.jsp
        return "welcome";
    }
}
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
<servlet-mapping>
    <servlet-name>Welcome</servlet-name>    
    <url-pattern>*.action</url-pattern>
</servlet-mapping>
@RequestMapping(value = "/Welcome.action", method = RequestMethod.POST)