动态创建的表单,具有不同数量的输入

时间:2018-01-26 19:10:00

标签: java spring jsp spring-mvc

我想根据HashMap中传递给视图的项目在jsp中创建表单。

首先,我创建了一个表单,它给了我一个项目的HashMap,然后以第二种形式,我希望让用户有机会更改第一个表单操作的结果。在第二种形式中,我的控制器设置了HashMap属性,并且我尝试过这样的事情:

控制器:

package com.capc.controller;

import com.capc.model.ConferenceTimetable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.servlet.ModelAndView;

@Controller
public class CapcController {

    @RequestMapping(value = "/conferenceAdvertParser", method = RequestMethod.GET)
    public ModelAndView showConferenceAdvertParser() {
        return new ModelAndView("conferenceAdvertParser", "command", new ConferenceTimetable());
    }

    @RequestMapping(value = "/parseConferenceAdvert", method = RequestMethod.POST)
    public String addEmployee(@ModelAttribute("SpringWeb") ConferenceTimetable ct, ModelMap model) {
        ct.parseConferenceAdvert();
        model.addAttribute("conferenceTimetable", ct.getConferenceTimetable());
        return "conferenceEventEditor";
    }
}

第二个JSP表单:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
<head>
    <title>Title</title>
    <meta name="viewport" content="initial-scale=1, maximum-scale=1">
    <link rel='stylesheet' href='webjars/bootstrap/3.2.0/css/bootstrap.min.css'>

</head>
<body>

<form:form method="POST" action="/">
    <table border="1">
        <c:forEach items="${conferenceTimetable}" var="entry">
            <tr>
                <td><form:label path="${entry.key}">test label</form:label></td>
                <td><form:input path="${entry.key}"></form:input></td>
            </tr>
        </c:forEach>
        <tr>
            <td colspan="2">
                <input type="submit" value="Submit"/>
            </td>
        </tr>
    </table>
</form:form>
</body>
</html>

创建第二个JSP页面时,我遇到了错误。我没有任何其他想法如何做到这一点,任何想法?

错误:

HTTP Status 500 – Internal Server Error
Type Exception Report

Message An exception occurred processing JSP page [/conferenceEventEditor.jsp] at line [30]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: An exception occurred processing JSP page [/conferenceEventEditor.jsp] at line [30]

27: 
28: 
29:             <tr>
30:             <td><form:label path="a">test label</form:label></td>
31:             <td><form:input path="a" value="dupa"></form:input></td>
32:             <%--<td>${entry.value.eventDate}</td>--%>
33:             <%--<td>${entry.value.eventDescription}</td>--%>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    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:1243)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
    org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
    org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:130)
    org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:120)
    org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:90)
    org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
    org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
    org.apache.jsp.conferenceEventEditor_jsp._jspx_meth_form_005flabel_005f0(conferenceEventEditor_jsp.java:346)
    org.apache.jsp.conferenceEventEditor_jsp._jspx_meth_c_005fforEach_005f0(conferenceEventEditor_jsp.java:291)
    org.apache.jsp.conferenceEventEditor_jsp._jspx_meth_form_005fform_005f0(conferenceEventEditor_jsp.java:206)
    org.apache.jsp.conferenceEventEditor_jsp._jspService(conferenceEventEditor_jsp.java:152)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    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:1243)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Note The full stack trace of the root cause is available in the server logs.

Apache Tomcat/8.5.24

1 个答案:

答案 0 :(得分:0)

最后我将JSP改为百里香。您可以通过设置

之类的参数来生成该类型的字段
th:field="*{conferenceTimetableMap[__${event.key}__]}"