<form>标签 - Spring MVC </form>出错

时间:2013-08-30 15:49:30

标签: java jsp spring-mvc

我正在使用spring mvc开发一个简单的聊天应用程序,但是出现了错误

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/view/UserDetails.jsp at line 23

20: <c:out value="${chat.message}"/>
21: </c:forEach>
22: <form:form id="chat" modelAttribute="chatList" method="post" action="saveChat.html">
23: <form:input path="message"/>
24: <input type="submit" value="Send" />
25: </form:form>
26: <br>

org.springframework.beans.NotReadablePropertyException: Invalid property 'message' of bean class [java.util.ArrayList]: Bean property 'message' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:665)
org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:657)
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:147)
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:409)
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.view.UserDetails_jsp._jspx_meth_form_005finput_005f0(UserDetails_jsp.java:250)
org.apache.jsp.WEB_002dINF.view.UserDetails_jsp._jspx_meth_form_005fform_005f0(UserDetails_jsp.java:215)
org.apache.jsp.WEB_002dINF.view.UserDetails_jsp._jspService(UserDetails_jsp.java:92)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

我的jsp文件是

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Being Java Guys | User Details</title>
</head>
<body>
<center>
<br><br><br><br><br><br>
<div style="color: teal;font-size: 30px">Group Chat</div>
<br><br>
<c:url var="userChat" value="saveChat.html"/>

<c:forEach items="${chat}" var="chat">
<c:out value="${chat.username}:"/>
<c:out value="${chat.message}"/>
</c:forEach>
<form:form id="chat" modelAttribute="chatList" method="post" action="saveChat.html">
<form:input path="message"/>
<input type="submit" value="Send" />
</form:form>
<br>
</center>
</body>
</html>

相关的控制器代码是

@RequestMapping("/saveChat")
    public ModelAndView saveChat(@ModelAttribute("chatList") Chat chat,BindingResult result){
        userService.addChat(chat);
        return new ModelAndView("redirect:/userList");
    }

    @RequestMapping("/userList")
    public ModelAndView getUserList() {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("chat", userService.getChat());
        return new ModelAndView("UserDetails", model);

    }

错误是在表单输入标记上肯定的,因为当我对它进行评论时,它工作正常。但为什么会这样,修复是什么?

1 个答案:

答案 0 :(得分:1)

这一行

return new ModelAndView("redirect:/userList");

导致303重定向状态代码作为响应发送。因此,在该请求期间添加的模型属性将无法在浏览器的下一个请求中使用。

请考虑使用RedirectAttributes(闪存属性)。