2016-03-07 130 views
0

我試圖創建Spring MVC項目來管理我的會議。我可以瀏覽它,並且我將添加創建新的會議功能。我想在一個jsp視圖中具有這兩個功能。它看起來像這樣:如何將數據從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> 

當我試圖訪問我的控制器(本地主機:8080 /會議)即時獲取這樣的錯誤:

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. 

不應該讓我看看我的實際會議,如果我試圖傳遞一些數據,然後(如果有的話)拋出一個錯誤?我應該如何正確地工作以創建新的會議?

+0

請在您的會議課程中添加getter和setter –

回答

2

隨着你告訴Spring尋找一個meeting模型屬性行

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

。但是,您尚未將meeting添加到您的模型中。

試着改變你的getMeetings()方法是這樣的:

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

,並確保您Meeting類具有getter和setter。

相關問題