how to use jsp variable in struts 2 tag
I am wondering if there is any way to use jsp variable in struts 2 tags.
I tried as given below but i did not work.
Suggest me if there is any way to do this.
<%@ taglib prefix="s" uri="/struts-tags" %>
<% for(int i=0;i<10;i++)
{
%>
<s:textarea name=%{i} value=%{i}></s:textarea>
<%开发者_运维百科}%>
please help me.
For any question involving struts2 tags please see: https://struts.apache.org/tag-developers/tag-reference.html
Note the begin and end properties as follows
<s:iterator var="counter" begin="0" end="9" >
<s:textfield name="%{#counter}" value="%{#counter}" /><br/>
</s:iterator>
will produce the desired result.
Edit: just reading your comment to leonbloy you would not use the above, but rather iterate over the collection. Provide a getter which returns your collection. ie:
public List<String> getCollection(){return this.collection};
Then in the jsp you would write:
<s:iterator value="collection">
<s:property/><br/>
</s:iterator>
The above will print out all the strings in the collection.
Did you try this?
<s:property value="%{#i}" />
Read the docs. BTW, that code smells bad, you should try to not mix struts2 tags with JSP Scriptlets (in general -struts2 or anything- you'd try to avoid JSP Scriptlets <% ... %>
as much as possible.). Read about the iterator tag.
精彩评论