admin管理员组

文章数量:1336340

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?

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?

Share Improve this question asked Aug 7, 2011 at 8:14 user829237user829237 1,7698 gold badges38 silver badges62 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

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

See: http://struts.apache/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"/>";

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.

本文标签: javaStruts2 parameter to javascriptStack Overflow