我在spring中编写了一个简单的Web应用程序,但是我在运行项目时遇到错误。我面临的错误是
org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/view/UserDetails.jsp at line 22
19: <c:out value="${chat.username}:"/>
20: <c:out value="${chat.message}"/>
21: </c:forEach>
22: <form:input path="message"/>
23: <input type="submit" value="Send" />
24: </form:form>
25: <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?
我的POJO课程是
package com.beingjavaguys.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Generated;
@Entity
@Table(name="messages")
public class Chat {
@Id
@GeneratedValue
@Column(name = "user_id")
private int id;
@Column(name="username")
private String username;
@Column(name="message")
private String message;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
我的Dispatcher servlet
是
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.beingjavaguys.domain.User</value>
<value>com.beingjavaguys.domain.Chat</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
我的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>Group chat | 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="userRegistration" value="saveChat.html"/>
<form:form id="chatMessages" modelAttribute="chat" method="post" action="${userRegistration}">
<c:forEach items="${chat}" var="chat">
<c:out value="${chat.username}:"/>
<c:out value="${chat.message}"/>
</c:forEach>
<form:input path="message"/>
<input type="submit" value="Send" />
</form:form>
<br>
</center>
</body>
</html>
我的控制器
@RequestMapping("/saveChat")
public ModelAndView saveChat(@ModelAttribute("chat") 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);
}
我无法理解此处的问题(编辑错误)。
答案 0 :(得分:1)
在JSP网页中,您尝试获取类ArayList的对象的message
属性而不是Chat
。
也许您应该尝试更改modelAttribute名称以避免列表使用的名称chat
:
<form:form id="chatMessages" modelAttribute="chatModel" method="post" action="${userRegistration}">
<c:forEach items="${chat}" var="user">
<c:out value="${user.username}:"/>
<c:out value="${user.message}"/>
</c:forEach>
<form:input path="message"/>
<input type="submit" value="Send" />
</form:form>
并在以下内容中进行更改:
public ModelAndView saveChat(@ModelAttribute("chatModel") Chat chatModel,BindingResult result){
userService.addChat(chatModel);
return new ModelAndView("redirect:/userList");
}