熟悉ssh框架的朋友在使用struts2作为mvc框架时候,会在action类上设置成员属性对应页面上提交的参数,当程序启动的时候struts2会将request中的parameter通过反射的机制自动设置到action上。struts框架会负参数责转型等问题的处理,这样一来可以大大减少web开发过程中的重复劳动,大大降低出错的概率。
如果能将该机制底层的实现流程搞清楚,将来可以将这个功能模块移植到其他框架中,那无疑会提升整体代码的优雅性。
import java.lang.reflect.Field;
import ognl.OgnlRuntime;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ognl.OgnlValueStackFactory;
import com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor;
import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.ValueStack;
/**
* Base Action class for the Tutorial package.
*/
public class ExampleSupport extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] arg) throws Exception {
OgnlValueStackFactory factory = new OgnlValueStackFactory();
factory.setXWorkConverter(new ExtendXWorkConverter());
Field field = OgnlValueStackFactory.class
.getDeclaredField("compoundRootAccessor");
field.setAccessible(true);
CompoundRootAccessor propertyAccessor = new CompoundRootAccessor();
field.set(factory, propertyAccessor);
factory.setContainer(new MockContainer());
OgnlRuntime.setPropertyAccessor(CompoundRoot.class, propertyAccessor);
ValueStack valueStack = factory.createValueStack();
User user = new User();
valueStack.push(user);
valueStack.setValue("createTime", "2012-12-12 5/24/33");
valueStack.setValue("name", "baisui");
valueStack.setValue("age", "11");
valueStack.setValue("gender", "true");
valueStack.setValue("department", new String[] { "office" });
System.out.println(user.getName());
System.out.println(user.getAge());
System.out.println(user.getDepartment());
System.out.println(user.getGender());
System.out.println(user.getCreateTime());
}
}
public class User {
private String name;
private Integer age;
private Date createTime;
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
private String[] department;
private Boolean gender;
public String[] getDepartment() {
return department;
}
public void setDepartment(String[] department) {
this.department = department;
}
public Boolean getGender() {
return gender;
}
public void setGender(Boolean gender) {
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
分享到:
相关推荐
xwork-1.1.1.jar, xwork-1.1.jar, xwork-1.2.1.jar, xwork-1.2.3.jar, xwork-2.0.0.jar, xwork-2.0.1.jar, xwork-2.0.3.jar, xwork-2.0.4.jar, xwork-2.0.5.jar, xwork-2.0.6.jar, xwork-2.0.7.jar, xwork-2.1.0.jar...
JavaEE源代码 xwork-2.0.4JavaEE源代码 xwork-2.0.4JavaEE源代码 xwork-2.0.4JavaEE源代码 xwork-2.0.4JavaEE源代码 xwork-2.0.4JavaEE源代码 xwork-2.0.4JavaEE源代码 xwork-2.0.4JavaEE源代码 xwork-2.0.4JavaEE源...
xwork-2.0.4源码 xwork2源码
xwork源码及文档,xwork源码及文档,xwork源码及文档
xwork源码,xwork源码,xwork源码
xwork-2.0.4 源码 xwork-2.0.4 源码
xwork-2.1.2-all.zip,xwork2.2.1源码,github上下载,适合不会SVN下载的初学者
xwork 源文件 及相关文档struts2参考
xwork2 chm api
NULL 博文链接:https://hezhou-0521.iteye.com/blog/781942
用于解决struts2升级至2.3.37时 出现java.lang.NoSuchMethodError: com.opensymphony.xwork2.ActionContext.put(Ljava/lang/异常时,更换的xwork-core-2.3.37.jar
里面包含xwork-2.0.7jar和xwork-2.0.7的源码
xwork-1.2.1
XWork处理用户请求参数数据时存在漏洞,远程攻击者可以利用此漏洞在系统上执行任意命令。 Struts2中WebWork框架使用XWork基于HTTP参数名执行操作和调用,将每个HTTP参数名处理为OGNL(对象图形导航语言)语句,而...
xwork-2.0.7.jar是 struts2.0的包,
xwork-2.1.6.rar
这是xwork-core的源代码,这是学习struts要用到的xwork-core的源代码
Struts2 2.1.6 xwork源码 xwork-2.1.2-sources
struts2 之xwork源码 供学习Struts2和Xwork的朋友使用
xwork源码 xwork src Eclipse关联xwork源码 这个目录里面xwork-2.0.7\src\java