http://www.35java.com/zhibo/forum.php?mod=viewthread&tid=364&extra=page%3D4
| 由于开发任务紧张,因为这里的开发笔记,仅用于记录遇到的几个struts2和hibernate结合开发的现象.不对其做分析. 1. 在使用struts2时,页面和action之间的传值 这是struts2和struts1最大的区别. Struts2中,action和jsp页面之间的信息交互,是通过 action中定义的成员变量来实现的. 例如,我在一个名为EstateAction的类中有如下定义 public class CityAction extends BaseAction { private MthCity mthCity ; private String cityName; private Long cityId private int couter; public String loadCity() throws DataAccessException, BaseException{ counter ++; return "city"; } } 然后,这里上面的类中的成员类MthCity的定义如下 public class MthCity implements java.io.Serializable { private Long cityId private String cityName; public MthCity() { public Long getCityId() { return this.cityId; } public void setCityId(Long cityId) { this.cityId = cityId; public String getCityName() { return this.cityName; } public void setCityName(String cityName) { this.cityName = cityName; } } 这是一个Hibatenate使用的数据对象 POJO类. 有了这两个类后,我们来看看Struts2的Action和JSP页面之间是如何交互的 一. JSP -> Action Jsp页面 以下是一个jsp页面submit.jsp.这个页面只有一个功能,就是向struts提交申请 <%@ page language="java" contentType="text/html; charset=gbk"%> <%@ include file="/common/taglibs.jsp"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gbk" /> <title>提交</title> </head> <script> function go (){ window.location ="${pageContext.request.contextPath}/admin/city/loadCity.do”; } </script> <body> <form name=”myform” > <input type="button" name="cityupdate" id="cityupdate" value="编辑"/> <input type="hidden" name="mthCity.cityName" id=" mthCity " value="广州" /> </form> </body> </html> 大家可以看到,这个页面只有一个按钮,将页面提交到struts的一个action中,这是为什么呢. 我们先看一段struts2的配置文件admin-action.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="admin" namespace="/admin" extends="struts-default"> <action name="city/*" method="{1}" class="com.mytophome.admin.representation.struts.action.CityAction"> <result name="city">/admin/city.jsp</result> <result name="city_update">/admin/city_update.jsp</result> </action> </package> </struts> 这是一个struts2的典型配置文件. 上面有几处要注意的 首先是namespace = “/admin” 这是一个struts模块名,可以不写,但如果写了,能比较方便的将struts2的action按配置来分模块.(何谓分模块呢?struts2有个特性,是action定义不需要像struts1一样写在同一个struts.xml文件中.而是可以使用include的形式.例如我使用的项目的struts.xml文件就是这样写的: <struts> <include file="struts-action/admin-action.xml"/> <include file="struts-action/agent-action.xml"/> </struts> 这样include了一系统的xml配置,而上面的admin-action.xml文件就是其中一段,因此将这一段中涉及的action类设定为一个模块,就定namespace = “/admin” ) 其次 <action name="city/*" method="{1}" 这一句配置的意思,就是,当用户提交一个符合struts2格式的申请时(所有包含.do形式的http链接) 例如http:/city/loadCity.do 其中包含了/city/那么在配置 文件中,只要定义action name=”city/*”,那么所有包含有/city/的do,都会提交到action定义的类中来,也就是类om.mytophome.admin.representation.struts.action.CityAction中,那么提交到这个类的哪个方法中呢? 因为选择的是city/*.而且mothed={1},所以方法名由链接指定 也就是loadCity.do所指定的.loadCity方法. 这个do方法后面是可以带参数的.所带的参数名,要是CityAction中定义的成员变量,包括成员类.例如,如果想提交后,CityAction中的cityId有值,链接可以这样写 http:/city/loadCity.do?cityId=9 这样,在loadCity方法中,如果你访问cityId,就可以发现cityId的值是9 System.out.println(Long.toString(cityId)); 但这里有一个条件,就是CityAction中,必须要有cityId变量的getter/setter方法(这两个方法可以用MyEclipse自动生成) public Long getCityId() { return cityId; } public void setCityId(Long cityId) { this.cityId = cityId; }如果要给CityAction中的MthCity类的 这样才能在jsp页面提交时,由struts为cityId赋值.(当然,getter方法就方便当action返回到jsp页面时,cityId的值能在页面取到.) 如果要为action中的类成员变量赋值也是可以的 例如http:/city/load ... mp;mthCity.cityId=8 这条链接提交后,会和上面一样调用CityAction的loadCity方法,但这里,action的成员mthCity会被创建实例,并为mthCity的一个属性cityId赋值,也就是说,在action中,大家可以通过mthCity.getCityId()来获得这个属性的值. 当然,一定要在action设置mthCity的getter setter,jsp上的内容才能传到action中 public MthCity getMthCity() return mthCity; } public void setMthCity(MthCity city) { this. mthCity = city; } 从JSP提交内容到Action,还有一种方法,就是将参数内容放在表单form的元素中 <input type="hidden" name="mthCity.cityName" id=" mthCity " value="广州" /> 这样,当用以下语句表单提交的时候 doucment. myform.submit(); 就能在Action中创建一个mthCity实例,并为mthCity.cityName设置值为:广州. 原因是在页面的表单元素中,设置了name= mthCity.cityName,而action中刚好有成员类叫mthCity,而这个类中刚好有属性叫cityName.,就是通过这样的方法,能将表单的内容,提交到Action中. |
1000

被折叠的 条评论
为什么被折叠?



