Spring MVC配置静态资源和资源包
Spring MVC配置静态资源和资源包
本例映射:css目录:
pom.xml
<properties>
<spring.version>4.3.5.RELEASE</spring.version>
</properties> <dependencies> <!-- spring模块库 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<!-- transaction事务 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency> <!-- Servlet dependencies -->
<!-- servlet(HttpServletRequest,HttpServletResponse) -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency> <!-- Jstl for jsp page -->
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</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> </dependencies> <build>
<finalName>gugua18</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
</plugin>
</plugins>
</pluginManagement>
</build>
web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param> <servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> </web-app>
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> </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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 引入包 -->
<context:component-scan base-package="springmvc"/> <!-- 自动装配 -->
<context:annotation-config/> <!-- Important!! -->
<!-- 配置静态资源: -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven/> <!-- Config resource mapping -->
<!-- 资源映射 -->
<mvc:resources location="/WEB-INF/resources/css/" mapping="/styles/**"></mvc:resources> <!-- Config Properties file -->
<!-- 加载文件属性 -->
<bean id="addProperties" 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/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
<!-- exposedContextBeanNames属性来限制能够暴露到页面上的spring bean的名称列表 -->
<property name="exposedContextBeanNames">
<list>
<value>addProperties</value>
</list>
</property>
</bean> </beans>
将styles/css/目录映射到/WEB-INF/resources/css目录下
<mvc:resources location="/WEB-INF/resources/css/" mapping="/styles/**"></mvc:resources>
ApplicationRB.properties
text.loginPrompt=Enter user name and password
text.userName=User Name
text.password=Password
然后新建common.js/common.css
注意路径;
webapp/scripts/common.js
function sayHello() {
alert("Hello from JavaScript");
}
webaapp/WEB-INF/resources/css/common.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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>resource example</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/common.js"></script>
<link src="${pageContext.request.contextPath}/styles/css/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>
staticBoundleTest.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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>boundle resouce example</title>
</head>
<body> <h3>${addProperties['text.loginPrompt']}</h3> ${addProperties['text.userName']}:<input type="text" name="userName"/><br>
${addProperties['text.password']}:<input type="password" name="password"/> </body>
</html>
控制器:
MyController.jva
package springmvc.controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; @Controller
public class MyController { @RequestMapping(value="/staticResourceTest", method=RequestMethod.GET)
public String staticResource(Model model)
{ return "staticResourceTest";
} @RequestMapping(value="/staticBoundleTest", method=RequestMethod.GET)
public String staticBoundle(Model model)
{
return "staticBoundleTest";
}
}
注意方法中一定要带:Model model,否则系统配置的文件属性,无法传递到jsp页面
访问地址:
http://localhost:8080/gugua18/staticBoundleTest
http://localhost:8080/gugua18/staticResourceTest
Spring MVC配置静态资源和资源包的更多相关文章
- Spring MVC配置静态资源和资源包教程
1- 介绍 这篇教程文章是基于: Spring 4 MVC 2- 创建一个项目 File/New/Other.. 输入: Group ID: com.yiibai Artifact ID: Sprin ...
- 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:// ...
随机推荐
- mysql数据库新插入数据,需要立即获取最新插入的id
在MySQL中,使用auto_increment类型的id字段作为表的主键.通常的做法,是通过“select max(id) from tablename”的做法,但是显然这种做法需要考虑并发的情况, ...
- Java导出Excel表,POI 实现合并单元格以及列自适应宽度(转载)
POI是apache提供的一个读写Excel文档的开源组件,在操作excel时常要合并单元格,合并单元格的方法是: sheet.addMergedRegion(new CellRangeAddress ...
- js 的正则表达式 部分展示test()方法的验证功能
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 亿级别G级别文本数据去重
亿级别G级别文本数据去重 文件总行数 字节数 去重后行数 [root@d mongoexport]# wc -l superpub-ask-question.csv126530681 superpub ...
- stark - 增、删、改
一.效果图 二.增.删.改 知识点: 1.解决代码重用 {% include 'form.html' %} 2.自定制配置modelform 每张表,就可自定义配置 labels , widges.. ...
- H5上传压缩图片
看这个,比较全的 https://github.com/mhbseal/html5ImgCompress ,几乎所有痛点都解决了! PC上传图片 基本结构 form[enctype="mul ...
- MAVEN项目(仓库中没有jar包)
E:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\bajie\WEB-INF\lib 把jar包放 ...
- 【Python】Python 读取csv的某行或某列数据
Python 读取csv的某行 转载 2016年08月30日 21:01:44 标签: python / csv / 数据 站长用Python写了一个可以提取csv任一列的代码,欢迎使用.Gith ...
- git-【一】概述安装
一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以 ...
- DIY自己的GIS程序(2)——局部刷新
绘制线过移动鼠标程中绘制临时线段防闪烁 参考OpenS-CAD想实现绘制线的功能.希望实现绘制线的过程,在移动线的时候没有闪烁和花屏.但是出现了问题,困扰了2天,前天熬的太晚,搞得现在精力都没有恢复. ...