sweety
sweety

Reputation: 85

How to convert <html:select> dropdown in Struts 2

What are the possible ways to convert below Struts 1 code to migrate in Struts 2. I know <s:select> tag but how to integrate <c:foreach> JSTL in that?

<html:select property="acti" onchange="setActi(form);" style="width:45px">  
    <html:option value=""><s:text name="form.date" /></html:option>  
    <c:forEach items="${v}" var="var">  
        <c:if test="${not (var eq form.acti)}">  
            <option value="<c:out value="${var}"/>"><c:out value="${var}"/></option>  
        </c:if>  
        <c:if test="${var eq form.acti}">  
            <option selected value="<c:out value="${form.jacti}"/>"><c:out value="${form.acti}"/></option>  
        </c:if>  
    </c:forEach>  
</html:select>  

Upvotes: 2

Views: 1044

Answers (2)

Roman C
Roman C

Reputation: 1

You should remove Struts1 tags, that is enough. Struts2 can use JSTL so, you can cut your time on migration.

<select name="activite" value="<c:out value='${form.activite}'/>" onchange="setActivite(form);" style="width:45px">  
    <option value=""><s:text name="form.date.jour" /></option>  
    <c:forEach items="${jour}" var="varJour">  
        <c:if test="${not (varJour eq form.activite)}">  
            <option value="<c:out value="${varJour}"/>"><c:out value="${varJour}"/></option>  
        </c:if>  
        <c:if test="${varJour eq form.activite}">  
            <option selected value="<c:out value="${form.jactivite}"/>"><c:out value="${form.activite}"/></option>  
        </c:if>  
    </c:forEach>  
</select>  

Note, that this code supposed to use for migration purposes. If you are developing a new application you can spend more time to redesign your code with struts tags (if you really need them).

Upvotes: 1

Andrea Ligios
Andrea Ligios

Reputation: 50261

All of your code can be flattened with the same functionalities in:

<s:select name = "activite" 
          list = "jour"    
     headerKey = ""
   headerValue = "getText(form.date.jour)"
         value = "form.activite"
      onchange = "setActivite(form);" 
      cssStyle = "width:45px"
/>

So you don't need the forEach at all. But if you need it, then use <s:iterator> (struts2 equivalent tag to JSTL's <c:forEach>).

I'm assuming form.jactivite is a typo for form.activite, if not you have to tweak it a bit with OGNL. BTW you are doing a lot of strange things for a simple, straight business like feeding a selectbox (same key and value, different key and value for the selected object, ecc...)

Upvotes: 2

Related Questions