JavaWeb -- Struts2 模型驱动

1. 模型驱动

示例:

注册表单reg.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>reg.jsp</title>
	</head>
	<body>
		<s:actionerror/>
		<s:form namespace="/md" action="MdAction_reg" method="post" theme="xhtml" validate="true">
			<s:textfield name="name" label="UserName" />
			<s:textfield name="age" label="UserAge" />
			<s:submit />
		</s:form>
	</body>
</html>

用户显示list页面 userList.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>reg.jsp</title>
	</head>
	<body>
	<table border="1">
		<tr>
			<td>id</td><td>name</td><td>age</td><td>edit</td>
		</tr>
		<s:iterator value="userList">
			<tr>
				<td><s:property value="id" /></td>
				<td><s:property value="name" /></td>
				<td><s:property value="age" /></td>
				<td>
					<a href='<s:url action="MdAction_edit" namespace="/md"><s:param name="uid" value="id" /></s:url>'>edit</a>
				</td>
			</tr>
		</s:iterator>
	</table>
	</body>
</html>

处理action,模型驱动需要实现ModelDriven 接口,实现Preparable 接口,在modelDriven拦截器之前先运行 prepare拦截器。

package cn.itcast.struts2.modeldriven;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.validation.SkipValidation;

import cn.itcast.struts2.model.User;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

/**
 * MdAction:某型驱动
 */
public class MdAction extends ActionSupport implements ModelDriven<User>,Preparable {
	private static final long serialVersionUID = -6933309304624396640L;

	private String name;
	
	private Integer uid ;
	
	private User user = new User();

	private List<User> userList ;
	
	public String reg() {
		return "success";
	}

	@SkipValidation
	public String toRegView() {
		System.out.println("toRegView");
		return "regView";
	}
	
	/**
	 * 查询所有用户
	 */
	public String findAllUsers(){
		userList = new ArrayList<User>();
		User u = null ;
		for(int i= 0 ; i < 10 ; i ++){
			u = new User();
			u.setId(1 + i);
			u.setName("tom" + i);
			u.setAge(20 + i);
			userList.add(u);
		}
		return "userListView";
	}
	
	public String edit(){
		return "editView" ;
	}

	//
	public User getModel() {
		return user;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public Integer getUid() {
		return uid;
	}

	public void setUid(Integer uid) {
		this.uid = uid;
	}

	/**
	 * 该方法在getModel之前运行,在modelDriven拦截器之前先运行
	 */
	public void prepareDoEdit() throws Exception {
		//
		User u = new User();
		u.setId(uid);
		u.setName("jerry");
		u.setAge(30);
		user = u ;
	}

	public void prepare() throws Exception {
	}
}

struts配置文件md.xml, 需要添加paramsPrepareParamsStack, 在prepare之前填充参数。

<?xml version="1.0"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
    <package name="MdPkg" namespace="/md" extends="struts-default">
        <action name="MdAction_*" class="cn.itcast.struts2.modeldriven.MdAction" method="{1}">
            <result name="success">/md/reg.jsp</result>
            <result name="regView">/md/reg.jsp</result>
            <result name="editView">/md/edit.jsp</result>
            <result name="userListView">/md/userList.jsp</result>
            <interceptor-ref name="paramsPrepareParamsStack" />
        </action>
    </package>
</struts>



 
posted @ 2014-01-20 16:55  今晚打酱油_  阅读(335)  评论(0编辑  收藏  举报