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
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">
4.0.0
com.yiibai
SpringMVCResource
war
0.0.1-SNAPSHOT
SpringMVCResource Maven Webapp
http://maven.apache.org
junit
junit
3.8.1
test
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet
jstl
1.2
javax.servlet.jsp
jsp-api
2.2
provided
org.springframework
spring-core
4.1.4.RELEASE
org.springframework
spring-web
4.1.4.RELEASE
org.springframework
spring-webmvc
4.1.4.RELEASE
SpringMVCResource
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
4- 配置Spring
配置 web.xml
SpringContextListener 将读取配置文件参数 contextConfigLocation:
WEB-INF/web.xml
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">
Archetype Created Web Application
spring-mvc
org.springframework.web.servlet.DispatcherServlet
1
spring-mvc
/
contextConfigLocation
/WEB-INF/root-context.xml
org.springframework.web.context.ContextLoaderListener
配置Spring MVC:
WEB-INF/spring-mvc-servlet.xml
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">
classpath:ApplicationRB.properties
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
/WEB-INF/pages/
.jsp
appProperties
WEB-INF/root-context.xml
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">
配置静态资源:
资源映射 :
配置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
pageEncoding="UTF-8"%>
Spring MVC Resource examplehref="${pageContext.request.contextPath}/styles/common.css">
value="Click me!">
resourceBundleTest.jsp
pageEncoding="UTF-8"%>
Spring MVC Resource Bundle example${appProperties['text.loginPrompt']}
${appProperties['text.userName']}
${appProperties['text.password']}
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