Spring MVC配置静态资源和资源包教程
1- 介绍
- Spring 4 MVC
2- 创建一个项目
- File/New/Other..



输入:
- Group ID: com.yiibai
- Artifact ID: SpringMVCResource
- Package: com.yiibai.springmvcresource
项目被创建以后如下:

不要担心有错误消息在项目被创建时。原因是,我们还没有声明 Servlet 库。
注意:

3- 配置Maven
- pom.xml
<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.yiibai</groupId>
<artifactId>SpringMVCResource</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringMVCResource 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 API -->
<!-- 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> <!-- Jstl for jsp page -->
<!-- http://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <!-- JSP API -->
<!-- http://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency> <!-- Spring dependencies -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> <!-- http://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> <!-- 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>SpringMVCResource</finalName>
<plugins> <!-- Config: Maven Tomcat Plugin -->
<!-- http://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version> <!-- Config: contextPath and Port (Default - /SpringMVCResource : 8080) --> <!--
<configuration>
<path>/</path>
<port>8899</port>
</configuration>
-->
</plugin>
</plugins>
</build> </project>
4- 配置Spring
配置 web.xml

- WEB-INF/web.xml
<web-app 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"
version="3.0"> <display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- Other XML Configuration -->
<!-- Load by Spring ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/root-context.xml
</param-value>
</context-param> <!-- Spring ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </web-app>
- WEB-INF/spring-mvc-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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- Package Scan -->
<context:component-scan base-package="com.yiibai.springmvcresource" /> <!-- Enables the Spring MVC Annotation Configuration -->
<context:annotation-config /> <!-- Important!! -->
<!-- Default Servlet Handler (For Resources *.css, *.js, image,..) -->
<mvc:default-servlet-handler />
<mvc:annotation-driven /> <!-- Config resource mapping -->
<mvc:resources mapping="/styles/**" location="/WEB-INF/resources/css/" /> <!-- Config Properties file -->
<bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list><value>classpath:ApplicationRB.properties</value></list>
</property>
</bean> <bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix">
<value>/WEB-INF/pages/</value>
</property> <property name="suffix">
<value>.jsp</value>
</property> <property name="exposedContextBeanNames">
<list><value>appProperties</value></list>
</property>
</bean>
</beans>
- WEB-INF/root-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Empty --> </beans>
<!-- /WEB-INF/spring-mvc-servlet.xml --> <!-- Important!! -->
<!-- Default Servlet Handler (For Resources *.css, *.js, image,..) -->
<mvc:default-servlet-handler />
<mvc:annotation-driven />

配置Properties文件:
5- Java类
- MyController.java
package com.yiibai.springmvcresource; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class MyController { @RequestMapping(value = "/staticResourceTest")
public String staticResource(Model model) {
return "staticResourceTest";
} @RequestMapping(value = "/resourceBundleTest")
public String resourceBundle(Model model) {
return "resourceBundleTest";
}
}
6- 资源包,静态资源和视图
Resource Bundle (Properties file):

- ApplicationRB.properties
text.loginPrompt=Enter user name and password
text.userName=User Name
text.password=Password

- scripts/common.js
function sayHello() {
alert("Hello from JavaScript");
}
- /WEB-INF/resource/css/commons.css
.button {
font-size: 20px;
background: #ccc;
} .red-text {
color: red;
font-size: 30px;
} .green-text {
color: green;
font-size: 20px;
}
视图(两个JSP文件)

- staticResourceTest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring MVC Resource example</title> <script type="text/javascript"
src="${pageContext.request.contextPath}/scripts/common.js"></script> <link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/styles/common.css"> </head>
<body> <div class="red-text">Red text</div>
<br>
<div class="green-text">Green text</div>
<br> <input type="button" class="button" onclick="sayHello();"
value="Click me!"> </body>
</html>
- resourceBundleTest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring MVC Resource Bundle example</title>
</head>
<body> <h2>${appProperties['text.loginPrompt']}</h2> ${appProperties['text.userName']} <input type="text" name="userName"> <br>
${appProperties['text.password']} <input type="password" name="password"> <br> </body>
</html>
7- 运行应用程序




输入:
- Name: Run SpringMVCResource
- Base directory: ${workspace_loc:/SpringMVCResource} 可选择“Browse Workspace..." 来选对应项目。
- Goals: tomcat7:run

点击 Run

