• 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奋斗的小蘑菇
  • 相关阅读:
    js 每个月有多少天算法
    js 树的操作
    画线
    程序员如何防止脑疲劳
    汉字求出拼音缩写
    datagird 多行外于编辑状态
    overflow: hidden 失效
    CSS 相对/绝对(relative/absolute)定位系列(三)
    display:inlineblock在Chrome与FF下导致的间隙
    css ul li 的使用及浏览器兼容问题
  • 原文地址:https://www.cnblogs.com/liyuchen/p/4871922.html
Copyright © 2020-2023  润新知