欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2属性驱动与模型驱动

程序员文章站 2022-07-12 16:14:00
...
1. struts2属性驱动与模型驱动

user.java

package com.andrew.model;
public class User {
    private String userName;
    private String password;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

UserService.java

package com.andrew.service;
import com.andrew.model.User;
public class UserService {
    public boolean login(User user) {
        if ("andrew".equals(user.getUserName()) && "123456".equals(user.getPassword())) {
            return true;
        } else {
            return false;
        }
    }
}

success.jsp

登录成功!

error.jsp

登录失败!


1) 属性驱动(FieldDriven)

1.1) 基本数据类型属性

UserAction.java

package com.andrew.action;
import com.andrew.model.User;
import com.andrew.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
    private UserService userService = new UserService();
    private String userName;
    private String password;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String execute() throws Exception {
        System.out.println("执行了UserAction的默认方法");
        User user = new User();
        user.setUserName(userName);
        user.setPassword(password);
        if (userService.login(user)) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }
}

struts.xml

<action name="user" class="com.andrew.action.UserAction">
      <result name="success">success.jsp</result>
      <result name="error">error.jsp</result>
</action>

login.jsp

<form action="user" method="post">
    用户名:<input type="text" name="userName"/>
    密码:<input type="text" name="password"/>
    <input type="submit" value="登录"/>
</form>

http://localhost:8080/HeadFirstStruts2Chap02/login.jsp
abc 123 submit
运行结果:
登录失败! 
控制台:
执行了UserAction的默认方法
andrew 123456 submit
运行结果:
登录成功!
控制台:
执行了UserAction的默认方法


1.2) JavaBean类型属性

UserAction2.java

package com.andrew.action;
import com.andrew.model.User;
import com.andrew.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction2 extends ActionSupport {
    private UserService userService = new UserService();
    private User user;
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    @Override
    public String execute() throws Exception {
        System.out.println("执行了UserAction2的默认方法");
        if (userService.login(user)) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }
}

struts.xml

<action name="user2" class="com.andrew.action.UserAction2">
      <result name="success">success.jsp</result>
      <result name="error">error.jsp</result>
</action>

login2.jsp

<form action="user2" method="post">
    用户名:<input type="text" name="user.userName"/>
    密码:<input type="text" name="user.password"/>
    <input type="submit" value="登录"/>
</form>

http://localhost:8080/HeadFirstStruts2Chap02/login2.jsp
abc 123 submit
运行结果:
登录失败! 
控制台:
执行了UserAction2的默认方法
andrew 123456 submit
运行结果:
登录成功!
控制台:
执行了UserAction2的默认方法


2) 模型驱动(ModelDriven)

现实接口 implements ModelDriven<xxx>

UserAction3.java

package com.andrew.action;
import com.andrew.model.User;
import com.andrew.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class UserAction3 extends ActionSupport implements ModelDriven<User> {
    private UserService userService = new UserService();
    private User user = new User();
    @Override
    public String execute() throws Exception {
        System.out.println("执行了UserAction3的默认方法");
        if (userService.login(user)) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }
    public User getModel() {
        return user;
    }
}

struts.xml

<action name="user3" class="com.andrew.action.UserAction3">
      <result name="success">success.jsp</result>
      <result name="error">error.jsp</result>
</action>

login3.jsp

<form action="user3" method="post">
    用户名:<input type="text" name="userName"/>
    密码:<input type="text" name="password"/>
    <input type="submit" value="登录"/>
</form>

http://localhost:8080/HeadFirstStruts2Chap02/login3.jsp
abc 123 submit
运行结果:
登录失败! 
控制台:
执行了UserAction3的默认方法
andrew 123456 submit
运行结果:
登录成功!
控制台:
执行了UserAction3的默认方法
相关标签: Java struts2