Spring MVC3构建Web应用第三步
在SampleSpringMVC/war/WEB-INF中建立文件SampleSpringMVC-servlet.xml,并将内容修改如下:
1
<?xml version="1.0" encoding="UTF-8"?>
2
3
<!-- Copyright : adobocode.com , 2010 -->
4
5
<beans xmlns="http://www.springframework.org/schema/beans"
6
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7
xmlns:p="http://www.springframework.org/schema/p"
8
xmlns:context="http://www.springframework.org/schema/context"
9
xsi:schemaLocation="
10
11
http://www.springframework.org/schema/beans
12
13
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
14
15
http://www.springframework.org/schema/context
16
17
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
18
19
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
20
21
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
22
23
<context:component-scan base-package="paul.sydney.controller"/>
24
25
<context:component-scan base-package="paul.sydney.service"/>
26
27
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
28
<property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
29
<property name="prefix"><value>/WEB-INF/jsp/</value></property>
30
<property name="suffix"><value>.jsp</value></property>
31
</bean>
32
33
</beans>
<?xml version="1.0" encoding="UTF-8"?> 2
3
<!-- Copyright : adobocode.com , 2010 -->4
5
<beans xmlns="http://www.springframework.org/schema/beans"6
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"7
xmlns:p="http://www.springframework.org/schema/p"8
xmlns:context="http://www.springframework.org/schema/context"9
xsi:schemaLocation=" 10
11
http://www.springframework.org/schema/beans 12
13
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 14
15
http://www.springframework.org/schema/context 16
17
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 18
19
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> 20
21
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> 22
23
<context:component-scan base-package="paul.sydney.controller"/> 24
25
<context:component-scan base-package="paul.sydney.service"/> 26
27
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 28
<property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property> 29
<property name="prefix"><value>/WEB-INF/jsp/</value></property> 30
<property name="suffix"><value>.jsp</value></property> 31
</bean> 32
33
</beans>
这里,我们利用了spring 3 mvc的新特性,自动扫描,通过context:component-scan base-package,设置了paul.syney.controller和paul.sydney.service两个包下的文件只要使用spring 3的标准注释都可以被扫描到。
在SampleSpringMVC/war/WEB-INF下建立目录jsp;
在SampleSpringMVC/war/WEB-INF下建立目录classes;
在SampleSpringMVC/war/WEB-INF/jsp下建立一个新的jsp文件叫personDisplay,并写入如下代码:
1
<!-- Copyright : adobocode.com , 2010 -->
2
3
<%@ page language="java" session="false"
4
contentType="text/html; charset=UTF-8"%>
5
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
6
7
<jsp:useBean id="personList" scope="request" type="java.util.List" />
8
<html>
9
<head>
10
<title>Adobocode : Sample Spring MVC using JSTL iteration</title>
11
</head>
12
<body>
13
<h2>Adobocode : Person List</h2>
14
<table border="1">
15
<tr>
16
<th>Id</th>
17
<th>Name</th>
18
<th>Age</th>
19
<th>Address</th>
20
</tr>
21
<c:forEach var="p" items="${personList}">
22
<tr>
23
<td>
24
<c:url var="editUrl" value="personForm.htm">
25
<c:param name="personId" value="${p.id}" />
26
</c:url>
27
<a href='<c:out value="${editUrl}"/>'>${p.id}</a>
28
</td>
29
<td>${p.name}</td>
30
<td>${p.age}</td>
31
<td>${p.address}</td>
32
</tr>
33
</c:forEach>
34
</table>
35
</body>
<!-- Copyright : adobocode.com , 2010 -->2
3
<%@ page language="java" session="false" 4
contentType="text/html; charset=UTF-8"%> 5
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 6
7
<jsp:useBean id="personList" scope="request" type="java.util.List" /> 8
<html> 9
<head> 10
<title>Adobocode : Sample Spring MVC using JSTL iteration</title> 11
</head> 12
<body> 13
<h2>Adobocode : Person List</h2> 14
<table border="1"> 15
<tr> 16
<th>Id</th> 17
<th>Name</th> 18
<th>Age</th> 19
<th>Address</th> 20
</tr> 21
<c:forEach var="p" items="${personList}"> 22
<tr> 23
<td> 24
<c:url var="editUrl" value="personForm.htm"> 25
<c:param name="personId" value="${p.id}" /> 26
</c:url> 27
<a href='<c:out value="${editUrl}"/>'>${p.id}</a> 28
</td> 29
<td>${p.name}</td> 30
<td>${p.age}</td> 31
<td>${p.address}</td> 32
</tr> 33
</c:forEach> 34
</table> 35
</body>
在SampleSpringMVC/war/WEB-INF/jsp下建立一个新的jsp文件叫personForm,修改代码如下:
1
<!-- Copyright : adobocode.com , 2010 -->
2
3
<%@ page language="java" session="false" contentType="text/html; charset=UTF-8"%>
4
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
5
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
6
<html>
7
<head>
8
<title>Adobocode : Sample Spring MVC using Forms</title>
9
</head>
10
11
<body>
12
<h2>Adobocode : Person Form</h2>
13
<form:form modelAttribute="person">
14
<form:hidden path="id" />
15
<fieldset>
16
<table>
17
<tr>
18
<td>Name</td>
19
<td><form:input path="name" /></td>
20
</tr>
21
<tr>
22
<td>Age</td>
23
<td><form:input path="age" /></td>
24
</tr>
25
<tr>
26
<td>Address</td>
27
<td><form:input path="address" /></td>
28
</tr>
29
<tr>
30
<td></td>
31
<td>
32
<input type="submit" id="save" name="_eventId_save" value="Save" />
33
<input type="submit" onClick="history.go(-1);" name="_eventId_cancel" value="Cancel" />
34
</td>
35
</tr>
36
</table>
37
</fieldset>
38
</form:form>
39
</body>
40
</html>
<!-- Copyright : adobocode.com , 2010 -->2
3
<%@ page language="java" session="false" contentType="text/html; charset=UTF-8"%> 4
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> 5
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 6
<html> 7
<head> 8
<title>Adobocode : Sample Spring MVC using Forms</title> 9
</head> 10
11
<body> 12
<h2>Adobocode : Person Form</h2> 13
<form:form modelAttribute="person"> 14
<form:hidden path="id" /> 15
<fieldset> 16
<table> 17
<tr> 18
<td>Name</td> 19
<td><form:input path="name" /></td> 20
</tr> 21
<tr> 22
<td>Age</td> 23
<td><form:input path="age" /></td> 24
</tr> 25
<tr> 26
<td>Address</td> 27
<td><form:input path="address" /></td> 28
</tr> 29
<tr> 30
<td></td> 31
<td> 32
<input type="submit" id="save" name="_eventId_save" value="Save" /> 33
<input type="submit" onClick="history.go(-1);" name="_eventId_cancel" value="Cancel" /> 34
</td> 35
</tr> 36
</table> 37
</fieldset> 38
</form:form> 39
</body> 40
</html>