你的位置:首页 > Java教程

[Java教程]Struts2(四)Action一接收参数


一、属性接收参数并输出

导入struts2的包,导入需要的包

和struts.

<??><!DOCTYPE struts PUBLIC  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  "http://struts.apache.org/dtds/struts-2.3.dtd"><struts><!--   <constant name="struts.enable.DynamicMethodInvocation" value="false" />  <constant name="struts.devMode" value="false" />  <package name="default" namespace="/" extends="struts-default">    <default-action-ref name="index" />    <global-results>      <result name="error">/error.jsp</result>    </global-results>    <global-exception-mappings>      <exception-mapping exception="java.lang.Exception" result="error"/>    </global-exception-mappings>    <action name="index">      <result type="redirectAction">        <param name="actionName">HelloWorld</param>        <param name="namespace">/example</param>      </result>    </action>  </package>  <include file="example.-->  <!-- Add packages here -->  <!-- 开发模式-->   <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="login" class="com.pb.web.action.LoginAction"> <result> /loginSuccess.jsp </result> </action> </package></struts>

二、实现登录名和密码的接收和输出

Action接收参数(属性方式)

package com.pb.web.action;import com.opensymphony.xwork2.ActionSupport;/* * 登录响应action */public class LoginAction extends ActionSupport {  private String userName;  private String passWord;  private static final long serialVersionUID = 1L;  @Override  public String execute() throws Exception {        return "success";  }  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;  }        }

web.

<??><web-app ="http://www.w3.org/2001/ ="http://java.sun.com/ xsi:schemaLocation="http://java.sun.com/ id="WebApp_ID" version="3.0"> <display-name>Struts2demo2</display-name> <welcome-file-list>  <welcome-file>index.html</welcome-file>  <welcome-file>index.htm</welcome-file>  <welcome-file>index.jsp</welcome-file>  <welcome-file>default.html</welcome-file>  <welcome-file>default.htm</welcome-file>  <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter>    <filter-name>struts2</filter-name>    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>struts2</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping></web-app>

登录页面和登录成功页面

<%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>登录页面</title></head><body><form action="login.action" method="post"><table><tr><td>用户名:</td><td><input type="text" name="userName" /></td></tr><tr><td>密码:</td><td><input type="password" name="passWord" /></td></tr><tr><td><input type="submit" value="登录" /></td><td><input type="reset" value="重置" /></td></tr></table></form></body></html>

<%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>  <!-- 导入struts标签 --> <%@taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>登录成功页面</title></head><body>欢迎您的到来!<s:property value="userName"/></body></html>

三、Action接收参数一(属性方式)

以上用的就是第一种方式表单参数名称必须在Action中有对应的setter和getter方法

解决乱码问题

修改struts.

<constant name="struts.i18n.encoding" value="相应的编码格式" />

页面显示:

  <!-- 导入struts标签 --> <%@taglib prefix="s" uri="/struts-tags" %>

应用场景:接收参数字段个数少的时候适合使用,比如:对于搜索应用接收用户输入的查询条件

接收参数字段个数多时,就可以使用JavaBean的方式来接收

四、Action接收参数二(JavaBean形式)

建立实体类

package com.pb.entity;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;  }    }

package com.pb.web.action;import com.opensymphony.xwork2.ActionSupport;import com.pb.entity.User;/* * 登录响应action */public class LoginAction2 extends ActionSupport {  private User user;  private static final long serialVersionUID = 1L;  @Override  public String execute() throws Exception {        return "success";  }  public User getUser() {    return user;  }  public void setUser(User user) {    this.user = user;  }}

struts.

<?DOCTYPE struts PUBLIC  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  "http://struts.apache.org/dtds/struts-2.3.dtd"><struts><!--   <constant name="struts.enable.DynamicMethodInvocation" value="false" />  <constant name="struts.devMode" value="false" />  <package name="default" namespace="/" extends="struts-default">    <default-action-ref name="index" />    <global-results>      <result name="error">/error.jsp</result>    </global-results>    <global-exception-mappings>      <exception-mapping exception="java.lang.Exception" result="error"/>    </global-exception-mappings>    <action name="index">      <result type="redirectAction">        <param name="actionName">HelloWorld</param>        <param name="namespace">/example</param>      </result>    </action>  </package>  <include file="example.package name="default" namespace="/" extends="struts-default"> <action name="login" class="com.pb.web.action.LoginAction"> <result> /loginSuccess.jsp </result> </action> <action name="login2" class="com.pb.web.action.LoginAction2"> <result> /loginSuccess2.jsp </result> </action> </package></struts>

登录页面

<%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>登录页面</title></head><body><form action="login2.action" method="post"><table><tr><td>用户名:</td><!--这里的name要和提交的地址中声明的实体类.属性来用 --><td><input type="text" name="user.userName" /></td></tr><tr><td>密码:</td><!--这里的name要和提交的地址中声明的实体类.属性来用 --><td><input type="password" name="user.passWord" /></td></tr><tr><td><input type="submit" value="登录" /></td><td><input type="reset" value="重置" /></td></tr></table></form></body></html>

登录成功页面

<%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>  <!-- 导入struts标签 --> <%@taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>登录成功页面</title></head><body><!-- 输出:提交的地址中声明的实体类.属性来用-->欢迎您的到来!<s:property value="user.userName"/></body></html>

 定义实体类,为实体添加属性及setter和getter方式

com.pb.entity.User

为Action添加实体属性以及setter和getter方法

User user

修改表单参数name为实体对象名.属性名 这里的对象名要与Action中声名的对象名一致

