简体   繁体   中英

How to populate Struts2 dropdown menu?

I am using Struts2 and I am trying to populate the drop down using <s:select> tag but for some reason I don't see anything in the drop down. In the back end the list wsAuditConfig is getting populated with the values but it is not showing up in the drop down.

Action

private List<String> wsAuditConfig;

public void prepare() throws Exception {
    AppConfigClient appConfigClient = ( AppConfigClient ) AppContext.getBean( Constants.SPRING_BEAN_CMS_APP_CONFIG_CLIENT );
    String appCode = ( String ) System.getProperty( Constants.SPRING_BEAN_EIS_APP_CODE );

    List<AppConfig> list = new ArrayList<>();
    wsAuditConfig = new ArrayList<>();

    GetAppConfigServiceRequest request = appConfigClient.getGetAppConfigServiceRequest();
    request.setParameters( appConfigClient.getAppConfigRequestParameters( appCode, null ) );
    request.setAccepts( ContentType.JSON );

    GetAppConfigServiceResponse response = appConfigClient.get( request );
    if( response != null && response.getEntity() != null ) {
        list = response.getEntity().getConfig();
        for( AppConfig appConfig : list ) {
            if( appConfig.getConfigCode().equals( Constants.APP_WS_AUDIT_CONFIG ) ) {
                wsAuditConfig.add( appConfig.getKeyName1() );
            }
        }
    }
    this.records = new ArrayList<>();
}

JSP

<s:form action="viewWSAuditXml" namespace="/eismain/cmswsaudit/view" name="wsAudit">
    <s:select name="wsName" headerValue="" headerKey="" list="wsAuditConfig" listKey="wsName" listValue="wsName" key="ws.audit.view.select" onchange="wsAudit.submit();" cssStyle="min-width: 220px;"/>
</s:form>

What is wsName ? Since you have list of String change select as following and it shold work.

<s:select name="wsName" headerValue="" headerKey="" list="wsAuditConfig"   onchange="wsAudit.submit();" cssStyle="min-width: 220px;"/>

The dropdown should be populated in the prepare() method, for which the action class should implement Preparable and there should be prepare interceptor in the action config.

A getter is required

private List<String> wsAuditConfig;
public List<String> getWsAuditConfig() { return wsAuditConfig; }

Since it's a list of strings the element of the list doesn't have a key or value properties. So it could be used without listKey and listValue .

<s:form action="viewWSAuditXml" namespace="/eismain/cmswsaudit/view" name="wsAudit">
    <s:select name="wsName" headerValue="" headerKey="" list="wsAuditConfig" key="ws.audit.view.select" onchange="wsAudit.submit();" cssStyle="min-width: 220px;"/>
</s:form>

If you want to list objects in the dropdown you should use typed list and the key and value provided similar how in this answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM