在我的情况下,Web服务器返回HTTP状态500,请参阅下面的完整堆栈。
方法返回视图时,方法editWorker
发生错误
@RequestMapping(value=("/worker/edit/{workerId}"), method = RequestMethod.GET)
public String editWorker(@PathVariable("workerId") Long workerId, Model model) {
Worker worker = workerService.getWorkerById(workerId);
model.addAttribute(worker);
return "db_editor"; //error occurs here
}
如果在使用第二种形式(.. modelAttribute =“tabel”..)时不使用spring标签,则没有错误。
请告诉我,出了什么问题?
jsp文件:
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags" %>
<div id="editor">
<div id="f_ed">
<sf:form method="post" action="/db_editor/worker/add" modelAttribute="worker">
<sf:hidden path="id"/>
<fieldset>
<table>
<tr>
<td><sf:label path="fam">First name</sf:label></td>
<td><sf:input path="fam" size="30"/><br/>
<sf:errors path="fam" cssClass="error" />
</td>
<td>Табель</td>
</tr>
<tr>
<td><sf:label path="imj">Last name</sf:label></td>
<td><sf:input path="imj" size="30"/><br/>
<sf:errors path="imj" cssClass="error" />
</td>
</tr>
<tr>
<td><sf:label path="otch">Middle name</sf:label></td>
<td><sf:input path="otch" size="30"/><br/>
<sf:errors path="otch" cssClass="error" />
</td>
</tr>
<tr>
<td></td>
<td><input name="commit" type="submit" value="Commit" />
</td>
</tr>
</table>
</fieldset>
</sf:form>
</div>
<div id="tab_ed">
<sf:form method="post" action="/db_editor/tabel/add" modelAttribute="tabel">
<sf:input path="d" type="date" />
<sf:input path="d_type" size="2"/>
<input name="commit" type="submit" value="Add">
</sf:form>
</div>
<c:if test="${!empty workerList}">
<div id = "r_ed">
<table>
<tr>
<th>Name</th>
<th>BD</th>
</tr>
<c:forEach var="worker" items="${workerList}">
<s:url value="db_editor/worker/edit/${worker.id}" var="edit_url" />
<tr>
<td>${worker.fam}</td>
<td>${worker.imj}</td>
<td>${worker.otch}</td>
<td><fmt:formatDate value="${worker.dbirth}" pattern="dd.MM.yyyy" /> </td>
<td><a href="${edit_url}">Edit</a></td>
<td><a href="db_editor/worker/delete/${worker.id}">Delete</a></td>
</tr>
</c:forEach>
</table>
</div>
</c:if>
</div>
控制器:
import ..
@Controller
@RequestMapping("/db_editor")
public class DBEditorController_test {
private WorkerService workerService;
private TabelService tabelService;
@Inject
public DBEditorController_test(WorkerService workerService, TabelService tabelService) {
this.workerService = workerService;
this.tabelService = tabelService;
}
@RequestMapping(method = RequestMethod.GET)
public String getWorkers(Model model) {
model.addAttribute("worker", new Worker());
model.addAttribute("tabel", new Tabel());
model.addAttribute("workerList", this.workerService.listWorker());
return "db_editor";
}
@RequestMapping(value = "/worker/add", method = RequestMethod.POST)
public String addWorkerFromForm(Worker worker) {
workerService.addWorker(worker);
return "redirect:/db_editor";
}
@RequestMapping("/worker/delete/{workerId}")
public String delWorker(@PathVariable("workerId") Long workerId) {
workerService.deleteWorker(workerId);
return "redirect:/db_editor";
}
@RequestMapping(value=("/worker/edit/{workerId}"), method = RequestMethod.GET)
public String editWorker(@PathVariable("workerId") Long workerId, Model model) {
Worker worker = workerService.getWorkerById(workerId);
model.addAttribute(worker);
return "db_editor";
}
@RequestMapping(value = "/tabel/add", method = RequestMethod.POST)
public String addOneDayForTabel (Tabel tabel) {
tabel.setWorker( workerService.getWorkerById( 2L ) );
tabelService.addTabel( tabel );
return "redirect:/db_editor";
}
}
模型:
Worker.java
@Entity
@Table(name="workers")
public class Worker {
@Id
@Column(name = "id")
@GeneratedValue
private Long id;
@Column(name = "fam")
private String fam;
@Column(name = "imj")
private String imj;
@Column(name = "otch")
private String otch;
@Column(name = "dbirth")
private Date dbirth;
/*getters and setters*/
}
Tabel.java
@Entity
@Table(name="tabel")
public class Tabel {
@Id
@Column(name = "id")
@GeneratedValue
private Long id;
@Column(name = "d")
private Date d;
@Column(name = "d_type")
private String d_type;
@ManyToOne
@JoinColumn(name="worker_id")
private Worker worker;
/*getters and setters*/
}
Full StackTrace:
HTTP Status 500 - Request processing failed; nested exception is org.apache.tiles.impl.CannotRenderException: ServletException including path '/WEB-INF/views/main_template.jsp'.
type Exception report
message Request processing failed; nested exception is org.apache.tiles.impl.CannotRenderException: ServletException including path '/WEB-INF/views/main_template.jsp'.
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.apache.tiles.impl.CannotRenderException: ServletException including path '/WEB-INF/views/main_template.jsp'.
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
root cause
org.apache.tiles.impl.CannotRenderException: ServletException including path '/WEB-INF/views/main_template.jsp'.
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:680)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:633)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:322)
org.springframework.web.servlet.view.tiles2.TilesView.renderMergedOutputModel(TilesView.java:124)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
root cause
org.apache.tiles.util.TilesIOException: ServletException including path '/WEB-INF/views/main_template.jsp'.
org.apache.tiles.servlet.context.ServletUtil.wrapServletException(ServletUtil.java:232)
org.apache.tiles.servlet.context.ServletTilesRequestContext.forward(ServletTilesRequestContext.java:243)
org.apache.tiles.servlet.context.ServletTilesRequestContext.dispatch(ServletTilesRequestContext.java:222)
org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44)
org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:103)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:659)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:678)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:633)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:322)
org.springframework.web.servlet.view.tiles2.TilesView.renderMergedOutputModel(TilesView.java:124)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
root cause
javax.servlet.ServletException: org.apache.tiles.jsp.taglib.TilesJspException: IO Error executing tag: JSPException including path '/WEB-INF/views/db_editor.jsp'.
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:906)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838)
org.apache.jsp.WEB_002dINF.views.main_005ftemplate_jsp._jspService(main_005ftemplate_jsp.java:223)
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:443)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
org.apache.tiles.servlet.context.ServletTilesRequestContext.forward(ServletTilesRequestContext.java:241)
org.apache.tiles.servlet.context.ServletTilesRequestContext.dispatch(ServletTilesRequestContext.java:222)
org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44)
org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:103)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:659)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:678)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:633)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:322)
org.springframework.web.servlet.view.tiles2.TilesView.renderMergedOutputModel(TilesView.java:124)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
root cause
org.apache.tiles.util.TilesIOException: JSPException including path '/WEB-INF/views/db_editor.jsp'.
org.apache.tiles.servlet.context.ServletUtil.wrapServletException(ServletUtil.java:232)
org.apache.tiles.jsp.context.JspTilesRequestContext.include(JspTilesRequestContext.java:105)
org.apache.tiles.jsp.context.JspTilesRequestContext.dispatch(JspTilesRequestContext.java:96)
org.apache.tiles.renderer.impl.UntypedAttributeRenderer.write(UntypedAttributeRenderer.java:61)
org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:103)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:659)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:337)
org.apache.tiles.jsp.taglib.InsertAttributeTag.render(InsertAttributeTag.java:234)
org.apache.tiles.jsp.taglib.InsertAttributeTag.render(InsertAttributeTag.java:211)
org.apache.tiles.jsp.taglib.RenderTag.doEndTag(RenderTag.java:220)
org.apache.jsp.WEB_002dINF.views.main_005ftemplate_jsp._jspx_meth_t_005finsertAttribute_005f0(main_005ftemplate_jsp.java:271)
org.apache.jsp.WEB_002dINF.views.main_005ftemplate_jsp._jspService(main_005ftemplate_jsp.java:159)
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:443)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
org.apache.tiles.servlet.context.ServletTilesRequestContext.forward(ServletTilesRequestContext.java:241)
org.apache.tiles.servlet.context.ServletTilesRequestContext.dispatch(ServletTilesRequestContext.java:222)
org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44)
org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:103)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:659)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:678)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:633)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:322)
org.springframework.web.servlet.view.tiles2.TilesView.renderMergedOutputModel(TilesView.java:124)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'tabel' available as request attribute
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:160)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:147)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:138)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:122)
org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:408)
org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:140)
org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
org.apache.jsp.WEB_002dINF.views.db_005feditor_jsp._jspx_meth_sf_005finput_005f3(db_005feditor_jsp.java:630)
org.apache.jsp.WEB_002dINF.views.db_005feditor_jsp._jspx_meth_sf_005fform_005f1(db_005feditor_jsp.java:587)
org.apache.jsp.WEB_002dINF.views.db_005feditor_jsp._jspService(db_005feditor_jsp.java:317)
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:443)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:945)
org.apache.jasper.runtime.PageContextImpl.doInclude(PageContextImpl.java:679)
org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:673)
org.apache.tiles.jsp.context.JspTilesRequestContext.include(JspTilesRequestContext.java:103)
org.apache.tiles.jsp.context.JspTilesRequestContext.dispatch(JspTilesRequestContext.java:96)
org.apache.tiles.renderer.impl.UntypedAttributeRenderer.write(UntypedAttributeRenderer.java:61)
org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:103)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:659)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:337)
org.apache.tiles.jsp.taglib.InsertAttributeTag.render(InsertAttributeTag.java:234)
org.apache.tiles.jsp.taglib.InsertAttributeTag.render(InsertAttributeTag.java:211)
org.apache.tiles.jsp.taglib.RenderTag.doEndTag(RenderTag.java:220)
org.apache.jsp.WEB_002dINF.views.main_005ftemplate_jsp._jspx_meth_t_005finsertAttribute_005f0(main_005ftemplate_jsp.java:271)
org.apache.jsp.WEB_002dINF.views.main_005ftemplate_jsp._jspService(main_005ftemplate_jsp.java:159)
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:443)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
org.apache.tiles.servlet.context.ServletTilesRequestContext.forward(ServletTilesRequestContext.java:241)
org.apache.tiles.servlet.context.ServletTilesRequestContext.dispatch(ServletTilesRequestContext.java:222)
org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44)
org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:103)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:659)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:678)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:633)
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:322)
org.springframework.web.servlet.view.tiles2.TilesView.renderMergedOutputModel(TilesView.java:124)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
答案 0 :(得分:0)
控制器上的编辑方法 - editWorker
- 未向模型添加tabel
属性。实际上他们中的任何一个都在这样做,但都重定向到同一个视图。因此,您需要在方法中添加该模型属性,就像在getWorkers
处一样。
但是我的建议是你应该编写用@ModelAttribute注释的方法来默认添加这些属性,这样它们将始终成为模型的一部分。如果您的任何请求映射方法添加它们,它们将覆盖那些默认值。
@ModelAttribute ( "worker" )
public Worker setWorkerToModel( )
{
return new Worker();
}
@ModelAttribute ( "tabel" )
public Tabel setTabelToModel( )
{
return new Tabel();
}
然后,您可以从getWorkers
方法中删除以下行。
model.addAttribute("worker", new Worker());
model.addAttribute("tabel", new Tabel());