Skip to content Skip to sidebar Skip to footer

Struts2 Parameter To Javascript

I have a struts2 action with a field: private Long omradeId; The field has a getter. The action is sent to a jsp and within that jsp i can access the field using

Solution 1:

"/path/to" will change depending on the web server. To avoid this use the struts2 url tag.

See: http://struts.apache.org/2.x/docs/url.html

For an action called "action" in namespace "/" with a parameter called parameter1 having the value omradeId, you would simply say:

<s:url namespace="/" action="action">
  <param name="parameter1" value="%{omradeId}"/>
</s:url>

putting the above into the JS variable we have:

var url = "<s:url action="action"><param name="parameter1" value="%{omradeId}"/></s:url>";

Using the above will mean your application can be installed on different application servers without change.

Having formated xml is nicer than inline, if using a lot of parameters adding the var parameter to the s:url tag to give it a name and then you can reference this string in a number of places with the s:property tag would keep things clean.

<s:url namespace="/" action="action" var="myString">
  <param name="parameter1" value="%{omradeId}"/>
</s:url>

var url = "<s:property value="#myString"/>";

Solution 2:

This should work:

<script type="text/javascript">
var url = "/path/to/action?parameter1=<s:property value="omradeId">";
</script>

If not you should check if the value is not null and value is successfully set in your action class.


Post a Comment for "Struts2 Parameter To Javascript"