How to call an action method from JSP and pass a parameter in Struts 2

45 Views Asked by At

I have the following code in JSP that would call an action method and then pass a parameter value.

<s:param name="lastname" value="outputData.Lastname"></s:param>
<li> Your fullname is: <s:property name = "fullname" value="%{getFullname(lastname)}"/> </li>

This is how it looks like in the action method:

public String getFullname (String lastname) throws Exception {
     String firstname = someClass.getFirstname();
     return firstname + lastname;
}

Unfortunately, when I use the param name to pass the value, the passed value is null. But when I do it like the code below, it successfully passes the value.

<li> Your fullname is: <s:property name = "fullname" value="%{getFullname('Doe')}"/> </li>
1

There are 1 best solutions below

0
Roman C On

The <s:param> tag doesn't work for setting a value to the variable. You should use <s:set> tag. It will create a variable in the action scope which is used by default.

The value will be set in the page scope and Struts’ action context using the name as key

<s:set var="lastname">Doe</s:set>
<li> Your fullname is: <s:property name = "fullname" value="%{getFullname(lastname)}"/> </li>