《Struts2接收参数的几种方式.pdf》由会员分享,可在线阅读,更多相关《Struts2接收参数的几种方式.pdf(4页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Struts2Struts2Struts2Struts2 接收参数的几种方式接收参数的几种方式1.1.1.1.用用 ActionActionActionAction 的属性:的属性:在 action 里面定义要接收的参数,并提供相应的 setter,getter,和提交参数的名称一致,并不用做数据类型的转换。相应提交方式可以用 get 和 post,如:testAction?name=admin如:publicpublicpublicpublic classclassclassclass TestAction extendsextendsextendsextends BaseActionpri
2、vateprivateprivateprivate staticstaticstaticstatic finalfinalfinalfinal longlonglonglongserialVersionUID=-7463970150000893325L;privateprivateprivateprivate String name;publicpublicpublicpublic voidvoidvoidvoid execeute()System.out.println(name+:+name);publicpublicpublicpublic String getName()returnr
3、eturnreturnreturn name;publicpublicpublicpublic voidvoidvoidvoid setName(String name)thisthisthisthis.name=name;2.2.2.2.使用使用 DomainDomainDomainDomainModelModelModelModel:在 Action 里面不存很多的属性,而是用 Model 层用到的模型,保存它的一个对象。相应提交方式可以用 get 和 post,如:testAction?resBananRc.name=admin如:publicpublicpublicpublic cla
4、ssclassclassclass TestAction extendsextendsextendsextends BaseActionprivateprivateprivateprivate staticstaticstaticstatic finalfinalfinalfinal longlonglonglongserialVersionUID=-7463970150000893325L;privateprivateprivateprivate ResBananRc resBananRc;publicpublicpublicpublic ResBananRc getResBananRc()
5、returnreturnreturnreturn resBananRc;publicpublicpublicpublic voidvoidvoidvoid setResBananRc(ResBananRc resBananRc)thisthisthisthis.resBananRc=resBananRc;publicpublicpublicpublic voidvoidvoidvoid execeute()System.out.println(姓名:+resBananRc.getName();3.3.3.3.使用使用 DTO-DTO-DTO-DTO-数据传输对象数据传输对象它的作用是接收参数,
6、传递参数,并不是项目中的实体类。如用户注册时,会用到确认密码,所以要先把参数接收过来,做处理后,再传递给相应方法去创建 User 对象。提交参数的方式的 Domail Model 方式的相同。DTO:publicpublicpublicpublic classclassclassclass UserDTO privateprivateprivateprivate String name;privateprivateprivateprivate String password;privateprivateprivateprivate String confirm;publicpublicpubl
7、icpublic String getName()returnreturnreturnreturn name;publicpublicpublicpublic voidvoidvoidvoid setName(String name)thisthisthisthis.name=name;publicpublicpublicpublic String getPassword()returnreturnreturnreturn password;publicpublicpublicpublic voidvoidvoidvoid setPassword(String password)thisthi
8、sthisthis.password=password;publicpublicpublicpublic String getConfirm()returnreturnreturnreturn confirm;publicpublicpublicpublic voidvoidvoidvoid setConfirm(String confirm)thisthisthisthis.confirm=confirm;Action:publicpublicpublicpublic classclassclassclass TestAction extendsextendsextendsextends B
9、aseActionprivateprivateprivateprivate staticstaticstaticstatic finalfinalfinalfinal longlonglonglongserialVersionUID=-7463970150000893325L;privateprivateprivateprivate UserDTO userDTO;publicpublicpublicpublic UserDTO getUserDTO()returnreturnreturnreturn userDTO;publicpublicpublicpublic voidvoidvoidv
10、oid setUserDTO(UserDTO userDTO)thisthisthisthis.userDTO=userDTO;publicpublicpublicpublic voidvoidvoidvoid execeute()System.out.println(姓名:+userDTO.getName();4.4.4.4.使用使用 ModelDriven:ModelDriven:ModelDriven:ModelDriven:在创建 Action 的时候,发现 Action 实现了 ModelDriven 接口,去调用 接口的 getModel()方法,取到了相关对象。相应提交方式可以用
11、 get 和 post,如:testAction?name=adminpublicpublicpublicpublic classclassclassclass TestAction extendsextendsextendsextends BaseAction implementsimplementsimplementsimplements ModelDrivenprivateprivateprivateprivate staticstaticstaticstatic finalfinalfinalfinal longlonglonglongserialVersionUID=-7463970
12、150000893325L;privateprivateprivateprivate ResBananRc resBananRc=newnewnewnew ResBananRc();/这里要手动的New下publicpublicpublicpublic ResBananRc getModel()returnreturnreturnreturn resBananRc;publicpublicpublicpublic voidvoidvoidvoid execeute()System.out.println(姓名:+resBananRc.getName();5 5 5 5.使用使用 request
13、requestrequestrequest 对象对象:此方法与与传统的 JSP 等传接参数一样,即使用 request.getParameter(“”)方法publicpublicpublicpublic classclassclassclass TestAction extendsextendsextendsextends BaseActionprivateprivateprivateprivate staticstaticstaticstatic finalfinalfinalfinal longlonglonglongserialVersionUID=-7463970150000893325L;publicpublicpublicpublic voidvoidvoidvoid execeute()String name=supersupersupersuper.getRequest().getParameter(paraName);System.out.println(姓名:+name);其中上述第其中上述第 1-31-31-31-3 相对而言较为常用,第相对而言较为常用,第 4 4 4 4、5 5 5 5 方法相对不常用。方法相对不常用。
限制150内