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;
public class UserAction extends ActionSupport {
//模型驱动需要的模型实例引用
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String add() {
System.out.println("name:" + user.getName());
System.out.println("password:" + user.getPassword());
return "add";
}
}
表单页面:
<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>
</action>
</package>
</struts>
转发页面:
<body>
用户名:<s:property value="user.name" /><br>
密码:<s:property value="user.password" />
</body>
运行截图: