论坛首页 Java企业应用论坛

利用xwork封装request parameter

浏览 2158 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-04-15  

    熟悉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("name", "baisui");
		valueStack.setValue("age", "11");
		valueStack.setValue("gender", "");
		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());

	}
}

 

public class User {
	private String name;
	private Integer age;

	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;
	}
}
 
   发表时间:2012-04-18   最后修改:2012-04-18
你的代码缺少ExtendXWorkConverter类和MockContainer类,我帮你修改了一下,代码如下:

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.config.Configuration;
import com.opensymphony.xwork2.config.ConfigurationManager;
import com.opensymphony.xwork2.inject.Container;
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;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import ognl.OgnlRuntime;


public class YouAreStupid 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);

        //$$$$$$ YouAreStupid修改 开始 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        Map paramMap = new HashMap();
        paramMap.put("id", "iblog.iteye.com");
        Map context = new HashMap();
        context.put(ActionContext.PARAMETERS, paramMap);
        ConfigurationManager cm = new ConfigurationManager();
        Configuration conf = cm.getConfiguration();
        Container containter = conf.getContainer();
        //$$$$$$ YouAreStupid修改 开始 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

        factory.setContainer(containter);
        OgnlRuntime.setPropertyAccessor(CompoundRoot.class, propertyAccessor);



        ValueStack valueStack = factory.createValueStack();

        User user = new User();
        valueStack.push(user);

        valueStack.setValue("name", "baisui");
        valueStack.setValue("age", "11");
        valueStack.setValue("gender", "");
        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());

    }
}



import com.opensymphony.xwork2.conversion.impl.XWorkConverter;

/**
 *
 * @author YouAreStupid
 */
public class ExtendXWorkConverter extends XWorkConverter {
    public ExtendXWorkConverter(){
        super();
    }
}



public class User {

    private String name;
    private Integer age;
    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;
    }
}

0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics