struts2的处理方式和action实现方式
1.属性驱动
jsp页面:
<form action="register" method="post">
用户名:<input type="text" name="name" /><br>
密码:<input type="password" name="pwd" /><br>
年龄:<input type="text" name="age" /><br>
邮箱:<input type="text" name="email" /><br>
<input type="submit" value="提交" />
</form>
action代码:
public class Login extends ActionSupport{
private String name;
private String pwd;
@Override
public String execute() throws Exception {
if ("admin".equals(name)&&"123".equals(pwd)) {
return "success";
} else {
return "fail";
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
2.对驱动象
jsp页面:
<form action="register" method="post">
用户名:<input type="text" name="user.name" /><br>
密码:<input type="password" name="user.pwd" /><br>
年龄:<input type="text" name="user.age" /><br>
邮箱:<input type="text" name="user.email" /><br>
<input type="submit" value="提交" />
</form>
action类:
public class Login extends ActionSupport{
private User user; //注册
public String register(){
System.out.println(user);
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
userBean类:
public class User {
private String name;
private String pwd;
private int age;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
3.模式驱动
jsp页面:
<form action="register" method="post">
用户名:<input type="text" name="name" /><br>
密码:<input type="password" name="pwd" /><br>
年龄:<input type="text" name="age" /><br>
邮箱:<input type="text" name="email" /><br>
<input type="submit" value="提交" />
</form>
action:
import com.opensymphony.xwork2.ModelDriven;
public class Login extends ActionSupport{
public class UserDrivenAction implements ModelDriven<User>{
private User user = new User(); //注册
public String register(){
System.out.println(user);
return SUCCESS;
}
public User getModel() {
return user;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
}
uesrBean类:
public class User {
private String name;
private String pwd;
private int age;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
4.定义一个pojo类
好处:自定义一个普通的 java 类即可,不具有侵入型
public class PojoAction {
public String execute(){
System.out.println("pojo action");
return "success";
}
}
5.实现Action接口
好处:使得我们编写的代码更加规范
public class InterfaceAction implements Action{
public String execute() throws Exception {
System.out.println("interface action");
return SUCCESS;
}
}
6.继承ActionSupport类
好处:可以继承一些 ActionSuport 实现功能,如:验证;官方推荐使用
public class ExtendsAction extends ActionSupport{
@Override
public String execute() throws Exception {
System.out.println("extends action");
return SUCCESS;
}
}