select drop down in struts2, migrating from struts1 to struts2

319 Views Asked by At

Below is Struts1 code snippet from jsp

<logic:notEmpty name="user" property="myLanguages" scope="session">
                       <html:select name="user" property="defaultLanguage">
                         <html:optionsCollection label="languageName" name="user" property="myLanguages" value="languageCode"/>
                       </html:select>
</logic:notEmpty>

Below is Struts2 (2.5.16 version) code snippet from jsp which is not working

<s:if test="%{#session.user.myLanguages != null && #session.user.myLanguages != ''}">
                          <s:select list="{#session.user.myLanguages}"
                                      listKey="languageCode" listValue="languageName"
                                      name="user"  value="defaultLanguage"/>
</s:if>

Below is the scriplet code from jsp

<%
User user = (User) session.getAttribute("user");
Vector vMyLang = user.getMyLanguages();
        System.out.println("logon.jsp:vMyLang--- "+vMyLang);
        if(null != vMyLang){
            System.out.println("logon.jsp:vMyLang-----  "+vMyLang.size());//here output(i.e size) is 20
        }
%>

So values exists in the session but i'm not able to get using strut2 tags, there are no error/exception in the logs

Struts1 code is working fine, Struts2 code is not working(i mean drop down is not shown in the UI)

Please help me out what mistake i have done.

2

There are 2 best solutions below

0
learning knowledge On BEST ANSWER

Fixed with below piece of code

<s:if test="%{#session.user.myLanguages != null}">
                          <s:select list="%{#session.user.myLanguages}"
                                      listKey="languageCode" listValue="languageName"
                                      value="%{#session.user.defaultOperaLanguage}/>
</s:if>
7
Dave Newton On

The JSP and scriptlet are not equivalent.

In your scriptlet you're calling a method named myLanguages().

In your <s:if /> tag you're attempting to call a method named getMyLanguages().

Since you're not following JavaBean naming conventions you cannot use the syntactic sugar OGNL provides and you'll need to actually call the method.

Note: You should follow JavaBean naming conventions; they're a convention for a reason. Any Java that relies on reflection, like OGNL, requires it.