这是 web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<display-name>BulkSMS2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>BulkSMS2</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/BulkSMS2-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>BulkSMS2</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/BulkSMS2-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
Dispatcher-Servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.nt.beans" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
当我运行该应用程序时,出现 404 错误。我已将 index.jsp 文件放置在 WEB-INF/pages/index.jsp 文件夹中,但仍然遇到相同的错误。我已经检查了所有配置,但不知道为什么找不到该页面。
最佳答案
您的应用程序存在一些配置问题。首先是您指定应用程序上下文的方式。从您的配置来看,您似乎有一个应用程序上下文 BulkSMS2-servlet.xml。因此,您不需要根应用程序上下文。因此,请删除 web.xml 中的此部分
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/BulkSMS2-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
第二个问题是你的 url 映射/*。它覆盖所有 servlet 映射,包括容器提供的 jsp 处理。这意味着包括 jsps 请求在内的所有请求都由您的 DispatcherServlet 处理。更改这部分
群发短信2 /*
致 群发短信2 /
这会覆盖容器默认 servlet 处理程序,但不会覆盖特殊容器 servlet。要处理静态资源,请将以下行添加到 BulkSMS2-servlet.xml
<mvc:default-servlet-handler/>
要以静态资源的形式访问您的index.jsp,您需要将其放在WEB-INF 文件夹之外的根webapp 文件夹中。但是,如果您想将其保留在 WEB-INF 文件夹中,那么您的请求应该由 Controller 处理
@RequestMapping("/")
public class HomeController{
@RequestMapping()
public String showHomePage(){
return "index";
}
}
关于java - 无法在 spring mvc 中加载默认索引页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37269282/