comparing two valuestack string values in JSP - struts2

ⅰ亾dé卋堺 提交于 2019-12-10 21:16:07

问题


Thanks in advance for your time.

I need to preselect a radio button if it has a saved value. I basically need to compare 2 strings in the valuestack to determine this.

(I can't use <s:radio at the moment because of some business rules I need to attach based on other input elements in the form).

I tried to do <s:set the value of the saved id inside s:iterate like below and then compare them like below but obviously I didnt get it right.

<s:set var="savedId" value="%{flag.problemId}"/> 
              <s:iterator value="problemIdList"> 
                    <s:set var="currentId" value='<s:property value="id"/>' />                   
                        <s:if test="%{#currentId.equals(#savedId)}" >
                                <input checked="checked" type="radio" name="problemId" id="problemId" value='<s:property value="id"/>'/> <s:property value="description"/> <br/>
                        </s:if>
                <s:else>
                    <input type="radio" name="problemId" id="problemId" value='<s:property value="id"/>'/> <s:property value="description"/> <br/>
                </s:else>                       
             </s:iterator>

Basically I need to compare the two strings, my code is below. I know I can't compare with equals() like I have below - any ideas?

Thanks a bunch!

<s:set var="savedId" value="%{flag.problemId}"/>  <s:iterator value="problemIdList">                                 
<s:if test=' <s:property value="id"/>.equals(<s:property value="savedId"/>) '>
    <input checked="checked" type="radio" name="problemId" id="problemId" value='<s:property value="id"/>'/>            <s:property value="description"/> <br/>
</s:if>
<s:else>
    <input type="radio" type="radio" name="problemId" id="problemId" value='<s:property value="id"/>'/>                     <s:property value="description"/> <br/>
</s:else>                       

Regards, VeeCan


回答1:


Have you tried:

<s:if test='id.equals(savedId)'>

If "id" is a string OGNL will allow you to use methods of String.




回答2:


Access value form <s:iterator> into <s:if> (ognl with <s:if> and <s:iterator>)

For example: Suppose loadUserList is the iterator(containing UserName and Address) to be iterated in jsp,

     UserName:<s:property escape="false" value="userName" />
     Address:<s:property escape="false" value="address" />
     <s:if test='userName.equals("Admin")'>This is admin User</s:if>
     <s:else>This is not an admin user!</s:else>

OGNL with s:if and s:iterator



来源:https://stackoverflow.com/questions/4996905/comparing-two-valuestack-string-values-in-jsp-struts2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!