首页 > 其他 > 详细

Struts2中Action接收参数的四种形式

时间:2015-10-12 16:56:37      阅读:229      评论:0      收藏:0      [点我收藏+]

 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
               < form action= "LoginAction" method = "post">
               < input type= "text" name = "username">< br />
               < input type= "password" name = "userpwd">< br />
               < input type= "submit" value = "提交">
               </ form>
          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后可用此方法进行传值。

Struts2中Action接收参数的四种形式

原文:http://www.cnblogs.com/liyuchen/p/4871922.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!