`

理解Struts2的Action中的setter方法是怎么工作的

    博客分类:
  • SSH
阅读更多

接触过webwork和Struts2的同行都应该知道,

提交表单的时候,只要Action中的属性有setter 方法,这些表单数据就可以正确赋值到Action中属性里;
另外对于Spring配置文件中声明的bean,也可以在Action中声明setter 方法将其注入到Action实例中。
那么现在要研究:这些是怎么工作的呢?


(1)提交表单时的参数
在struts2-core-2.3.1.2.jar压缩包内的struts-default.xml配置文件中有这个配置:
<interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
这个拦截器是负责解析请求中的URL参数,并赋值给action中对应的属性

来看代码:

//com.opensymphony.xwork2.interceptor.ParametersInterceptor.java
//主要代码如下:
//...

    @Override
    public String doIntercept(ActionInvocation invocation) throws Exception {
        Object action = invocation.getAction();
        if (!(action instanceof NoParameters)) {
            ActionContext ac = invocation.getInvocationContext();
            final Map<String, Object> parameters = retrieveParameters(ac);

            if (LOG.isDebugEnabled()) {
                LOG.debug("Setting params " + getParameterLogMap(parameters));
            }

            if (parameters != null) {
                Map<String, Object> contextMap = ac.getContextMap();
                try {
                    ReflectionContextState.setCreatingNullObjects(contextMap, true);
                    ReflectionContextState.setDenyMethodExecution(contextMap, true);
                    ReflectionContextState.setReportingConversionErrors(contextMap, true);

                    ValueStack stack = ac.getValueStack();
                    setParameters(action, stack, parameters);
                } finally {
                    ReflectionContextState.setCreatingNullObjects(contextMap, false);
                    ReflectionContextState.setDenyMethodExecution(contextMap, false);
                    ReflectionContextState.setReportingConversionErrors(contextMap, false);
                }
            }
        }
        return invocation.invoke();
    }

    protected void setParameters(Object action, ValueStack stack, final Map<String, Object> parameters) {
        ParameterNameAware parameterNameAware = (action instanceof ParameterNameAware)
                ? (ParameterNameAware) action : null;

        Map<String, Object> params;
        Map<String, Object> acceptableParameters;
        if (ordered) {
            params = new TreeMap<String, Object>(getOrderedComparator());
            acceptableParameters = new TreeMap<String, Object>(getOrderedComparator());
            params.putAll(parameters);
        } else {
            params = new TreeMap<String, Object>(parameters);
            acceptableParameters = new TreeMap<String, Object>();
        }

        for (Map.Entry<String, Object> entry : params.entrySet()) {
            String name = entry.getKey();

            boolean acceptableName = acceptableName(name)
                    && (parameterNameAware == null
                    || parameterNameAware.acceptableParameterName(name));

            if (acceptableName) {
                acceptableParameters.put(name, entry.getValue());
            }
        }

        ValueStack newStack = valueStackFactory.createValueStack(stack);
        boolean clearableStack = newStack instanceof ClearableValueStack;
        if (clearableStack) {
            //if the stack's context can be cleared, do that to prevent OGNL
            //from having access to objects in the stack, see XW-641
            ((ClearableValueStack)newStack).clearContextValues();
            Map<String, Object> context = newStack.getContext();
            ReflectionContextState.setCreatingNullObjects(context, true);
            ReflectionContextState.setDenyMethodExecution(context, true);
            ReflectionContextState.setReportingConversionErrors(context, true);

            //keep locale from original context
            context.put(ActionContext.LOCALE, stack.getContext().get(ActionContext.LOCALE));
        }

        boolean memberAccessStack = newStack instanceof MemberAccessValueStack;
        if (memberAccessStack) {
            //block or allow access to properties
            //see WW-2761 for more details
            MemberAccessValueStack accessValueStack = (MemberAccessValueStack) newStack;
            accessValueStack.setAcceptProperties(acceptParams);
            accessValueStack.setExcludeProperties(excludeParams);
        }

        for (Map.Entry<String, Object> entry : acceptableParameters.entrySet()) {
            String name = entry.getKey();
            Object value = entry.getValue();
            try {
                newStack.setParameter(name, value);
            } catch (RuntimeException e) {
                if (devMode) {
                    String developerNotification = LocalizedTextUtil.findText(ParametersInterceptor.class, "devmode.notification", ActionContext.getContext().getLocale(), "Developer Notification:\n{0}", new Object[]{
                             "Unexpected Exception caught setting '" + name + "' on '" + action.getClass() + ": " + e.getMessage()
                    });
                    LOG.error(developerNotification);
                    if (action instanceof ValidationAware) {
                        ((ValidationAware) action).addActionMessage(developerNotification);
                    }
                }
            }
        }

        if (clearableStack && (stack.getContext() != null) && (newStack.getContext() != null))
            stack.getContext().put(ActionContext.CONVERSION_ERRORS, newStack.getContext().get(ActionContext.CONVERSION_ERRORS));

        addParametersToContext(ActionContext.getContext(), acceptableParameters);
    }
//...

 

上面的代码ValueStack stack = ac.getValueStack();
表明,它是从当前Action上下文获取值栈(其实就类似一个全局Map集合,用来存储参数值或struts上下文全局变量),
然后由判断如果是当前action可以接受的参数(Action中有setter方法)就过滤出来,
调用这句“newStack.setParameter(name, value); ”来保存到值栈中,
保存到了值栈中其实action实例的属性就能拿到值了。
最后一句“addParametersToContext(ActionContext.getContext(), acceptableParameters);
表明它还把这些过滤出来的参数保存到了ActionContext上下文中,
这样,如果跳转的类型是forward(服务器内部重定向),
目标url中就会带上上次请求的url的所有有用的参数。

 

(2) Spring配置bean注入到Action中
来看一个简单的Action类:

package com.liany.demo.pubs.org.employee.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

import com.liany.demo.pubs.org.employee.model.Employee;
import com.liany.demo.pubs.org.employee.service.EmployeeService;

public class EmployeeAction extends ActionSupport{
	
	private EmployeeService employeeService;
	private List list; 
	private Employee employee = new Employee();  
	
	
	public StringReader getStringReader() {
		StringReader is = null;
		try {
			is = new StringReader(xmlBuf.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return is;
	}
	
	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	} 

	public List getList() {
		return list;
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
	public String execute(){
		//列表
		list = this.employeeService.getEmployees();
		
		return "list";
	}
	
	public String view(){
		employee = this.employeeService.getEmployeeById(employee.getId());
		
		return "view";
	}
	
	public String edit(){
		if(employee.getId()!=null){
			//修改
			employee = this.employeeService.getEmployeeById(employee.getId());
		}else{
			//新增
			employee.setId(null);
		}
		
		return "input";
	}
	
	public String save(){
		this.employeeService.saveEmployee(employee);
		
		return "repage";
	}
	
	public String delete(){
		this.employeeService.deleteEmployeeById(employee.getId());
		
		return "repage";
	}
}

 

上面Action中的employeeService对象其实是在Spring配置文件中声明的bean,
代码中给它定义一个public的setEmployeeService()方法,这样就可以将bean实例注入到
Action中的实例中,这个功能是在Struts过虑器初始化的时候初始化了一个全局变量,
从而使得调用action时,从spring ioc容器中找到这个bean,再set给action对象。
配置文件是在struts.properties 文件中声明:
struts.objectFactory = spring
struts.objectFactory.spring.autoWire = name

 

好了先研究到这里, 有兴趣的朋友可以交流交流...

 

 

3
0
分享到:
评论
1 楼 tianzhihehe 2012-10-24  

先顶一个
SPRING与STRUTS2整合的时候,发现ACTION的参数总是没有赋值为NULL。
终于发现这张贴,忘了INTERCEPTOR这桩。
开始DEBUG。

相关推荐

    struts1和struts2的区别

    另外,按照惯例,在Struts1.x中只有“execute”方法能调用Action, 但在Struts2中并非必要,任何声明为public String methodName() 方法,都能通过配置来调用Action。 最后,和Struts1.x最大的革命性的不同是,...

    struts1&struts2

    2.Struts2和Struts1.x的差别, 最明显的就是Struts2是一个pull-MVC架构。 这是什么意思呢?从开发者角度看, ... Struts 2无须继承任何类型或实现任何接口,表单数据包含在Action中,通过Getter和Setter获取。

    用Struts2新建一个应用的方法步骤

    用Struts2新建一个应用的方法步骤用Struts2新建一个应用的方法步骤: 1.首先新建一个Web Project。File——new——Web Project,在Project Name中输入一个合法的名字。例如:struts2 2. 单击采单Window——...

    struts项目学习笔记

    允许POJO(Plain Old Java Objects,简单javabean对象,没有继承,没有实现,getter,setter方法)对象 作为Action,没有耦合 Action的execute 默认方法不再与Servlet API耦合,更易测试 支持更多视图技术(JSP(转译java...

    webwork in action 第二卷(高清中文版2/3)

    为什么需要Ioc模式,它在struts2中如何体现其价值?struts2是如何利用基本的servlet构建出来这样一个灵活的框架的……这所有的疑问,在本书中都会给你一个满意的解答。  市面上的什么“深入浅出struts2”、“精通...

    webwork in action 第一卷(高清中文版1/3)

    为什么需要Ioc模式,它在struts2中如何体现其价值?struts2是如何利用基本的servlet构建出来这样一个灵活的框架的……这所有的疑问,在本书中都会给你一个满意的解答。  市面上的什么“深入浅出struts2”、...

    webwork in action 第三卷(高清中文版3/3)

    为什么需要Ioc模式,它在struts2中如何体现其价值?struts2是如何利用基本的servlet构建出来这样一个灵活的框架的……这所有的疑问,在本书中都会给你一个满意的解答。  市面上的什么“深入浅出struts2”、“精通...

    jsp+struts权限管理

    // getter和setter方法 public Function getF() { return f; } public void setF(Function f) { this.f = f; } public Module getM() { return m; } public void setM(Module m) { this.m = m; } ...

    spring考试通过必备材料.docx

    applicationContext.xml中setter方法(UserDAO注入到UserServiceImpl中) 12 3、引入实体类及配置文件book.java和book.hbm.xml 13 实体类Book.java 13 配置文件book.hbm.xml 14 在applicationContext.xml中配置映射...

    jquery.param.patch.zip

    页面上jQuery需要把一个复杂类型的javascript数组对象作为参数传到Struts2 action,但是他们的默认格式不匹配导致action无法正确获得参数。 Struts2 action class SomeAction{ List&lt;SomeBean&gt; list; //getter...

    ssh(structs,spring,hibernate)框架中的上传下载

    HibernateDaoSupport封装了HibernateTemplate,而HibernateTemplate封装了Hibernate所提供几乎所有的的数据操作方法,如execute(HibernateCallback action),load(Class entityClass, Serializable id),save(final ...

    J2EE应用开发详解

    118 8.3.5 Struts2配置文件 119 8.4 Action的配置方式 121 8.4.1 动态方法调用 121 8.4.2 设置action元素的method属性 122 8.4.3 使用通配符配置action 122 8.4.4 默认action 123 8.5 拦截器Interceptor 123 8.5.1 ...

    火炬博客系统7

    Struts框架中Controller角色是由一个中心Servlet类和众多Action类合作扮演的,中心Servlet类为控制器提供中心控制点来初步处理所有的用户请求,并选择一个Action类处理具体业务逻辑。 Struts负责管理用户的请求...

    火炬博客系统6

    Struts框架中Controller角色是由一个中心Servlet类和众多Action类合作扮演的,中心Servlet类为控制器提供中心控制点来初步处理所有的用户请求,并选择一个Action类处理具体业务逻辑。 Struts负责管理用户的请求...

    火炬博客系统5

    Struts框架中Controller角色是由一个中心Servlet类和众多Action类合作扮演的,中心Servlet类为控制器提供中心控制点来初步处理所有的用户请求,并选择一个Action类处理具体业务逻辑。 Struts负责管理用户的请求...

    《MyEclipse 6 Java 开发中文教程》前10章

    3.2.10生成getter和setter 方法 60 3.2.11格式化源代码 61 3.2.12注释和取消注释 61 3.2.13手工和自动编译 61 3.2.14直接粘贴Java源码为类文件 61 3.2.15复制项目中的文件 61 3.2.16断点和调试器 62 3.2.17快速加入...

    网络架构师148讲视频课程

    │ 第20节:X-gen生成需要的Action.avi │ 第21节:通过X-gen生成商品模块.avi │ 第22节:通过X-gen生成购物车模块.avi │ 第23节:通过X-gen来生成订单和库存模块.avi │ 第24节:加入ehcache,把工程加入到Git....

Global site tag (gtag.js) - Google Analytics