gpt4 book ai didi

java - 如何将数据从jsp表单传递到 Controller

转载 作者:行者123 更新时间:2023-12-02 03:51:25 26 4
gpt4 key购买 nike

我正在尝试创建 Spring MVC 项目来管理我的 session 。我可以浏览它,并且我将添加创建新 session 的功能。我想在一个 jsp View 中同时拥有这两种功能。它看起来像这样:

实体:

@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 类:

@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 View (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>

当我尝试访问我的 Controller (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.

它不应该向我显示我的实际 session ,并且如果我尝试传递一些数据(如果有的话)会抛出错误吗?我应该如何正确地创建新 session ?

最佳答案

用线

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

你告诉Spring寻找 session 模型属性。但是,您尚未将 session 添加到您的模型中。

尝试像这样更改您的 getMeetings() 方法:

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

并确保您的 Meeting 类具有 getter 和 setter。

关于java - 如何将数据从jsp表单传递到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35841107/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com