一、Field-Driven(属性驱动)Action,Action拥有自己的属性,这些属性一般是Java的基本类型。表单字段直接和Action的属性 对应。
二、实现了modelDriven接口可以在action中直接获得例如User对象,它会将Object getModel()取得的User放到ValueStack中。可以理解为将这个User的属性追加到Action中。它主要是作用是实现类似 Struts的FormBean功能。
action实现com.opensymphony.xwork2.ModelDriven接口,ModelDriven可以直接操作应用程序中的领域对象,允许在web层和业务层使用相同的对象。
ModelDriven接口只有一个方法
public Object getModel() {
return null;
}
三、Preparable
prepare方法由PrepareInterceptor拦截器调用执行。
流程:
1. params拦截器首先给action中的相关参数赋值,如id
2. prepare拦截器执行prepare方法,prepare方法中会根据参数,如id,去调用业务逻辑,设置model对象 3. modelDriven拦截器将model对象压入value stack,这里的model对象就是在prepare中创建的 4. params拦截器再将参数赋值给model对象 5. action的业务逻辑执行 依据此stack
综合案例说明:
User.java
package model;
public class User {
private int id;
private String name;
private String pass;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
package action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import model.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
public class LoginAction extends ActionSupport implements ModelDriven<User>,
Preparable {
/**
*
*/
private static final long serialVersionUID = 1L;
private User user = new User();
private int id;
public String Login() {
System.out.println("Login==================");
if (user.getName().equals("123") && user.getPass().equals("csdn")) {
System.out.println(user.getId() + "@@@@");
System.out.println(user.getName() + "@@@@");
return SUCCESS;
}
return INPUT;
}
public String add() {
return SUCCESS;
}
public String delete() {
return SUCCESS;
}
public String index() {
return SUCCESS;
}
@Override
public User getModel() {
System.out.println("getModel==================");
System.out.println(user.getId() + "????");
System.out.println(user.getName() + "????");
return user;
}
@Override
public void prepare() throws Exception {
System.out.println("prepare==================");
HttpServletRequest request = ServletActionContext.getRequest();
String id = request.getParameter("id").toString();
System.out.println(id + "&&&&&&&&&");
user.setId(1234);
user.setName("22222");
System.out.println(user.getId() + "****");
System.out.println(user.getName() + "****");
System.out.println("!!!!!!!");
}
/*
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
*/
}
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'login.jsp' starting page</title>
</head>
<body>
<!-- struts2标签 -->
<s:form action="/hello/login" method="post">
<s:textfield name="id" label="编号" />
<s:textfield name="name" label="用户名" />
<s:password name="pass" label="密码 " />
<s:submit />
</s:form>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="hello" namespace="/hello" extends="struts-default">
<action name="login" class="action.LoginAction" method="Login">
<result name="success">
/index.jsp
</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>