• 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奋斗的小蘑菇
  • 相关阅读:
    33. 搜索旋转排序数组
    54. 螺旋矩阵
    46. 全排列
    120. 三角形最小路径和
    338. 比特位计数
    746. 使用最小花费爬楼梯
    spring boot的一些常用注解
    SSM整合Dubbo案例
    一些面试题
    Spring Aop和Spring Ioc(二)
  • 原文地址:https://www.cnblogs.com/liyuchen/p/4871922.html
Copyright © 2020-2023  润新知