سؤال

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

هل كانت مفيدة؟

المحلول

Have you tried:

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

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

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top