1

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 <s:property>tag. Thats all good.

Now i also have within the jsp a section where i define a <script>. Within that script i would like to create a variable that will build a url with the above mentioned struts2 field as a value.

<script type="text/javascript">
var url = "/path/to/action?parameter1=";
</script>

How can i put the value of omradeId after the equals (=) sign? I tried using <s:property>but that did not work. Any suggestions?

2 Answers 2

4

"/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"/>";
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.