Struts 2-我可以访问不在ValueStack上的另一个操作的属性吗? [英] Struts 2 - Can I access properties of another action which is not on the ValueStack?

查看:85
本文介绍了Struts 2-我可以访问不在ValueStack上的另一个操作的属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个JSP页面,显示两个不同操作的两个列表:页面A显示员工列表,页面B显示部门列表.

I have two JSP pages displaying two lists from two different actions: page A displays employee list, page B displays department list.

两个页面的顶部都有一个公共文本字段(包含在JSP的第三个页面中),用于按名称搜索员工:

Both pages have a common text field (included from a third JSP page) on the top to search employees by name:

<s:form action="searchEmployeesByName">
    <s:textfield name="employeeName" />
    <s:submit>
</s:form>

搜索动作是类EmployeeAction的一部分,我可以加载页面A并执行搜索而不会出现问题.但是,在加载页面B时,我遇到了ognl.NoSuchPropertyException,因为属性employeeName不在DepartmentActionValueStack上.

The search action is a part of class EmployeeAction and I can load page A and perform searching without problems. However, when loading page B, I encountered ognl.NoSuchPropertyException because property employeeName is not on the ValueStack of DepartmentAction.

我该如何解决这个问题?有什么方法可以从DepartmentAction访问EmployeeActionemployeeName?还是应该重新组织我的操作以执行常见的搜索功能?

How can I solve this problem? Are there any ways to access employeeName of EmployeeAction from DepartmentAction? Or how should I reorganize my actions to perform the common search functionality?

这是我的动作配置文件:

Here is my action configuration file:

<struts>

    <package name="employee" namespace="/employee" extends="tiles-default">

        <action name="getEmployeeList" class="my.package.EmployeeAction"
            method="getEmployeeList">
            <result name="success">/employee_list.tiles</result>
        </action>

        <action name="searchEmployeesByName" class="my.package.EmployeeAction"
            method="searchEmployeesByName">
            <result name="success">/search_results.tiles</result>
        </action>

    </package>

    <package name="department" namespace="/department" extends="tiles-default">

        <action name="getDepartmentList" class="my.package.DepartmentAction"
            method="getDepartmentList">
            <result name="success">/department_list.tiles</result>
        </action>

    </package>

</struts>

推荐答案

操作是根据请求创建的,并且不共享上下文,因为它是线程本地的.如果需要通过操作设置的属性,则应在url中为其提供参数或从会话中获取它.您应该为要传递的属性创建getter和setter.通常,使用param标签完成的传递参数可用于参数化其他标签.

Actions are created upon request and don't share the context because it's local to their thread. If you need the property set by the action then you should supply it with parameter in the url or take it from the session. You should create getters and setters for the property you want to pass. Usually passing parameters done with param tag can be used to parametrize other tags.

在您的情况下,您可以在结果配置中使用param标记来创建动态参数

In your case you could use param tag in the result configuration to create a dynamic parameter

<result name="searchEmployeesByName" type="redirectAction">
  <param name="actionName">department</param>
  <param name="employeeName">${employeeName}</param>
</result>

请参见动态结果配置.

这篇关于Struts 2-我可以访问不在ValueStack上的另一个操作的属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