Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.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 请求映射中的通配符_Java_Spring_Spring Mvc_Annotations_Http Status Code 404 - Fatal编程技术网

Java 请求映射中的通配符

Java 请求映射中的通配符,java,spring,spring-mvc,annotations,http-status-code-404,Java,Spring,Spring Mvc,Annotations,Http Status Code 404,我正在开发我的Spring应用程序,根据下面的代码,我正在将多个URL映射到一个方法。我正在创建一个方法“root”,以满足所有以索引和root开头的请求 @RequestMapping(value = {"/", "index*"}, method = RequestMethod.GET) public String root(Model model) { logger.info("Welcome to index page."); model.addAttribute("

我正在开发我的Spring应用程序,根据下面的代码,我正在将多个URL映射到一个方法。我正在创建一个方法“root”,以满足所有以索引和root开头的请求

@RequestMapping(value = {"/", "index*"}, method = RequestMethod.GET)
public String root(Model model) {
    logger.info("Welcome to index page.");  
    model.addAttribute("hello", "Welcome to index page." );     
    return "index";
}
上面的
“index*”
“index”、“index123”、“index.html”和“index.txt”
的情况下工作正常,请求被定向到映射的方法,即
“root”
,但它不适用于
“index.jsp”
。在
“index.jsp”
的情况下,我将获得
“HTTP状态404”“请求的资源不可用”

摘自
web.xml

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

appServlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring/appServlet/servlet-context.xml
1.
appServlet
/

有人能告诉我这背后的原因吗?我想这可能是因为servlet容器将*.jsp请求映射到
JspServlet
,以便编译和执行jsp。如果您使用的是Tomcat,那么这种情况会发生在Tomcat的
conf
目录的顶层
web.xml
。这意味着以*.jsp结尾的请求将在到达控制器之前被拦截。
JspServlet
将尝试根据webapp中的路径加载
index.jsp
文件,当找不到时将返回404


对于
/index.jsp
,您最好在webapp的根目录中实际创建此文件,这样您就不会看到404。您可以向其中添加一些代码以将请求重定向到其他地方(可能是到
/index.html
-或应用程序的其他入口点)。

我不想映射所有JSP,只想为index*类型请求使用一个方法。在您的web.xml中,dispatcher servlet映射了“/”url?是,在我上面的问题中添加了
web.xml
的摘录。