<input name="user.userName"/>

五、Action接收参数三(ModelDriven形式)

 定义实体类,为实体添加属性及setter和getter方式

com.pb.entity.User

创建Action实现接口ModelDriven接口,重写getModel();

Action中要提供JavaBean类型的属性,需要实例化,但不需要相应的setter和getter方法

Form表单项的name属性以及页页面取值通过<s:property value="属性名"/>的形式

实体类:

package com.pb.entity;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

package com.pb.web.action;import com.opensymphony.xwork2.ModelDriven;import com.pb.entity.User;/* * 登录响应action3 */public class LoginAction3 implements ModelDriven<Object> {    //实例化对象  private User user=new User();   //重写接口的方法  @Override  public User getModel() {    return user;  }    //要有execute方法public String execute(){  return "success";}}

struts.

<??><!DOCTYPE struts PUBLIC  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  "http://struts.apache.org/dtds/struts-2.3.dtd"><struts><!--   <constant name="struts.enable.DynamicMethodInvocation" value="false" />  <constant name="struts.devMode" value="false" />  <package name="default" namespace="/" extends="struts-default">    <default-action-ref name="index" />    <global-results>      <result name="error">/error.jsp</result>    </global-results>    <global-exception-mappings>      <exception-mapping exception="java.lang.Exception" result="error"/>    </global-exception-mappings>    <action name="index">      <result type="redirectAction">        <param name="actionName">HelloWorld</param>        <param name="namespace">/example</param>      </result>    </action>  </package>  <include file="example.-->  <!-- Add packages here -->  <!-- 开发模式-->   <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="login" class="com.pb.web.action.LoginAction"> <result> /loginSuccess.jsp </result> </action> <action name="login2" class="com.pb.web.action.LoginAction2"> <result> /loginSuccess2.jsp </result> </action> <action name="login3" class="com.pb.web.action.LoginAction3"> <result> /loginSuccess3.jsp </result> </action> </package></struts>

登录login3.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>登录页面</title></head><body><form action="login3.action" method="post"><table><tr><td>用户名:</td><!--这里的name要和提交的地址中声明的实体类的属性来用 --><td><input type="text" name="userName" /></td></tr><tr><td>密码:</td><!--这里的name要和提交的地址中声明的实体类的属性来用 --><td><input type="password" name="passWord" /></td></tr><tr><td><input type="submit" value="登录" /></td><td><input type="reset" value="重置" /></td></tr></table></form></body></html>

登录成功loginSuccess3.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>  <!-- 导入struts标签 --> <%@taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>登录成功页面</title></head><body><!-- 输出:提交的地址中声明的实体类的属性来用-->欢迎您的到来!<s:property value="userName"/></body></html>

 六、区别

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;}}public class School {private String schoolName;private String schoolAddress;private String schoolName;private String schoolAddress;public String getSchoolName() {return schoolName;}public void setSchoolName(String schoolName) {this.schoolName = schoolName;}public String getSchoolAddress() {return schoolAddress;}public void setSchoolAddress(String schoolAddress) {this.schoolAddress = schoolAddress;}}

Action 使用ModelDriven 方式代码如下:

public class UserAction implements ModelDriven{private User user=new User();private School school=new School();@Overridepublic User getModel() {return user;}public String execute(){return "success";}}/*上述代码中,属性中尽管有两个Model(user 以及school),但是只有一个可以被getModel()返回(user),这个实体用于接收参数。*/

Action 使用JavaBean 方式代码如下:

public class UserAction2 {private User user;private School school;public User getUser() {return user;}public void setUser(User user) {this.user = user;}public School getSchool() {return school;}public void setSchool(School school) {this.school = school;}public String execute(){return "success";}}/*上述代码中,同样声明了两个Model(user 以及school),这两个实体都可以用于接收参数。*/

Action 接收参数:ModelDriven 方式与属性方式区别

ModelDriven 方式和属性方式同时存在Action 中时, ModelDriven 方式优先被赋值

//实体类Userpublic 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 代码如下,同时使用了ModelDriven 以及属性方式。并且在execute()方法中在控
制台分别为两种方式的属性输出了相应语句。

package com.pb.web.action;import com.opensymphony.xwork2.ModelDriven;import com.pb.entity.User;/* * 登录响应action3 */public class LoginAction3 implements ModelDriven<Object> {    //实例化对象  private User user=new User();   private String userName;  private String passWord;  //重写接口的方法  @Override  public User getModel() {    return user;  }    //要有execute方法public String execute(){  System.out.println("使用ModelDriven 方式:"+user.getUserName());  System.out.println("使用ModelDriven 方式:"+user.getPassWord());  System.out.println("使用属性 方式:"+userName);  System.out.println("使用属性 方式:"+passWord);  return "success";}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;}}

在struts.

<action name="login3" class="com.pb.web.action.UserAction3"><result>/loginSuccess3.jsp</result></action>

登录页面login3.jsp 表单代码如下:

<form action="login3" method="post">用户名:<input type="text" name="userName"/><br>用户密码:<input type="password" name="password"/><br><input type="submit" value="登录">&nbsp;&nbsp;<input type="reset" value="重置"></form>

登录成功loginSuccess3.jsp 主要代码如下:

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

控制台打印

使用ModelDriven 方式:去去去使用ModelDriven 方式:qqq使用属性 方式:null使用属性 方式:null

七、三种方试对比

三种Action 接收参数的方式中,当Action 接收非字符串类型参数时,Action 中相应属性最好设置为包装类型。