我正在尝试创建一个包含所有用户报告下拉列表的 Spring 表单,但 jsp 页面将我的对象绑定解释为字符串。我已经为此苦苦挣扎了一段时间,目前正在使用内联脚本来打印结果,但更喜欢使用对象绑定。
视图.jsp
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@page
import="com.cognos.developer.schemas.bibus._3.Report, java.util.*"%>
<form:form method="POST" modelAttribute="reportView" action="">
<form:label path="defaultName.value">Name</form:label>
<form:input path="defaultName.value"/><br/>
<form:label path="base">Report Name</form:label>
<form:select path="base">
<form:options items="${reports}" itemValue="defaultName" itemLabel="defaultName" />
<%
//List<Report> reportList = (List)request.getAttribute("reports");
//for (Report r : reportList) {
// out.print("<option value=");
// out.print(r);
// out.println(">" + r +"</option>");
//}
%>
</form:select>
<form:label path="disabled.value">Disabled?</form:label>
<form:checkbox path="disabled.value"/>
<form:hidden path="searchPath.value" />
<div class="input-group-btn">
<input class="btn btn-default" type="submit" value="Update" />
</div>
</form:form>
控制器.java
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.servlet.ModelAndView;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import com.cognos.developer.schemas.bibus._3.Report;
@RequestMapping(value="/new", method=GET)
public String newView (@RequestParam("usr")String usr, Map<String, Object> model) {
//public String newView(){
cognos.checkConfig();
cognos.connectToCognos();
cognos.logonToCognosAsUsr(usr);
ReportView rv = new ReportView();
model.put("reportView", rv);
Map<String, String> map = new HashMap<String, String>();
map.put("STILL DOES NOT", "WORK");
model.put("reports", map);
//model.put("reports", cognos.fetchReports());
cognos.logoffFromCognos();
return "new_view";
}
CognosService.java
public Map<String, Report> fetchReports() {
BaseClass bc[] = null;
String searchPath = "/content//report";
List<Report> rtn = new ArrayList<Report>();
try {
SearchPathMultipleObject spMulti =
new SearchPathMultipleObject(searchPath);
bc = cmService.query(spMulti, props, new Sort[] {}, new QueryOptions());
} catch (Exception e) {
e.printStackTrace();
}
if (bc != null) {
for (int i = 0; i < bc.length; i++) {
Report r = (Report)bc[i];
rtn.add(r);
}
}
Collections.sort(rtn, new ReportComparator());
for (Report rp : rtn){
reportList.put(rp.getStoreID().getValue().toString(), rp);
}
return reportList;
}
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Report Scheduler</display-name>
<servlet>
<servlet-name>views</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>views</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>views/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
意见-servlet.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.enernoc.reporting.ReportScheduler" />
<!-- <mvc:resources mapping="/resources/**" location="/resources/" /> -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix">
<value>/views/</value>
</property>
<property name="suffix" value=".jsp" />
</bean>
</beans>
堆栈跟踪
org.apache.jasper.JasperException: An exception occurred processing JSP page /views/new_view.jsp at line 13
10: <form:input path="defaultName.value"/><br/>
11: <form:label path="base">Report Name</form:label>
12: <form:select path="base">
13: <form:options items="${reports}" itemValue="defaultName" itemLabel="defaultName" />
14: </form:select>
15: <form:label path="disabled.value">Disabled?</form:label>
16: <form:checkbox path="disabled.value"/>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause
javax.servlet.ServletException: javax.servlet.jsp.JspException: Type [java.lang.String] is not valid for option items
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
org.apache.jsp.views.new_005fview_jsp._jspService(new_005fview_jsp.java:103)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause
javax.servlet.jsp.JspException: Type [java.lang.String] is not valid for option items
org.springframework.web.servlet.tags.form.OptionWriter.writeOptions(OptionWriter.java:143)
org.springframework.web.servlet.tags.form.OptionsTag.writeTagContent(OptionsTag.java:157)
org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
org.apache.jsp.views.new_005fview_jsp._jspx_meth_form_005foptions_005f0(new_005fview_jsp.java:335)
org.apache.jsp.views.new_005fview_jsp._jspx_meth_form_005fselect_005f0(new_005fview_jsp.java:296)
org.apache.jsp.views.new_005fview_jsp._jspx_meth_form_005fform_005f0(new_005fview_jsp.java:144)
org.apache.jsp.views.new_005fview_jsp._jspService(new_005fview_jsp.java:93)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)