zoukankan      html  css  js  c++  java
  • Struts2中表单与Action传递数据三种方式

    1.       Action中的属性与表单中的属性一致就可以

    JSP中的表单

    <form action="login.action" method="post">

        用户名:<input type="text" name="username"/>    <br/>

        密码:  <input type="password" name="password" /> <br/>

        <input type="submit" value="登陆" />

    </form>

     

    Action中的属性                                                        

    public class LoginAction extends ActionSupport {

        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;

        }

       

        public String execute(){

           ifusername.equalsIgnoreCase("aaa")&&password.equals("aaaaaa")){

               return SUCCESS;

           }

           else{

               return ERROR;

           }

        }

    }

    2.       使用一个VO类

    在表单中提交的属性名改为user.username

    <form action="login.action" method="post">

           用户名:<input type="text" name="user.username"/>  <br/>

           密码:  <input type="password" name="user.password" /> <br/>

           <input type="submit" value="登陆" />

        </form>

    LoginAction中的属性改为user

    public class LoginAction extends ActionSupport{

        private User user;

       

        public User getUser() {

           return user;

        }

        public void setUser(User user) {

           this.user = user;

        }

     

        public String execute(){

           ifuser.getUsername().equalsIgnoreCase("aaa")&&user.getPassword().equals("aaaaaa")){

               return SUCCESS;

           }

           else{

               return ERROR;

           }

        }

    }

     

     

    3.       使用Struts2中的ModelDriven数据模式

    Action类要实现一个泛型接口,前台表单与1相同

    public class LoginAction extends ActionSupport implements ModelDriven<User> {

        private User user = new User();

       

        public String execute(){

           ifuser.getUsername().equalsIgnoreCase("aaa")&&user.getPassword().equals("aaaaaa")){

               return SUCCESS;

           }

           else{

               return ERROR;

           }

        }

     

        public User getModel() {

           return user;

        }

    }

  • 相关阅读:
    Python3 sorted() 函数
    [Python网络编程]一个简单的TCP时间服务器
    [爬虫]统计豆瓣读书中每个标签下的前两百本书
    [leetcode]39. Combination Sum
    [leetcode]18. 4Sum
    [leetcode DP]72. Edit Distance
    [leetcode DP]120. Triangle
    [leetcode DP]91. Decode Ways
    [leetcode DP]70. Climbing Stairs
    [leetcode DP]64. Minimum Path Sum
  • 原文地址:https://www.cnblogs.com/soundcode/p/6366178.html
Copyright © 2011-2022 走看看