在servlet中,我们以request.getParater("")方式去传递获取参数,那么在Structs2中是传递的参数的方式大体上有3中:
1:简单类型参数的传递eg:int,String等等,传递方式非常的简单 eg: 表单参数<input type="xxx" name="参数名">,
对应的类中接收参数也非常的简单:在 Action里面: 提供参数对应的属性, 并提供setXXX(),通过查看文档,得之在底层是采用了发射(reflect机制)去匹配对应的参数,所以在类中必须要提供一个setXXX(Xxx为参数名字),以给struts2框架提供激活方法的入口。
eg:
package com.yc.web.actions;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class StudentAction extends ActionSupport{
private String sname;
private Integer age;
private String sex;
public String reg(){
System.out.println("调用了reg()");
return Action.SUCCESS;
}
public void setSname(String sname){
System.out.println("调用了setName:"+sname);
this.sname=sname;
}
public void setAge(int age){
this.age=age;
}
public void setSex(String sex){
this.sex=sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSname() {
return sname;
}
public String getSex() {
return sex;
}
}
但是从上面的方法也可以得出,这样通过直接setXxx的方式激活并不好,想象一下,加入要传递100个参数,则要在此类 中写100个set方法,而且维护起来非常的麻烦。
第二种方法:通过对象传参:在前段传: <input type="xx" name="对象名.参数名">
在后台接参:只要实现2步a. 创建这个接收参数的注入类 b. 在Action中定义这个类属性,并提供set和get两个方法.eg:
package com.yc.web.actions;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.yc.bean.Student;
public class StudentAction extends ActionSupport{
public Student student;
public String reg(){
System.out.println("调用了reg()");
System.out.println("student"+student);
return Action.SUCCESS;
}
public void setStudent(Student student){
System.out.println("调用setStudent方法");
this.student=student;
}
public Student getStudent(){
System.out.println("调用了getStudent方法");
return student;
}
}
这样就是面向bean类开发,对维护已经操作有极大的帮助。
第三种方法:通过ModelDriven方式传递参数(一般不建议用,因为这对Javascript的校验会有影响,对一些老项目要改进有一定的影响)
传:<input type="xx" name="参数名">
接: class XXXAction implements ModelDriven{
public Object getModel(){
return 对象名;
}
}
接下来说下Struct2操作Wbb对象:
一般有4个方法:
1:通过ActionContext取到Map,它的原理是struts2的底层将 HttpServletRequest, HttpSession, Servletcontext三种对象转成了 Map<String,Object>存在 ActionContext 对象. 这就实现了httprequest的解耦了,这是非常好的。它的实现:Map= ActionContext.getContext().get("request");ActionContext.getContext().getSession();
ActionContext.getContext().getApplication();
2:使用RequestAware,SessionAware,ApplicationAware接口,来注入 三个Map,这个方法非常的重要,一般都是使用这个方法。
实现:实现:
class XXXAction implements RequestAware, SessionAware, ApplicationAware{
public void setSession( Map<String,Object> session){
xxx;
}
}
3:通过ServletActionContext取HttpServletRequest对象,它的原理: 通过 ServletActionContext取出 HttpServletRequest对象,然后HttpSession,ServletContext按j2ee的方法取。实现的原理: 通过 ServletActionContext取出 HttpServletRequest对象,然后HttpSession,ServletContext按j2ee的方法取.
实现:
HttpServletRequest request=ServletActionContext.getRequest();
HttpSession session=request.getSession();
ServletContext application= session.getServletContext();
4:通过 ServletRequestAware接口,来注入HttpServletRequest对象即可.
原理:使用 ServletRequestAware接口注入一个 HttpServletRequest对象.
实现
public void setServletRequest(HttpServletRequest arg0) {
this.request = arg0;
session = this.request.getSession();
application = this.session.getServletContext();
}