ITeye博客

【张冰Struts2学习笔记】0202_Struts2的三种接收用户输入的方法

CoderDream · 2010-11-17 12:32 · Struts2 · 企业架构 · Markdown

Struts2的三种接收用户输入的方法

 

1.      利用 action 类的属性接收用户输入

这种方式最简单,在action 中创建需要的属性,然后添加getter 、setter 方法:

代码清单1 : Login1Action.java

 

package com.coderdream.action;
 
public class Login1Action {
      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() {
           if ("aaa" .equalsIgnoreCase(username .trim())
                      && "123" .equalsIgnoreCase(password .trim())) {
                 return "success" ;
           } else {
                 return "failure" ;
           }
      }
 
} 

 

 

在jsp 中调用(输入):

代码清单 2 : login.jsp 片段

 

1 、利用 action 类的属性接收用户输入:
< br />
< form action =" <%= path%> /login1.action" method ="POST" >
      姓名:
      < input type ="text" name ="username " >
      < br />
      密码:
      < input type ="text" name ="password " >
      < br />
      < input type ="submit" name =" 登录 " >
      < br />
</ form >  

在jsp 中调用(输出):

代码清单 3 : login1Suc.jsp 片段

欢迎 < s:property value ="username " /> ,登录成功! 

 

 

2.      利用领域对象( VO )接收用户输入

先创建VO 对象,在对象中创建需要的属性,然后添加getter 、setter 方法:

代码清单 4 : User.java

 

package com.coderdream.vo;
 
public class User {
      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;
      }
} 

 

 

在action 中创建User 对象,并添加该对象的getter 、setter 方法:

代码清单 5 : Login2Action.java

 

package com.coderdream.action;
import com.coderdream.vo.User;
import com.opensymphony.xwork2.Action;
public class Login2Action implements Action {
      private User user ;
      public User getUser() {
           return user ;
      }
      public void setUser(User user) {
           this .user = user;
      }
      public String execute() {
           if ("aaa" .equalsIgnoreCase(user .getUsername().trim())
                      && "123" .equalsIgnoreCase(user .getPassword().trim())) {
                 return SUCCESS ;
           } else {
                 return INPUT ;
           }
      }
} 

 

 

在jsp 中调用,需要使用user 对象名(输入):

代码清单 6 : login.jsp 片段

 

2 、利用领域对象( VO )接收用户输入:
< br />
< form action =" <%= path%> /login2.action" method ="POST" >
      姓名:
      < input type ="text" name ="user.username " >
      < br />
      密码:
      < input type ="text" name ="user.password " >
      < br />
      < input type ="submit" name =" 登录 " >
      < br />
</ form > 

 

在jsp 中调用,需要使用user 对象名(输出):

代码清单 7 : login1Suc.jsp 片段

欢迎 < s:property value ="user.username " /> ,登录成功! 

 

3.      使用 ModelDriven 模式接收用户输入

使用这种方式时,需要实现下面的接口:

 

com.opensymphony.xwork2

Interface ModelDriven <T>

public interface ModelDriven<T>ModelDriven Actions provide a model object to be pushed onto the ValueStack in addition to the Action itself, allowing a FormBean type approach like Struts. 

 

然后在action 中创建需要的模型,最后实现getModel() 方法:

代码清单 8 : Login3Action.java

 

package com.coderdream.action;
 
import com.coderdream.vo.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
 
public class Login3Action extends ActionSupport implements ModelDriven<User> {
 
      private User user = new User();
 
      public User getModel() {
           return user ;
      }
 
      public String execute() {
           if ("aaa" .equalsIgnoreCase(user .getUsername().trim())
                      && "123" .equalsIgnoreCase(user .getPassword().trim())) {
                 return SUCCESS ;
           } else {
                 return INPUT ;
           }
      }
 
} 

 

在jsp 中调用,不需要使用User 对象,直接通过属性名获取(输入):

代码清单 9 : login.jsp 片段

 

3 、使用 ModelDriven 模式接收用户输入:
< br />
< form action =" <%= path%> /login3.action" method ="POST" >
      姓名:
      < input type ="text" name ="username " >
      < br />
      密码:
      < input type ="text" name ="password " >
      < br />
      < input type ="submit" name =" 登录 " >
      < br />
</ form > 

 

在jsp 中调用,不需要使用User 对象,直接通过属性名获取(输出):

代码清单 10 : login1Suc.jsp 片段

欢迎 < s:property value ="username " /> ,登录成功! 

 

4.      三种方式小结

     **Java代码** **jsp代码** **备注** **1** **利用action类的属性接收用户输入** 通过属性的getter/setter方法设置和使用属性 输入与输出:属性名  

<input type="text" name="username ">
<s:property value=" username " /> 属性很多时,action中的代码会非常多 2 利用领域对象(VO)接收用户输入 通过对象的getter/setter方法设置和使用对象 输入与输出:对象.属性名
<input type="text" name="user.username ">
<s:property value=" user.username " />   3 使用ModelDriven模式接收用户输入 在创建属性对象时就创建对象,然后通过getModel()方法使用对象 输入与输出:属性名
<input type="text" name="username ">
<s:property value=" username " /> 在Java和jsp代码调用很方便,不需要对象名

5.      源代码

见 附件。

 

我的联系方式: 85337464

我的博客: http://coderdream.iteye.com

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。