1.spring3开始jar包不再包含全能的spring.jar了,而是分成20多个jar包了,配置可以按需引入了
2.引入dist目录下除了下面三个其余所有包
org.springframework.web.struts-3.1.0.RC1.jar
org.springframework.spring-library-3.1.0.RC1.libd
org.springframework.web.portlet-3.1.0.RC1.jar
3.导入日志相关的一个jar包commons logging和一个名为com.springsource.org.aopalliance-1.0.0.jar
如果不引入部署项目tomcat会报如下错误:严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
4.web.xml配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringMVC-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>*.htm</url-pattern>
</servlet-mapping>
5.Spring应用上下文配置,在web.xml中我们配置了spring的servlet -----<servlet-name>SpringMVC</servlet-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringMVC-servlet.xml</param-value>
</context-param>
用于开启基于注解的springMVC功能那么我们需要在跟web.xml同一目录下新建一个SpringMVC-servlet.xml文件,内容为:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.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-3.0.xsd">
<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<!--
启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean
-->
<context:component-scan base-package="com.mvc.rest" />
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
6.其他
如果
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Spring3中js/css/jpg/gif等静态资源无法找到
那是因为<servlet-mapping><url-pattern>/</url-pattern>把所有的请求都交给spring去处理了,而所有available的请求url都是在Constroller里使用类似@RequestMapping(value = "/login/{user}", method = RequestMethod.GET)这样的注解配置的,这样的话对js/css/jpg/gif等静态资源的访问就会得不到。
解决方法:在web.xml里添加如下的配置
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
解决方法2:在spring的配置文件中添加如下一行:
<mvc:default-servlet-handler/>
注意,需要是spring3.0.5以上版本
<%@ page language="java" session="false"
contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<jsp:useBean id="personList" scope="request"
type="java.util.List<com.mvc.rest.model.Person>" />
<html>
<head>
<title>Adobocode : Sample Spring MVC using JSTL iteration</title>
</head>
<body>
<h2> Adobocode : Person List 明明用了JSTL啊!</h2>
<table border="1">
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
<th>Address</th>
</tr>
<c:forEach var="p" items="${personList}">
<tr>
<td><c:url var="editUrl" value="personForm.htm">
<c:param name="personId" value="${p.id}" />
</c:url> <a href='<c:out value="${editUrl}"/>'>${p.id}</a></td>
<td>${p.name}</td>
<td>${p.age}</td>
<td>${p.address}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
personForm.jsp
<%@ page language="java" session="false"
contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Adobocode : Sample Spring MVC using Forms</title>
</head>
<body>
<h2>Adobocode : Person Form</h2>
<form:form modelAttribute="person">
<form:hidden path="id" />
<fieldset>
<table>
<tr>
<td>Name名字!</td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td>Age年龄!</td>
<td><form:input path="age" /></td>
</tr>
<tr>
<td>Address地址!</td>
<td><form:input path="address" /></td>
</tr>
<tr>
<td>咆哮体!!!</td>
<td><input type="submit" id="save" name="_eventId_save"
value="Save" /> <input type="submit" onClick="history.go(-1);"
name="_eventId_cancel" value="Cancel" /></td>
</tr>
</table>
</fieldset>
</form:form>
</body>
</html>
默认index.jsp内容
<%@ page language="java" session="false" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Adobocode : Sample Spring MVC</title>
</head>
<body>
<h2>Adobocode : 世界你好</h2>
<br/>
<a href="<c:url value="personDisplay.htm"/>">人员列表</a>
</body>
</html>
8.建立三个包和类文件
com.mvc.rest.controller
PersonDisplay .java
/**
* @author 作者 :
* @version 创建时间:2011-10-27 下午04:26:09
* 类说明
*/
@Controller
public class PersonDisplay {
private final DummyService dummyService;
@Autowired
public PersonDisplay(DummyService dummyService) {
this.dummyService = dummyService;
}
@RequestMapping("/personDisplay.htm")
public ModelMap defaultHandler() {
return new ModelMap("personList", this.dummyService.getDummyList());
}
}
PersonForm.java
@Controller
@RequestMapping("/personForm.htm")
@SessionAttributes("person")
public class PersonForm {
private final DummyService dummyService;
@Autowired
public PersonForm(DummyService dummyService) {
this.dummyService = dummyService;
}
@RequestMapping(method = RequestMethod.GET)
public String setupForm(@RequestParam("personId") int id, ModelMap model) {
Person person = this.dummyService.retrievePerson(id);
model.addAttribute("person", person);
return "personForm";
}
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("person") Person person,
BindingResult result, SessionStatus status) {
this.dummyService.savePerson(person);
status.setComplete();
return "redirect:personDisplay.htm";
}
}
com.mvc.rest.service
@Service
public class DummyService {
/**
* This method supposed to be returning a Collection of Person objects from
* a DAO layer For this tutorial, let us just hard-code this List of Person
* objects
*/
public List<Person> getDummyList() {
List<Person> list = new ArrayList<Person>();
Person p1 = new Person();
p1.setId(12345);
p1.setName("Paul");
p1.setAge(27);
p1.setAddress("Dalaguete, Cebu");
Person p2 = new Person();
p2.setId(54321);
p2.setName("Sydney");
p2.setAge(25);
p2.setAddress("Cebu City");
list.add(p1);
list.add(p2);
return list;
}
/**
* This method supposed to be returning Person object from a DAO layer For
* this tutorial, let us just hard-code the Person instance
*/
public Person retrievePerson(int id) {
Person person = new Person();
person.setId(56789);
person.setName("Nikki");
person.setAge(63);
person.setAddress("Dalaguete, Cebu");
return person;
}
/**
* This method supposed to be persisting the passed Person object For this
* tutorial, let us include the persisting DAO layer and assume the method
* successful saved or updated the Person object
*/
public void savePerson(Person person) {
System.out.println("\n\nSaving" + person);
}
}
com.mvc.rest.model
public class Person {
private int id;
private String name;
private int age;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("\nname : " + this.name);
sb.append("\nage : " + this.age);
sb.append("\naddress : " + this.address);
return sb.toString();
}
}