我有一个需要使用 Struts 2
<s:select>
标签填充的下拉列表。
<s:select label="Country" headerKey="-1" headerValue="Select Country" list="countries"
listKey="key" listValue="label" name="searchForm.custCountry"/>
在我的操作类中,我有以下声明,后跟 getter 和 setter。
ArrayList<DropDown> countries = new ArrayList<DropDown>();
我遇到了以下异常。
错在哪里?
SEVERE: Servlet.service() for servlet jsp threw exception
tag 'select', field 'list', name 'searchForm.custCountry': The requested list key 'countries' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
at org.apache.struts2.components.Component.fieldError(Component.java:240)
at org.apache.struts2.components.Component.findValue(Component.java:333)
at org.apache.struts2.components.ListUIBean.evaluateExtraParams(ListUIBean.java:80)
at org.apache.struts2.components.Select.evaluateExtraParams(Select.java:105)
at org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:902)
at org.apache.struts2.components.UIBean.end(UIBean.java:544)
at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
at org.apache.jsp.accountSearchDtls_jsp._jspx_meth_s_005fselect_005f0(accountSearchDtls_jsp.java:979)
at org.apache.jsp.accountSearchDtls_jsp._jspx_meth_s_005fdiv_005f0(accountSearchDtls_jsp.java:935)
at org.apache.jsp.accountSearchDtls_jsp._jspService(accountSearchDtls_jsp.java:521)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:745)
这是我获取下拉列表数据的方法。我想这已经足够了,如果不让我知道的话。
public ArrayList<DropDown> getCountrydd() {
ArrayList<DropDown> countrydd = new ArrayList<DropDown>();
try {
OraConn conn = new OraConn();
conn.getConnection();
cstmt = conn.setProc("call mgm.getCountry(?)");
cstmt.registerOutParameter(1, OracleTypes.CURSOR);
cstmt.execute();
rs = (OracleResultSet)cstmt.getObject(1);
countrydd = rsToDropDown(rs);
} catch (Exception e) {
e.printStackTrace();
//log.error("***getDSdropdowns*** ");
} finally {
closeORAConnection();
}
return countrydd;
}
您需要更改代码来填充列表
public ArrayList<DropDown> getCountrydd() {
List<DropDown> countrydd = new ArrayList<DropDown>();
OraConn oraConn = new OraConn();
Connection conn = null;
CallableStatement cstmt = null;
ResultSet rs = null;
try {
conn = oraConn.getConnection();
cstmt = conn.prepareCall("{call mgm.getCountry(?)}");
cstmt.registerOutParameter(1, OracleTypes.CURSOR);
cstmt.executeUpdate();
rs = (ResultSet)cstmt.getObject(1);
rsToDropDown(rs, countrydd);
} catch (Exception e) {
e.printStackTrace();
//log.error("***getDSdropdowns*** ");
} finally {
if (rs != null)
rs.close();
if (cstmt!= null)
cstmt.close();
if (conn != null)
conn.close();
}
return countrydd;
}
我发现即使用户为空,我也会用国家/地区值填充组合框或下拉列表,因此,当我单击“创建新用户”按钮时,我得到了这些异常。现在我更改了代码,这就是现在的样子:
public String getProfile() throws Exception {
user_name =(String) sessionMap.get("userid") ;
MgmService service = new MgmService();
userForm = new User();
userForm = service.getUsersProfile(user_name);
if(searched_user_name!=null){
userForm = service.getUsersProfile(searched_user_name);
countries= service.getCountrydd(); //This keep the content of the combobox by default on the list
return "createEditUser";
}
else if(userForm!=null)
{
return "success";
}
else {
addActionError("Invalid Login.");
return "success";
}
}
这是我之前的错误代码......
public String getProfile() throws Exception {
user_name =(String) sessionMap.get("userid") ;
MgmService service = new MgmService();
userForm = new User();
userForm = service.getUsersProfile(user_name);
countries= service.getCountrydd(); //This keep the content of the combobox by default on the list
if(searched_user_name!=null){
userForm = service.getUsersProfile(searched_user_name);
return "createEditUser";
}
else if(userForm!=null)
{
return "success";
}
else {
addActionError("Invalid Login.");
return "success";
}
}