zoukankan      html  css  js  c++  java
  • Struts2中Action接收参数的四种形式

     1.Struts2的Action接收参数的三种形式。

         a. 使用Action的属性接收(直接在action中利用get方法来接收参数):    
                  login.jsp
                  
     < form action= "LoginAction" method = "post">
                   < input type= "text" name = "username">< br />
                   < input type= "password" name = "userpwd">< br />
                   < input type= "submit" value = "提交">
     
             LoginAction.java
            
      public class LoginAction extends ActionSupport {
                     public String username ;
                public String userpwd ;
     
                public String getUsername() {
                     return username ;
                 }
     
                public void setUsername(String username) {
                     this .username = username ;
                 }
     
                 public String getUserpwd() {
                     return userpwd ;
                 }
     
                 public void setUserpwd(String userpwd) {
                     this .userpwd = userpwd ;
                 }
              public String execute(){
                  String result= "input" ;
                   if (getUsername().equals("lichenyu" )&&getUserpwd().equals( "123456")){
    //                   result="input";
                  } else {
                          result= "error" ;
                  }
                   return result ;
     
               }
          }
              
        b.使用 DomainModel接收参数:
              login.jsp
                   
    < form action= "LoginAction" method = "post">
                   < input type= "text" name = "user.username">< br />
                   < input type= "password" name = "user.userpwd">< br />
                   < input type= "submit" value = "提交">
                   </ form>
               新建User.java
                  
     public class User {
                 public String username ;
               public String userpwd ;
     
                 public String getUsername() {
                   return username ;
                }
                 public void setUsername(String username) {
                        this .username = username ;
                }
                 public String getUserpwd() {
                        return userpwd ;
                }
                 public void setUserpwd(String userpwd) {
                   this .userpwd = userpwd ;
                }
           
                        }
                LoginAction.java(此处必须添加user的set和get方法)
                       
     public class LoginAction extends ActionSupport {
                                private User user ;
           
                                public User getUser() {
                                       return user ;
                                         }
     
                           public void setUser(User user) {
                                  this .user = user ;
                                    }
     
                           public String execute(){
                            String result= "input" ;
                            System. out .println(user .username );
                                    return result ;
                          }
                             }
        c.使用 ModelDriven接收参数:
              login.jsp
                   
     public class LoginAction extends ActionSupport {
                                private User user ;
           
                                public User getUser() {
                                       return user ;
                                         }
     
                           public void setUser(User user) {
                                  this .user = user ;
                                    }
     
                           public String execute(){
                            String result= "input" ;
                            System. out .println(user .username );
                                    return result ;
                          }
                             }
              User.java同方法b中
              LoginAction.java
                   
    public class LoginAction extends ActionSupport implements ModelDriven<User> {
            User user= new User();
            public String execute(){
                  String result= "input" ;
                  System. out .println(user .username );
                  System. out .println(user .getUsername());
                   return result ;
                     }
            @Override
            public User getModel() {
                   // TODO Auto-generated method stub
                   return user ;
                     }
                   }
        d.使用request来接收参数:
        使Action支持request后可用此方法进行传值。
      
        HttpServletRequest request=ServletActionContext.getRequest();
        String username=request.getParameter("username");
     
    **********************************************************************************************************************
      如若转载请注明出处By奋斗的小蘑菇
    by奋斗的小蘑菇
  • 相关阅读:
    HDU 2852 KiKi's K-Number (主席树)
    HDU 2089 不要62
    Light oj 1140 How Many Zeroes?
    Bless You Autocorrect!
    HDU 6201 transaction transaction transaction
    HDU1561 The more ,The better (树形背包Dp)
    CodeForces 607B zuma
    POJ 1651 Mulitiplication Puzzle
    CSUOJ 1952 合并石子
    Uva 1599 Ideal path
  • 原文地址:https://www.cnblogs.com/liyuchen/p/4871922.html
Copyright © 2011-2022 走看看