(上篇:Struts2 - Rest(1))
6) 加入user-index.jsp到/WEB-INF/content中:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>User list</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/user/new">Add new user</a>
<hr/>
<s:actionmessage/><br/>
<table>
<thead>
<th>ID</th>
<th>User name</th>
<th>Age</th>
<th>View</th>
<th>Edit</th>
<th>Remove</th>
</thead>
<tbody>
<s:iterator value="model">
<tr>
<td><s:property value="id"/></td>
<td><s:property value="username"/></td>
<td><s:property value="age"/></td>
<td><s:a href="user/%{id}">View</s:a></td>
<td><s:a href="user/%{id}/edit">Edit</s:a></td>
<td><s:a href="user/%{id}/destroy">Remove</s:a></td>
</tr>
</s:iterator>
</tbody>
</table>
</body>
</html>
7) 加入user-show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/user">Back to home</a><hr/>
User ID:<s:property value="id"/><br/>
User Name:<s:property value="username"/><br/>
User Age:<s:property value="age"/><br/>
</body>
</html>
8) 加入user-editNew.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>Add new user</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/user">Back to home</a><hr/>
<s:form method="POST" action="%{contextPath}/user">
User Name:<s:textfield name="username"></s:textfield><br/>
User Age:<s:textfield name="age"></s:textfield><br/>
<s:submit value="Submit"></s:submit> <s:reset value="Reset"></s:reset>
</s:form>
</body>
</html>
9) 加入user-edit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>User edit</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/user">Back to home</a><hr/>
<s:form method="POST" action="%{contextPath}/user/%{id}">
<s:hidden name="_method" value="put"></s:hidden>
User ID:<s:textfield name="id" disabled="true"></s:textfield><br/>
User Name:<s:textfield name="username" disabled="false"></s:textfield><br/>
User Age:<s:textfield name="age" disabled="false"></s:textfield><br/>
<s:submit value="Submit"></s:submit> <s:reset value="Reset"></s:reset>
</s:form>
</body>
</html>
注意这里有一个hidden input,这个有个name="_method" value="put"的属性,意思是在这个form的提交是以PUT形式提交。这个不能少。不然rest认不出这是PUT提交。
完成后的页面例子:
Eclipse项目结构:
结束总结:
1) 这个例子使用了ModelDriven action来做Controller的model,具体可以参考下这个小文:
http://www.cnblogs.com/shihujiang/archive/2012/06/30/2571050.html
2) 事实上,rest插件可以生成的转向是一个jsp,在url的action中加入.xml或.json,将会出现对应的xml或json,比如:
http://localhost:8080/TestWeb/user.xml
http://localhost:8080/TestWeb/user.json
更多的内容可参考官方文档说明:
http://struts.apache.org/development/2.x/docs/rest-plugin.html