1 模型驱动 表单文本框的name值为action model实例名点属性名
User类(model):
package org.model;
public class User {
private String name;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//模型驱动所需属性
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
action类:
package org.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.model.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class UserAction extends ActionSupport implements
ModelDriven<User/*这里需要填写模型类*/> {
//模型驱动需要的模型实例引用
private User
user=new User();
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String add() {
return "add";
}
//实现模型驱动接口ModelDriven中的 getModel()返回User实例user
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
}
表单页面:
<body>
<form action="getuser" method="post">
用户名:<input type="text" name="user.name"><br>
密码:<input type="text" name="user.password">
<input type="submit" value="提交">
</form>
</body>
strut.xml
<struts>
<package name="default" extends="struts-default">
<action name="getuser" class="org.action.UserAction" method="add">
<result name="add">/success.jsp</result>
<interceptor-ref name="modelDriven"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>
转发页面:
<body></body>
运行截图:
原文地址:http://blog.csdn.net/u012871207/article/details/43227095