Struts 2 - Can I access properties of another acti

2019-08-06 15:43发布

问题:

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

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>

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.

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>

回答1:

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.

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>

See Dynamic Result configuration.



回答2:

The ognl.NoSuchPropertyException is thrown when a property is attempted to be extracted from an object that does not have such a property.

So there may not be getter and setter methods created for your OGNL expression on your respective Action class.

you can use Result Type chain(not recommended) in your result tag to access properties of one action in another.

you can also use redirectAction Result Type.

 <action name="getEmployeeList" class="...">
    <!-- Chain to another namespace -->
    <result type="chain">
        <param name="actionName">getDepartmentList</param>
    </result>
</action>

Here are all the result types for Struts 2.



回答3:

Thank you all for your answers. I solved this by commenting these lines in struts.properties:

struts.ognl.logMissingProperties=true

struts.el.throwExceptionOnFailure=true

Though I still don't understand why Struts tried to find employeeName before.