Struts2----参数传递

参数传递即将参数传输到程序后台中,后台可能做一些处理,然后再将内容存入数据库之类嗒!

参数传递的方法较多,一一说明如下。

1、Action中直接参数法

有如下的index.jsp文件

[html] view plaincopy
  1. <?xml version="1.0" encoding="GB18030" ?>  
  2. <%@ page language="java" contentType="text/html; charset=GB18030"  
  3.     pageEncoding="GB18030"%>  
  4.   
  5. <%   
  6. String path = request.getContextPath();  
  7. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  8. %>  
  9.   
  10. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  11. <html xmlns="http://www.w3.org/1999/xhtml">  
  12. <head>  
  13. <meta http-equiv="Content-Type" content="text/html; charset=GB18030" />  
  14. <base href="<%=basePath %>"/>  
  15. <title>Insert title here</title>  
  16. </head>  
  17. <body>  
  18. 使用action属性接收参数<a href="user/user!add?name=a&age=8">添加用户</a>  
  19.       
  20. </body>  
  21. </html>  

对于其中的<a></a>来说,传递两个参数至程序,一个是name,一个是age,在struts.xml中的配置如下:

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.     <constant name="struts.devMode" value="true" />  
  8.     <package name="user" extends="struts-default" namespace="/user">  
  9.           
  10.         <action name="user" class="com.bjsxt.struts2.user.action.UserAction">  
  11.             <result>/user_add_success.jsp</result>  
  12.         </action>  
  13.     </package>  
  14. </struts>  

这时我们的UserAction该如何写呢?范例如下:

[java] view plaincopy
  1. package com.bjsxt.struts2.user.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class UserAction extends ActionSupport {  
  6.       
  7.     private String name;  
  8.     private int age;  
  9.       
  10.     public String add() {  
  11.         System.out.println("name=" + name);  
  12.         System.out.println("age=" + age);  
  13.         return SUCCESS;  
  14.     }  
  15.   
  16.     public String getName() {  
  17.         return name;  
  18.     }  
  19.   
  20.     public void setName(String name) {  
  21.         this.name = name;  
  22.     }  
  23.   
  24.     public int getAge() {  
  25.         return age;  
  26.     }  
  27.   
  28.     public void setAge(int age) {  
  29.         this.age = age;  
  30.     }  
  31.       
  32.       
  33. }  

很简单,就是定义了两个属性,注:这两个属性的set和get方法必须要写,可以使用eclipse的快速生成方式,非常简单。这样上述程序在运行时就会打印出所要的结果

name=a和age=8。

有说明如下:第一,struts2会自动进行参数传递,这个过程无需我们参与;第二,struts传递参数时针对的是set和get方法,而不是name和age属性,也就是说,假如我们修改其中的name为其他名称,如username,但是方法仍然是setName和getName的话,对于整个功能的实现来说没有任何区别,只是有点别扭而已;第三,也是最重要的一点,就是假如有很多的属性,这样的话我们就需要非常多的set和get方法,这是非常不方便的,因此引伸出了下面这种方式。

2、Action添加类对象法

这个时候我们1中的属性都归于一个类中,如User

[java] view plaincopy
  1. package com.bjsxt.struts2.user.model;  
  2.   
  3. public class User {  
  4.     private String name;  
  5.     private int age;  
  6.     public String getName() {  
  7.         return name;  
  8.     }  
  9.     public void setName(String name) {  
  10.         this.name = name;  
  11.     }  
  12.     public int getAge() {  
  13.         return age;  
  14.     }  
  15.     public void setAge(int age) {  
  16.         this.age = age;  
  17.     }  
  18. }  

这样在Action类中的写法就变得简单了很多

[java] view plaincopy
  1. package com.bjsxt.struts2.user.action;  
  2.   
  3. import com.bjsxt.struts2.user.model.User;  
  4. import com.opensymphony.xwork2.ActionSupport;  
  5.   
  6. public class UserAction extends ActionSupport {  
  7.       
  8.     private User user;  
  9.   
  10.     public String add() {  
  11.         System.out.println("name=" + user.getName());  
  12.         System.out.println("age=" + user.getAge());  
  13.         return SUCCESS;  
  14.     }  
  15.   
  16.     public User getUser() {  
  17.         return user;  
  18.     }  
  19.   
  20.     public void setUser(User user) {  
  21.         this.user = user;  
  22.     }  
  23.       
  24. }  

注:此时我们不需要自己手动生成一个User对象,这个过程是由Struts2自动完成的。

并且此时的url也需要作下修改,即index中的<a></a>标签作下修改:

[java] view plaincopy
  1. <?xml version="1.0" encoding="GB18030" ?>  
  2. <%@ page language="java" contentType="text/html; charset=GB18030"  
  3.     pageEncoding="GB18030"%>  
  4.   
  5. <%   
  6. String path = request.getContextPath();  
  7. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  8. %>  
  9.   
  10. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  11. <html xmlns="http://www.w3.org/1999/xhtml">  
  12. <head>  
  13. <meta http-equiv="Content-Type" content="text/html; charset=GB18030" />  
  14. <base href="<%=basePath %>"/>  
  15. <title>Insert title here</title>  
  16. </head>  
  17. <body>   
  18. 使用Domain Model接收参数<a href="user/user!add?user.name=a&user.age=8">添加用户</a>  
  19.       
  20. </body>  
  21. </html>  

修改成上述18行部分。

上篇说明了下Struts2参数传递的两种方法,其中第二种方法被称为:DomainModel,域模型。即新建一个类,用于存放属性。

下面说明另外一种方法,被称为:ModelDriven,模型驱动。

它与第二种方法非常类似,其他都是一样的,仅仅就是Action和访问有区别,它的Action如下:

[java] view plaincopy
  1. package com.bjsxt.struts2.user.action;  
  2.   
  3. import com.bjsxt.struts2.user.model.User;  
  4. import com.opensymphony.xwork2.ActionSupport;  
  5. import com.opensymphony.xwork2.ModelDriven;  
  6.   
  7. public class UserAction extends ActionSupport implements ModelDriven<User>{  
  8.       
  9.     private User user = new User();  
  10.       
  11.     public String add() {  
  12.         System.out.println("name=" + user.getName());  
  13.         System.out.println("age=" + user.getAge());  
  14.         return SUCCESS;  
  15.     }  
  16.   
  17.     @Override  
  18.     public User getModel() {  
  19.         return user;  
  20.     }  
  21.       
  22. }  

我们可以从中看到,它实现了ModelDriven接口,并采用了泛型技术。采用这种方式Struts2不会自动的实例化一个对象,因此只能我们手动生成。它覆写了ModelDriven接口的getModel()方法,它的作用就是返回一个类对象。

它的访问是和第二种有区别的(与第一种方法一样):

[html] view plaincopy
  1. 使用ModelDriven接收参数<a href="user/user!add?name=a&age=8">添加用户</a>  

它并没有采用user.name的方式,这也是为什么必须要new一个对象的原因。

这种方式的基本思想过程为:首先Action解析url,获得其中的参数,然后进入Action中,发现此Action实现了一个ModelDriven接口,此时就调用ModelDriven接口的getModel方法,获得类的对象,然后调用此类的set和get方法,将参数传入。

此种方式体现了Struts2的MVC思想,M----Model,V----View,C----Controller,但是这种方式很少使用,我们使用最多的还是上篇的第二种方式。


posted on 2013-03-11 09:57  道法自然smile  阅读(281)  评论(0编辑  收藏  举报