开发者

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

Now i also have within the jsp a section where i define a <script>. Within that script开发者_如何学JAVA 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?


"/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"/>";


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