如何将数据从jsp表单传递给控制器

时间:2016-03-07 10:18:50

标签: java spring jsp spring-mvc

我正在尝试创建Spring MVC项目来管理我的会议。我可以浏览它,我将添加创建新的会议功能。我想在一个jsp视图中有两个功能。它看起来像这样:

实体:

@Entity
public class Meeting {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private Date addedDate;
    private Date meetingDate;
    private String notes;
    private String place; 
}

我的控制器类:

@Controller
@RequestMapping("/meetings")
public class MeetingController {

    @Autowired
    private MeetingRepository meetingRepository;

    @RequestMapping(method = RequestMethod.GET)
    public String getMeetings(Model model) {
        model.addAttribute("meetings", meetingList());
        return "meetings";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String setMeeting(@ModelAttribute(value = "meeting") Meeting meeting, Model model) {
        meeting.setAddedDate(new Date());
        meeting.setMeetingDate(new Date(System.currentTimeMillis() + 604800000));
        meetingRepository.save(meeting);

        model.addAttribute("meetings", meetingList());
        return "meetings";
    }

    private List<Meeting> meetingList() {
        return meetingRepository.findAll();
    }
}

我的jsp视图(meeting.jsp)

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Meetings application - Szymon Nowak</title>
    <style>
        tr:first-child{
            font-weight: bold;
            background-color: #C6C9C4;
        }
    </style>
</head>
<body>
    <h2>List of Meetings</h2>
    <table>
        <tr>
            <td>ID</td>
            <td>Added Date</td>
            <td>Meeting Date</td>
            <td>Notes</td>
            <td>Place</td>
            <%--<td>Delete</td>--%>
        </tr>
        <c:forEach items="${meetings}" var="meeting">
            <tr>
                <td>${meeting.id}</td>
                <td>${meeting.addedDate}</td>
                <td>${meeting.meetingDate}</td>
                <td>${meeting.notes}</td>
                <td>${meeting.place}</td>
                <%--<td><a href="<c:url value='/delete-${employee.ssn}-employee' var="meeting.id" />">delete</a></td>--%>
            </tr>
        </c:forEach>
    </table>
    <br/>
    <br/>
    <h2>Add meeting</h2>
    <form:form method="post" action="meetings" commandName="meeting">
        <table>
            <tr>
                <td>Notes</td>
                <td><form:input path="notes" /></td>
            </tr>
            <tr>
                <td>Place</td>
                <td><form:input path="place" /></td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="send"/>
                </td>
            </tr>
        </table>
    </form:form>



</body>
</html>

当我尝试访问我的控制器(localhost:8080 / meetings)时,我得到这样的错误:

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/views/meetings.jsp at line 50

47:         <table>
48:             <tr>
49:                 <td>Notes</td>
50:                 <td><form:input path="notes" /></td>
51:             </tr>
52:             <tr>
53:                 <td>Place</td>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:471)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    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:968)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'meeting' 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.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:141)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:132)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:116)
    org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
    org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
    org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
    org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
    org.apache.jsp.WEB_002dINF.views.meetings_jsp._jspx_meth_form_005finput_005f0(meetings_jsp.java:319)
    org.apache.jsp.WEB_002dINF.views.meetings_jsp._jspx_meth_form_005fform_005f0(meetings_jsp.java:270)
    org.apache.jsp.WEB_002dINF.views.meetings_jsp._jspService(meetings_jsp.java:162)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    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:968)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.30 logs.

它不应该告诉我我的实际会议,如果我尝试传递一些数据然后(如果有的话)抛出错误?我应该如何正确地工作以创建新会议?

1 个答案:

答案 0 :(得分:2)

使用

<form:form method="post" action="meetings" commandName="meeting">

你告诉Spring寻找meeting模型属性。但是,您尚未向模型添加meeting

尝试更改getMeetings()方法,如下所示:

@RequestMapping(method = RequestMethod.GET)
public String getMeetings(Model model) {
    model.addAttribute("meetings", meetingList());
    model.addAttribute("meeting", new Meeting());
    return "meetings";
}

并确保您的Meeting课程有吸气剂和制定者。