JDK1.6、tomcat6、Struts2.3
Struts2接收参数最常用的有三种方式
1、在action中定义对应的变量名,添加get和set方法,Struts2会帮助我们自动完成参数传递。具体代码如下:
下面是工程的目录结构
ParaTransferAction.java
package org.jsoft.action; import com.opensymphony.xwork2.ActionSupport; public class ParaTransferAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; private String username; private String password; public String login(){ System.out.println("username = " + username + " password = " + password); return "login"; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!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="true" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="paraTransfer" class="org.jsoft.action.ParaTransferAction"> <result name="login">login.jsp</result> </action> </package></struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.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>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <a href="paraTransfer!login?username=123&password=123">猛戳这里</a> </body></html>
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 登陆成功 <br> </body></html>
通过username和password的对比,我们可以发现Struts2调用了我们自动生成的get和set方法,把数据传入action。也就是说,可以通过action中的get和set方法得到对应的数据。
2、很显然如果要传递较多的数据,比如我们定义一个User类,里面有他的用户名、密码、年龄、生日等,那么我们要在action里面添加所有属性的get和set,而User类还要再写一遍,这是一件麻烦而且重复的工作,我们可以通过对象的传递来实现。代码如下:
新增的User类:
package org.jsoft.action; import java.util.Date; public class User { private String username; private String password; private int age; private int sex; private Date birthday; 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
修改ParaTransferAction
package org.jsoft.action; import com.opensymphony.xwork2.ActionSupport; public class ParaTransferAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; public User user; public String login(){ System.out.println("username = " + user.getUsername() + " age = " + user.getAge() + " birthday = " + user.getBirthday()); return "login"; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
index.jsp
要把原来的<a>标签替换成
<a href="paraTransfer!login?user.username=123&user.age=12&user.birthday=2012-01-01">猛戳这里</a>
重启服务,我们可以再后台看到:
username = 123 age = 12 birthday = Sun Jan 01 00:00:00 CST 2012
我们得到了user的几个属性数据。通过这个例子我们可以发现只要用user.XXX即可给user的对应属性赋值。
3、通过实现ModelDriven接口实现数据传递
Struts2为我们提供了ModelDriven接口,可以完成数据传递。代码修改如下:
添加ParaTransferImplAction实现ModelDriven接口
package org.jsoft.action; import com.opensymphony.xwork2.ModelDriven; public class ParaTransferImplAction implements ModelDriven<User>{ private User user = new User(); public User getModel() { // TODO Auto-generated method stub return user; } public String login(){ System.out.println("username = " + user.getUsername() + " age = " + user.getAge() + " birthday = " + user.getBirthday()); return "login"; } }
修改struts.xml,在package标签中添加一个action
<action name="paraTransferImpl"
class="org.jsoft.action.ParaTransferImplAction">
<result
name="login">login.jsp</result>
</action>
index.jsp
要把原来的<a>标签替换成
<a href="paraTransferImpl!login?username=123">猛戳这里</a>
运行控制台结果是
username = 123 age = 0 birthday = null
因为我们只传递了username,所以其他的没有得到数据。在action中我们并没有user的get和set,同样可以得到数据,而且并没有像第二种方法user.XXX,直接用属性名就可以。
思考问题:
1、如果在action中添加另外一个类,如Person,Person也有username这个属性,其他代码不动,那么username的数据将会如何复制?
2、如果你是开发者你会选择哪种方式?