【问题】 在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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://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/javaeehttp://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>

SpringMVC:返回HTML页面相关推荐

  1. js如何在当前页面加载springmvc返回的页面_手写SpringMVC学习

    前面我们学习了spring框架源码,做了一些自己手写的学习,最近,我们开始学习springMVC框架的学习 ,springMVC框架,相信大家不陌生了,所以这里不做过多的介绍了. SpringMVC以 ...

  2. springmvc返回html页面_深入浅出SpringMVC系列~

    写在前面: 小伙伴儿们,大家好!这次让我们一起来入门学习SpringMVC~ 思维导图: 1,环境配置 我们建一个Maven项目,把我们所需要的依赖引入进去:大概是下面这样: <dependen ...

  3. SpringMVC返回数据到页面的方法

    在JavaWeb项目中许多项目会使用SpringMVC作为开发框架,下面将介绍几种将后台Controller控制器执行的结果返回到页面的方法. 首先创建示例中需要用到的类.公共方法和结果显示页面. ( ...

  4. SpringMVC 返回json

    1.页面传递json数据,ajax传递 jsp <script type="text/javascript">$(document).ready(function(){ ...

  5. SSM 返回静态页面HTML Controller 被递归调用引起的StackOverflowError

    一 背景 最近在做工程实践,想实现这么一个效果: 前端url请求地址:localhost:8080/idevtools/search 后端返回一个静态页面HTML:search.html 按照网上说的 ...

  6. Spring Mvc返回html页面404错误解决记录--转载

    原文地址:http://53873039oycg.iteye.com/blog/2061992 以前使用Spring Mvc时候都是返回jsp页面或者ftl页面,昨天想返回html页面,spring- ...

  7. SpringMVC-学习笔记04【SpringMVC返回值类型及响应数据类型】

    Java后端 学习路线 笔记汇总表[黑马程序员] SpringMVC-学习笔记01[SpringMVC概述及入门案例][day01] SpringMVC-学习笔记02[参数绑定及自定义类型转换] Sp ...

  8. 【Java从0到架构师】SpringMVC - 返回值

    SpringMVC - 返回值 controller 中的返回值 无返回值 - 使用原始的 Servlet 的方法 普通文本.HTML - 设置响应头 Content-Type XML - 推荐使用 ...

  9. 什么是SpringMVC?SpringMVC之hello.jsp实现过程 问题:SpringMVC在JSP页面取不到ModelAndView中的值(已解决)

    兄弟,保持心情愉悦 初入本科,我就听到一个名词SSM,刚开始我还以为...wc计算机领域也好这口,当然在这里再次声明一下本人是个正经人,如果你翻看过的我的其他文章你会发现,我在最近发布的关与Sprin ...

最新文章

  1. Python:colorlog的三个例子
  2. python的生成器
  3. [转载]----linux系统工程师的前途在哪里?
  4. Spring查找方法示例
  5. php 检测死锁,MySQL 死锁检测
  6. JavaScript or JQuery 获取服务器时间
  7. PHP网站开发有哪些框架,罗列几款时下高人气的PHP开发框架
  8. 4556: [Tjoi2016Heoi2016]字符串
  9. tomcat设置编码为UTF-8
  10. HTML时间日期选择器
  11. UML图的各类符号解析
  12. 编程设计模式中委托 和代理模式的区别
  13. 计算机上分辨率怎么设置在哪里设置方法,win7分辨率怎么调|win7如何设置分辨率...
  14. Python学习笔记哈哈哈
  15. MathType 公式编辑器公式大小调整
  16. 大脑--物质与意识之综合体,是物质有双重现象(物性与灵性)的实证
  17. 获取列——Excel的COLUMN、COLUMNS
  18. win7为啥总扫描计算机,Win7系统U盘插入电脑后就会提示扫描并修复怎么办
  19. 微信公众号文章是否违规怎么检测?
  20. 中国实验室管理系统行业研究与未来预测报告(2022版)

热门文章

  1. MySQL or条件命中
  2. 当你部署tomcat遇到HTTP Status 500的时候
  3. tools:完全教程 Aircrack-ng
  4. 百度seo快排点击系统源码[易语言]拨号模拟点击(仅供学习使用!)
  5. 软件工程-人事管理系统项目(一)
  6. 记录自己第一次科研经历
  7. 华工2018计算机网络随堂练习,计算机网络随堂练习-华工2019随堂练习
  8. 围棋的分数计算机,围棋比赛积分规则
  9. 程序设计与算法(三)期末考试之013:编程填空:三生三世
  10. 十二星座时间及其英文缩写