【问题】 在webapp的WEB-INF下放入一个html,通过控制层Controller返回html报错(404)。但是返回jsp页面却不会报错。
【原因】静态的html访问不到,但是动态的jsp可以访问。英文SpringMVC中的控制器(org.springframework.web.servlet.DispatcherServlet)中默认是jsp页面,
默认的配置DispatcherServlet屏蔽了html页面的访问。【解决】在web.xml中添加如下代码
<servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping>
【目录】
【控制层】
@Controller public class CommonController { @GetMapping("/{url}") public String handle(@PathVariable String url) { System.out.println(url); return url; } }
【applicationContext.xml】
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 自动扫描指定包及其子包下的所有Bean类 --> <context:component-scan base-package="com.ysy.Service"/> </beans>
【daoContext.xml】
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"> <!-- 定义数据源Bean,使用C3P0数据源实现 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" p:driverClass="com.mysql.jdbc.Driver" p:jdbcUrl="jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC" p:user="root" p:password="123456"/> <!-- 配置MyBatis的核心组件:SqlSessionFactory 并为该SqlSessionFactory配置它依赖的DataSource 指定为com.ysy.Dao包下所有类注册别名 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource" p:typeAliasesPackage="com.ysy.Dao"/> <!-- 自动扫描指定包及其子包下的所有Mapper组件 --> <mybatis:scan base-package="com.ysy.Dao"/> </beans>
【springmvc-servlet.xml】
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 配置Spring自动扫描指定包及其子包中的所有Bean --> <context:component-scan base-package="com.ysy.Controller"/> <mvc:annotation-driven/> <!-- 将/resources/路径下的资源映射为/res/**虚拟路径的资源 --> <mvc:resources mapping="/res/**" location="/resources/"/> <!-- 将/images/路径下的资源映射为/imgs/**虚拟路径的资源 --> <!-- 配置InternalResourceViewResolver作为视图解析器 --> <!-- 指定prefix和suffix属性 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/content/" p:suffix=".html"/> </beans>
【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_4_0.xsd" version="4.0"> <servlet> <!-- 配置Spring MVC的核心控制器 --> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <!-- 配置Spring MVC的核心控制器处理所有请求 --> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <!-- 为创建Spring容器指定多个配置文件 --> <context-param> <!-- 参数名为contextConfigLocation --> <param-name>contextConfigLocation</param-name> <!-- 多个配置文件之间以“,”隔开 --> <param-value>/WEB-INF/daoContext.xml ,/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <!-- 使用ContextLoaderListener在Web应用启动时初始化Spring容器 --> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 定义字符编码的过滤器:CharacterEncodingFilter --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!--强制编码会导致html在显示的时候出现中文乱码--> <!-- <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> <!-- 使用CharacterEncodingFilter过滤所有请求 --> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
【test.html】
<!DOCTYPE html> <html> <head> <meta name="author" content="Yeeku.H.Lee(CrazyIt.org)"/> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <link rel="stylesheet" href="/res/bootstrap-4.3.1/css/bootstrap.min.css"> <script src="/res/jquery-3.4.1.min.js"> </script> <script src="/res/bootstrap-4.3.1/js/bootstrap.min.js"> </script> <title> 用户登录 </title> </head> <body> <div class="container"> <h4>用户登录</h4> <form method="post" action="login"> <div class="form-group row"> <label for="username" class="col-sm-3 col-form-label">用户名:</label> <div class="col-sm-9"> <input type="text" id="username" name="username" class="form-control" placeholder="输入用户名"> </div> </div> <div class="form-group row"> <label for="pass" class="col-sm-3 col-form-label">密码:</label> <div class="col-sm-9"> <input type="password" id="pass" name="pass" class="form-control" placeholder="输入密码"> </div> </div> <div class="form-group row"> <div class="col-sm-6 text-right"> <button type="submit" class="btn btn-primary">登录</button> </div> <div class="col-sm-6"> <button type="reset" class="btn btn-danger">重设</button> </div> </div> </form> </div> </body> </html>
【success.jsp】
<%@ page contentType="text/html; charset=utf-8" language="java" errorPage="" %> <!DOCTYPE html> <html> <head> <meta name="author" content="Yeeku.H.Lee(CrazyIt.org)"/> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <link rel="stylesheet" href="${pageContext.request.contextPath}/res/bootstrap-4.3.1/css/bootstrap.min.css"> <script src="${pageContext.request.contextPath}/res/jquery-3.4.1.min.js"> </script> <script src="${pageContext.request.contextPath}/res/bootstrap-4.3.1/js/bootstrap.min.js"> </script> <title> 登录成功 </title> </head> <body> <div class="container"> <div class="alert alert-primary">${tip}</div> </div> </body> </html>