Struts 2 - Can I access the properties of another Activity that is not on the ValueStack?
I have two JSP pages displaying two lists from two different actions: the page A
displays the list of employees, the page B
displays the list of departments.
Both pages share a common text box (included from the third JSP page) at the top for finding employees by name:
<s:form action="searchEmployeesByName">
<s:textfield name="employeeName" />
<s:submit>
</s:form>
The search action is part of the class EmployeeAction
and I can load the page A
and search without issue. However, when loading the page, B
I ran into ognl.NoSuchPropertyException
because the property is employeeName
not on ValueStack
DepartmentAction
.
How can I solve this problem? Are there ways to access employeeName
from EmployeeAction
from from DepartmentAction
? Or how should I refactor my activities to perform a general search function?
Here is my action config 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>
source to share
Actions are created on demand and do not exchange context because they are local to their thread. If you want a property set by an action, then you must specify it with a parameter in the url, or take it from the session. You must create getters and setters for the property you want to pass. Typically, transfer parameters executed with a tag param
can be used to parameterize other tags.
In your case, you can use a tag param
in the result config to create a dynamic parameter
<result name="searchEmployeesByName" type="redirectAction">
<param name="actionName">department</param>
<param name="employeeName">${employeeName}</param>
</result>
source to share
ognl.NoSuchPropertyException is thrown when a property tries to retrieve from an object that does not have such a property.
Thus, no getter and setter methods can be created for your OGNL expression in your corresponding Action class.
you can use result type chaining (not recommended) in your result tag to access the properties of one activity in another.
you can also use the redirectAction result type.
<action name="getEmployeeList" class="...">
<!-- Chain to another namespace -->
<result type="chain">
<param name="actionName">getDepartmentList</param>
</result>
</action>
Here are all result types for Struts 2.
source to share