1- 介绍

这篇教程文章是基于:
  • Spring 4 MVC

2- 创建一个项目

  • File/New/Other..



输入:
  • Group ID: com.yiibai
  • Artifact ID: SpringMVCResource
  • Package: com.yiibai.springmvcresource
 


项目被创建以后如下:

 

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

注意:

Eclipse 4.4(Luna)在创建 Maven 项目结构时可能是有错误的。需要修复。
 

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

SpringContextListener 将读取配置文件参数 contextConfigLocation:
  • 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>
配置Spring MVC:
  • 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 />
资源映射 <mvc:resources mapping ... >:
 

配置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配置静态资源和资源包教程的更多相关文章

  1. Spring MVC配置静态资源和资源包

    Spring MVC配置静态资源和资源包 本例映射:css目录: pom.xml <properties> <spring.version>4.3.5.RELEASE</ ...

  2. Spring MVC配置静态资源的正常访问

    SpringMVC如果过滤器过滤范围配置了/或者/*,那么框架会过滤所有请求,包括自己写的请求和静态资源请求,这样静态资源就不能正常加载,包括js文件.css文件.图片资源访问的时候都会出现404页面 ...

  3. Spring MVC 处理静态资源文件

    摘要: 三个方案: 1.方案一:激活Tomcat的defaultServlet来处理静态文件 2.方案二: 在spring3.0.4以后版本提供了mvc:resources (需要配置annotati ...

  4. 【Spring学习笔记-MVC-14】Spring MVC对静态资源的访问

    作者:ssslinppp       参考链接: http://www.cnblogs.com/luxh/archive/2013/03/14/2959207.html  http://www.cnb ...

  5. spring mvc对静态资源的访问

    如果我们的项目使用的是springmvc,在web.xml中会有一段这的配置. <servlet> <servlet-name>springMvc</servlet-na ...

  6. 【Spring学习笔记-MVC-18.1】Spring MVC实现RESTful风格-同一资源,多种展现:xml-json-html

    概要 要实现Restful风格,主要有两个方面要讲解,如下: 1. 同一个资源,如果需要返回不同的形式,如:json.xml等: 不推荐的做法: /user/getUserJson /user/get ...

  7. Spring mvc 增加静态资源配置后访问不了注解配置的controller

    spring mvc 增加静态资源访问配置. 例如: <!-- 静态资源映射 --> <mvc:resources location="/static/" map ...

  8. spring mvc: 多解析器映射(资源绑定视图解析器 + 内部资源[普通模式/]视图解析器)

    spring mvc: 多解析器映射(资源绑定视图解析器 + 内部资源[普通模式/]视图解析器) 资源绑定视图解析器 + 内部资源(普通模式)视图解析器 并存方式 内部资源视图解析器: http:// ...

  9. 最小可用 Spring MVC 配置

    [最小可用 Spring MVC 配置] 1.导入有概率用到的JAR包, -> pom.xml 的更佳实践 - 1.0 <- <project xmlns="http:// ...

随机推荐

  1. 解决IIS服务和用户上传的文件分别部署在不同的电脑上时,解决权限的问题

    为解决IIS服务和用户上传的文件分别部署在不同的电脑上时,解决权限的问题. 定义: A:iis服务器 B:文件服务器 步骤: 1.在B上创建一个用户[uploaduser](并设置密码) 2.给B上的 ...

  2. mac下更新自带的PHP版本到5.6或7.0

    下载和安装PHP 5.6 打开终端并且运行如下命令: curl -s http://php-osx.liip.ch/install.sh | bash -s 5.6 然后,PHP 5.6的版本会被安装 ...

  3. 前端:微信支付和支付宝支付在pc端和h5页面中的应用

    1:h5微信支付 使用的是https://pay.weixin.qq.com/wiki/doc/api/index.html  中的 (1):公司需要首先要配置公众号微信支付地址和测试白名单(支付的时 ...

  4. django admin中文输入编码错误

    修改models里面的str方法,改为unicode class Category(models.Model): name = models.CharField(max_length=20, verb ...

  5. 【Git】Git hangs while unpacking objects (Windows)

    Git hangs while unpacking objects (Windows) 14 Oct 2014 I'm not sure if this is because we're behind ...

  6. Shell实现循环执行curl向Solr导入json文件

    #!/bin/bash for file in ./文件夹名/* do echo $file curl "http://IP:8983/solr/集合名/update?commit=true ...

  7. ExtentReports 结合 TestNg 生成自动化 html 报告 (支持多 suite)

    转载:https://testerhome.com/topics/8134 重要说明:报告监听器源码修复一些bug,不再此处更新代码,最新代码可以到github查看最新报告监听器源码 前几天分享了ht ...

  8. node - 导包机制

    在学node js的时候,经常各种导包 let http = require('http'); 然后它的运行机制: 1. 查找当前目录下面的node_modules 2. 全局查找(首先添加到path ...

  9. 炫酷的sublimeText开发工具 快捷键总结

     sublimeText3绝对是写前端程序的利器.占内存小.启动快.代码提示功能齐全,界面酷炫并且再也不怕断电.了 网上搜罗一些快捷键总结一下.先来张图: (事实上sublimeText3也能够写 ...

  10. Unity Shader 效果学习

    Unity上对于图像的处理,假设单纯使用代码.那么非常遗憾,程序基本会跑死,毕竟是直接对像素的操作,读取写入都是比較耗费CPU和内存的. 所以.这次由于项目须要想实现类似哈哈镜的效果.想来想去,还是认 ...