SpringMVC:返回HTML页面
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                SpringMVC:返回HTML页面
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                【問題】?在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页面的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: itext 导出word
- 下一篇: 基于Glodstein枝切法相位解包裹算
