在Struts2中填充下拉列表

时间:2016-11-14 14:16:05

标签: struts2

我正在使用struts2来实现我的应用程序。在我的应用程序中,我必须实现两个下拉,第二个下拉的值取决于第一次下拉。我从一个例子中得到了参考并相应地实现了我的代码,但仍然没有得到结果。请提出您的建议,以使此代码有效。

SupporterAction.java

package action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import entityBean.TicketDataBean;
import entityBean.UserBean;
import entityListener.SupporterListener;
import entityManager.Application;

@SuppressWarnings("serial")
public class SupporterAction  extends ActionSupport{

private String application;
private String number;
private List<String> applicationNames;
List<String> ticketNumber;

public String getNumber() {
    return number;
}
public void setNumber(String number) {
    this.number = number;
}

public List<String> getApplicationNames() {
    return applicationNames;
}
public void setApplicationNames(List<String> applicationNames) {
    this.applicationNames = applicationNames;
}

public List<String> getTicketNumber() {
    return ticketNumber;
}
public void setTicketNumber(List<String> ticketNumber) {
    this.ticketNumber = ticketNumber;
}

public String getApplication() {
    return application;
}

public void setApplication(String application) {
    this.application = application;
}

 public String getJSON() {
        return execute();
    }

@SuppressWarnings("unchecked")
public String execute() 
{
    @SuppressWarnings("rawtypes")
    Map session = ActionContext.getContext().getSession();
    System.out.println(session.get("currentSessionUser"));
    applicationNames = new ArrayList<String>();
    UserBean userBean = (UserBean)session.get("currentSessionUser");
    List<Application> applicationObj = userBean.getApplication();

for (Application obj : applicationObj)
  {

     System.out.println(obj.getApplicationName());
     applicationNames.add(obj.getApplicationName());
  }
    session.put("ApplicationNames",applicationNames);

    System.out.println("Hello");
    System.out.println(application);
     if(application != null)
        {
         ticketNumber=new ArrayList<String>();
         System.out.println("Hello 1");
             SupporterListener sl = new SupporterListener();
             List<TicketDataBean> tdbList =        sl.getPendingTickets(application);

             if(!tdbList.isEmpty())
             {

                for(TicketDataBean td : tdbList)
                {
                    ticketNumber.add(td.getNumber());
                }
             session.put("logined","true");
             session.put("TicketNumber",ticketNumber);

             }
        }
    return "success";
}
}

Supporter.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"   "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<s:form id="formSelectReload" theme="simple" cssClass="yform"  action="getApplicationList">
        <fieldset>

            <div class="type-text">
                <label for="application">Application : </label>
                <s:url var="remoteurl" action="getApplicationList" namespace="/"/>
                <sj:select href="%{remoteurl}" id="application"  name="application"
                            list="applicationNames" onChange="reloadsecondlist" emptyOption="false"
                            headerKey="-1" headerValue="Please Select a Application" />
            </div>
            <s:property value="#session.ApplicationNames"/>

            <div class="type-text">
                <label for="number">Ticket Number: </label>
                <s:url var="remoteurl" action="getApplicationList" namespace="/"/>
                 <sj:select href="%{remoteurl}" id="ticketNumber"  formIds="formSelectReload"
                            reloadTopics="reloadsecondlist" name="number" 
                            list="ticketNumber"
                            emptyOption="false"
                            headerKey="-1" headerValue="Please Select a Ticket Number"/>

                            </div>
        <div class="type-button">
            <sj:submit 
                id="submitFormSelectReload"
                />

            </div>          


        </fieldset>
    </s:form>
</body>
</html>

struts.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
 <package name="login" extends="struts-default">


  <action name="login" 
  class="action.LoginAction" 
  method="execute">
   <result name="success">/Home.jsp</result>
   <result name="error">/InvalidLogin.jsp</result>
   <result name="INVALID SESSION">/Home.jsp</result>
    </action>

    <action name="logout" 
  class="action.LoginAction" 
  method="logout">
   <result name="success">/Login.jsp</result>          
  </action>

  <action name="register">
  <result>/Register.jsp</result></action>

  <action name="importexcel">
  <result>/ImportExcel.jsp</result></action>

  <action name="supporter" class="action.SupporterAction" 
  method="execute">
  <result name="success">/Supporter.jsp</result></action>

  <action name="registeruser" 
  class="action.RegisterAction" 
  method="register">
   <result name="success">/UserLogged.jsp</result>
   <result name="error">/InvalidLogin.jsp</result>
   <result name="INVALID SESSION">/Register.jsp</result>
    </action>


     <action name="importExcel" 
  class="action.ImportExcelAction" 
  method="importExcel">
   <result name="success">/Home.jsp</result>
   <result name="error">/InvalidLogin.jsp</result>
   <result name="INVALID SESSION">/Home.jsp</result>
    </action>




</package>
<package name="default" extends="struts-default,json-default" namespace="/">
<action name="getApplicationList" 
  class="action.SupporterAction" >
  <result type="json" />
    </action>



    </package>
   </struts>

jar文件

Image of jar files

我得到的结果是

Image of result

正如我们在结果图中看到的那样,列表正在填充,但我无法在下拉列表中获取它。请提出建议。

提前感谢您的回答。

1 个答案:

答案 0 :(得分:0)

prepare()拦截器是你的朋友。

https://struts.apache.org/docs/prepare-interceptor.html