- http://localhost:8080/SpringMVCResource/scripts/common.js
- http://localhost:8080/SpringMVCResource/styles/common.css
- http://localhost:8080/SpringMVCResource/staticResourceTest
- http://localhost:8080/SpringMVCResource/resourceBundleTest
Spring MVC配置静态资源和资源包教程的更多相关文章
- Spring MVC配置静态资源和资源包
Spring MVC配置静态资源和资源包 本例映射:css目录: pom.xml <properties> <spring.version>4.3.5.RELEASE</ ...
- Spring MVC配置静态资源的正常访问
SpringMVC如果过滤器过滤范围配置了/或者/*,那么框架会过滤所有请求,包括自己写的请求和静态资源请求,这样静态资源就不能正常加载,包括js文件.css文件.图片资源访问的时候都会出现404页面 ...
- Spring MVC 处理静态资源文件
摘要: 三个方案: 1.方案一:激活Tomcat的defaultServlet来处理静态文件 2.方案二: 在spring3.0.4以后版本提供了mvc:resources (需要配置annotati ...
- 【Spring学习笔记-MVC-14】Spring MVC对静态资源的访问
作者:ssslinppp 参考链接: http://www.cnblogs.com/luxh/archive/2013/03/14/2959207.html http://www.cnb ...
- spring mvc对静态资源的访问
如果我们的项目使用的是springmvc,在web.xml中会有一段这的配置. <servlet> <servlet-name>springMvc</servlet-na ...
- 【Spring学习笔记-MVC-18.1】Spring MVC实现RESTful风格-同一资源,多种展现:xml-json-html
概要 要实现Restful风格,主要有两个方面要讲解,如下: 1. 同一个资源,如果需要返回不同的形式,如:json.xml等: 不推荐的做法: /user/getUserJson /user/get ...
- Spring mvc 增加静态资源配置后访问不了注解配置的controller
spring mvc 增加静态资源访问配置. 例如: <!-- 静态资源映射 --> <mvc:resources location="/static/" map ...
- spring mvc: 多解析器映射(资源绑定视图解析器 + 内部资源[普通模式/]视图解析器)
spring mvc: 多解析器映射(资源绑定视图解析器 + 内部资源[普通模式/]视图解析器) 资源绑定视图解析器 + 内部资源(普通模式)视图解析器 并存方式 内部资源视图解析器: http:// ...
- 最小可用 Spring MVC 配置
[最小可用 Spring MVC 配置] 1.导入有概率用到的JAR包, -> pom.xml 的更佳实践 - 1.0 <- <project xmlns="http:// ...
随机推荐
- High Speed Inter-CHIP USB 2.0 PHY
转载:http://arasan.com/products/usb/usb-2-0/hsic-phy/ High Speed Inter-CHIP USB 2.0 PHY USB is the ubi ...
- [SpringMVC+redis]自定义aop注解实现控制器访问次数限制
原文:http://www.cnblogs.com/xiaoyangjia/p/3762150.html?utm_source=tuicool 我们需要根据IP去限制用户单位时间的访问次数,防止刷手机 ...
- RequireJs 入门
官网:http://www.requirejs.cn/ 使用方法: 1.引入require.js 可以在底部引入: <script type="text/javascript" ...
- java中List和Array相互转换
List to Array List 提供了toArray的接口,所以可以直接调用转为object型数组 List<String> list = new ArrayList<Stri ...
- MongoDB分片集群新增分片(自用)
机器IP为192.168.58.11,计划在上面新建两个分片并添加到原有分片集群中. 实施如下: 1.58.11创建mongodb文件夹 mkdir -p /opt/mongodb cd /opt/ ...
- Windows下启动Solr报错:Nothing to start,exiting...
如果用java -jar start.jar命令启动Solr时报错:Nothing to start,exiting...,可尝试: 百度jetty,上官网下载压缩包并解压(下载页面地址:http:/ ...
- 【重点突破】—— 当better-scroll 遇见Vue
前言:在学习黄轶老师的<Vue.js高仿饿了么外卖App>课程中接触到了better-scroll第三方JavaScript组件库,这是黄轶老师自己基于iscroll重写的库.这里结合黄轶 ...
- rails generate model/resource/scaffold的区别
If you’re just learning Ruby on Rails, you may be confused as to when to generate individual models, ...
- javascript中按位操作的应用,如何快速取整 判断字符串是否是包含某字符串
最近在看最基础的<javascript高级程序设计>看的灰常慢,看到按位运算这里,突然反思,这种鬼操作到底有什么实际的应用呢? 按位运算符有6个 & 按位与:a & b | ...
- 【高德地图API】从零開始学高德JS API(二)地图控件与插件——測距、圆形编辑器、鼠标工具、地图类型切换、鹰眼鱼骨
不管是控件还是插件,都是在一级API接口的基础上,进行二次开发,封装的一系列更加便于开发人员使用.降低开发人员工作量的二级API接口.除了官方通用的鱼骨.鹰眼控件,还有大量官方开发的地图插件,相似谷歌 ...