以下内容引用自http://wiki.jikexueyuan.com/project/spring/mvc-framework/spring-static-pages-example.html:

例子:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.jsoft.testspring</groupId><artifactId>teststaticpage</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>teststaticpage Maven Webapp</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- Servlet Library --><!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><!-- Spring Core --><!-- http://mvnrepository.com/artifact/org.springframework/spring-core --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.1.4.RELEASE</version></dependency><!-- Spring Web --><!-- http://mvnrepository.com/artifact/org.springframework/spring-web --><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.1.4.RELEASE</version></dependency><!-- Spring Web MVC --><!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.1.4.RELEASE</version></dependency></dependencies><build><finalName>teststaticpage</finalName><plugins><!-- Config: Maven Tomcat Plugin --><!-- http://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin --><!-- http://tomcat.apache.org/maven-plugin-2.0/tomcat7-maven-plugin/plugin-info.html --><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><!-- Config: contextPath and Port (Default:8080) --><!-- <configuration> <path>/</path> <port>8899</port> </configuration>--></plugin><!-- Config: Maven Jetty Plugin --><!-- http://mvnrepository.com/artifact/org.mortbay.jetty/jetty-maven-plugin --><!-- http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html --><plugin><groupId>org.eclipse.jetty</groupId><artifactId>jetty-maven-plugin</artifactId><version>9.4.3.v20170317</version><!-- Config: contextPath and Port (Default:8080) --><!--             <configuration><httpConnector><port>8899</port></httpConnector><webAppConfig><contextPath>/</contextPath></webAppConfig></configuration>--></plugin></plugins>        </build>
</project>

web.xml:

<web-app id="WebApp_ID" version="3.0"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><display-name>Spring MVC Application</display-name><servlet><servlet-name>spring-mvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 默认:[servlet-name]-servlet.xml --><!-- 通过初始化参数,指定xml文件的位置 --><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/springmvc-context.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet>   <servlet-mapping><servlet-name>spring-mvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>

springmvc-context.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"><context:component-scan base-package="com.jsoft.testspring"/><context:annotation-config/><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix"><value>/WEB-INF/jsp/</value></property><property name="suffix"><value>.jsp</value></property>   </bean><mvc:resources location="/WEB-INF/pages/" mapping="/pages/**"/><mvc:annotation-driven/></beans>

说明:<mvc:resources..../>标签被用来映射静态页面。 mapping属性必须是一个指定一个http请求URL模式的Ant模式。 location属性必须指定一个或者多个具有包含图片,样式表,JavaScript和其他静态内容的静态页面的资源目录位置。多个资源位置可以使用逗号分隔这些值的列表来被指定。<mvc:annotation-driven/>标签必须带上。

WebController.java:

package com.jsoft.testspring.teststaticpage;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;@Controller
public class WebController {@RequestMapping(value="/index",method=RequestMethod.GET)public String index(){return "index";}@RequestMapping(value="/staticPage",method=RequestMethod.GET)public String redirect(){return "redirect:/pages/final.html";}}

index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head><title>Spring Landing Page</title>
</head>
<body>
<h2>Spring Landing Pag</h2>
<p>Click below button to get a simple HTML page</p>
<form:form method="GET" action="staticPage">
<table><tr><td><input type="submit" value="Get HTML Page"/></td></tr>
</table>
</form:form>
</body>
</html>

final.html:

<!DOCTYPE html>
<html>
<head><title>Spring Static Page</title>
</head>
<body><h2>A simple HTML page</h2></body>
</html>

测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test19/teststaticpage

转载于:https://www.cnblogs.com/EasonJim/p/6919558.html

Spring MVC静态资源实例相关推荐

  1. Spring MVC静态资源处理(转)

    优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置DispatcherServlet的请求映射,往往 ...

  2. Spring MVC静态资源处理

    优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置DispatcherServlet的请求映射,往往 ...

  3. spring mvc静态资源访问的配置

    如果我们使用spring mvc来做web访问请求的控制转发,那么默认所有访问都将被DispatcherServlet独裁统治.比如我现在想访问的欢迎页index.html根本无需任何业务逻辑处理,仅 ...

  4. Spring MVC:资源

    我从博客读者那里收到的最常见的问题之一是如何在Spring MVC的应用程序中使用CSS和javascript文件. 因此,这是撰写有关Spring MVC中资源使用情况的文章的好机会. 通常,我将使 ...

  5. Spring Boot集成Ueditor富文本编辑器,实现图片上传,视频上传,返回内容功能并且通过OSS转换为链接并且解决Spring Security静态资源访问以及跨域问题

    学习自https://cloud.tencent.com/developer/article/1452451 现在是晚上22点,刚刚和我们的前端交流完了富文本编辑器的一些意见和看法 还是老样子 需求 ...

  6. Spring Boot 静态资源处理,妙!

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 来源:cnblogs.com/paddix/p/8301331.h ...

  7. Spring Boot静态资源访问和配置全解析

    在web开发中,静态资源的访问时必不可少的,比如image.css.js等.SpringBoot对静态资源访问提供了很好的支持,使用其提供的基本默认配置基本可以满足开发需求,同时,又支持开发人员进行自 ...

  8. Spring Boot 静态资源映射与上传文件路由配置

    默认静态资源映射目录 默认映射路径 在平常的 web 开发中,避免不了需要访问静态资源,如常规的样式,JS,图片,上传文件等;Spring Boot 默认配置对静态资源映射提供了如下路径的映射 /st ...

  9. Spring Boot 静态资源处理,原来如此!

    点击上方"朱小厮的博客",选择"设为星标" 后台回复"加群"加入公众号专属技术群 来源:rrd.me/faAmu 做web开发的时候,我们往 ...

最新文章

  1. JavaScript 立即执行函数的两种写法
  2. 特征重要度(feature importance)如何获取、排序、可视化、以及可视化阈值设置?
  3. 院士论坛|李德仁:测绘遥感与智能驾驶
  4. Linux Kernel TCP/IP Stack — L3 Layer — 邻居发现子系统
  5. AD备份文件安装额外域控制器
  6. SQL中关联表并使用子表的COUNT和SUM函数作为扩展字段
  7. sizeof 的结果取决于什么
  8. Grafana分析Nginx日志
  9. 蔬菜名称大全500种_96种室内盆栽植物图片及名称,室内植物品种大全
  10. 教育|我在美国读博士才发现,美国高等教育如此残酷,以前的感觉完全是扯淡...
  11. 如何配置YUM本地更新源
  12. 北京交通大学离散数学 谓词逻辑_离散数学_北京交通大学_中国大学MOOC(慕课)
  13. 判断浏览器是否支持html5
  14. python爬取双色球2003-2022年所有数据
  15. Genymotion 各对应版本
  16. AM335x启动流程(BootRom-MLO-Uboot)超详细源码分析
  17. 面试官:你了解 QPS、TPS、RT、吞吐量 这些高并发性能指标吗?
  18. python输出偶数_python程序使用递归查找数字是偶数还是奇数
  19. 第四讲 switch结构和循环(switch结构、while循环、do-while循环)
  20. 包含重复元素的全排列

热门文章

  1. 优秀!Jupyter 与 PyCharm 可以完美融合!
  2. 人工智能 21 个子领域高被引学者Top 3
  3. 最全深度学习资源列表!
  4. 我喜欢阅读和计算机英语,关于自考中的计算机专业
  5. android 应用切换动画,怎么在Android应用中利用Activity对动画进行切换
  6. 理解CapsuleNetwork2
  7. python函数实例化_Python中的__new__()方法与实例化
  8. java文件不能转class_安了jdk 却不能将.java文件转换成.class 文件,一运行就说‘javac’不是内部或外部指令,却能运行.class文...
  9. Java基础---分支结构(if--else / switch---case)
  10. SQL Server数据库-限制返回行数