英文:
modelAttribute and path in a form when using a wrapper of a list of entities and c:foreach loop
问题
我计划创建一个带有多个表单的视图,这些表单是由模型对象列表创建的。正如在这里的答案中所描述的 https://stackoverflow.com/a/32666512/5673289,我创建了一个列表的包装对象:
src/main/java/tk/trzczy/gallery/wrapperObject/GalleryForm.java
package tk.trzczy.gallery.wrapperObject;
import tk.trzczy.gallery.domain.Image;
import java.util.List;
public class GalleryForm {
private List<Image> gallery;
public GalleryForm(List<Image> gallery) {
this.gallery = gallery;
}
public List<Image> getGallery() {
return gallery;
}
public void setGallery(List<Image> gallery) {
this.gallery = gallery;
}
}
并且以以下方式改进了控制器:
src/main/java/tk/trzczy/gallery/controllers/ImageController.java
package tk.trzczy.gallery.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import tk.trzczy.gallery.domain.Image;
import tk.trzczy.gallery.services.ImageService;
import tk.trzczy.gallery.services.ImageCategoryService;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Date;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import tk.trzczy.gallery.wrapperObject.GalleryForm;
@Controller
public class ImageController {
private static final Logger log = LogManager.getLogger(LoginController.class);
@Autowired
private ImageService imageService;
//(...)
@RequestMapping("/gallery/edit")
public String getImagesToEdit(Model model) {
model.addAttribute("images", new GalleryForm(imageService.getAllImages()));
return "editGallery";
}
//(...)
}
并且尝试在 JSP 文件中使用正确的 modelAttribute
和 path
值,但未成功:
src/main/webapp/WEB-INF/views/editGalleryPage.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="g" uri="/WEB-INF/date-format-tag.tld" %>
<link rel="stylesheet" href="<spring:url value="/resource/css/editGallery.css" />"/>
<div class="panel-body">
<div class="row">
<c:forEach items="${images.gallery}" var="image" varStatus="vs">
<form:form modelAttribute="images" action="/images/edit/${image.id}" id="image" class="image"
method="post">
<form:input path="images.gallery[${vs.index}].url" id="image-${image.id}" style="width: 525px;"
class=" form:input-large"
name='url' value="${image.url}"/>
<input type="submit">
<p><form:errors path="url" cssClass="alert alert-danger"/></p>
</form:form>
</c:forEach>
</div>
</div>
错误是:
Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'images' of bean class [tk.trzczy.gallery.wrapperObject.GalleryForm]: Bean property 'images' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
这是一个 pom.xml 文件:
<!-- (项目依赖) -->
如何修复这个问题?
英文:
I plan to create a view with multiple forms tha are created of a list of model objects. As it is desribed in an answer here https://stackoverflow.com/a/32666512/5673289 I created a wrapper object for the list:
src/main/java/tk/trzczy/gallery/wrapperObject/GalleryForm.java
package tk.trzczy.gallery.wrapperObject;
import tk.trzczy.gallery.domain.Image;
import java.util.List;
public class GalleryForm {
private List<Image> gallery;
public GalleryForm(List<Image> gallery) {
this.gallery = gallery;
}
public List<Image> getGallery() {
return gallery;
}
public void setGallery(List<Image> gallery) {
this.gallery = gallery;
}
}
and improved a controller in this way:
src/main/java/tk/trzczy/gallery/controllers/ImageController.java
package tk.trzczy.gallery.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import tk.trzczy.gallery.domain.Image;
import tk.trzczy.gallery.services.ImageService;
import tk.trzczy.gallery.services.ImageCategoryService;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Date;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import tk.trzczy.gallery.wrapperObject.GalleryForm;
@Controller
public class ImageController {
private static final Logger log = LogManager.getLogger(LoginController.class);
@Autowired
private ImageService imageService;
//(...)
@RequestMapping("/gallery/edit")
public String getImagesToEdit(Model model) {
model.addAttribute("images", new GalleryForm(imageService.getAllImages()));
return "editGallery";
}
//(...)
}
And tried to use proper modelAttribute
and path
values in jsp file, but did not succeed:
src/main/webapp/WEB-INF/views/editGalleryPage.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="g" uri="/WEB-INF/date-format-tag.tld" %>
<link rel="stylesheet" href="<spring:url value="/resource/css/editGallery.css" />"/>
<div class="panel-body">
<div class="row">
<c:forEach items="${images.gallery}" var="image" varStatus="vs">
<form:form modelAttribute="images" action="/images/edit/${image.id}" id="image" class="image"
method="post">
<form:input path="images.gallery[${vs.index}].url" id="image-${image.id}" style="width: 525px;"
class=" form:input-large"
name='url' value="${image.url}"/>
<input type="submit">
<p><form:errors path="url" cssClass="alert alert-danger"/></p>
</form:form>
</div>
</div>
The error was
Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'images' of bean class [tk.trzczy.gallery.wrapperObject.GalleryForm]: Bean property 'images' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
Here it is a pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tk.trzczy</groupId>
<artifactId>spring-webmvc</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<aspect.version>1.9.2</aspect.version>
<jackson.version>2.9.8</jackson.version>
<hibernate.version>5.4.2.Final</hibernate.version>
<hibernate.validator.version>5.2.1.Final</hibernate.validator.version>
<c3p0.version>0.9.5.2</c3p0.version>
</properties>
<dependencies>
<!-- servlets and jps -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-extras</artifactId>
<version>3.0.8</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>jboss</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<!--hibernate-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
<exclusions>
<exclusion>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Hibernate-C3P0 Integration -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.validator.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<!-- for rest services -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<!-- For Aop -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspect.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspect.version}</version>
</dependency>
<!-- To Send Email -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!--logging-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-web -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
</dependency>
<!-- PostgreSQL -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.16</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-taglibs -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-war-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-rest-core -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-core</artifactId>
<version>3.3.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<!-- To define the plugin version in your parent POM -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!-- To use the plugin goals in your POM or parent POM -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
How to fix it?
答案1
得分: 1
这应该可以工作
<div class="panel-body">
<div class="row">
<c:forEach items="${images.gallery}" var="image" varStatus="vs">
<form:form modelAttribute="images" action="/images/edit/${image.id}" id="image" class="image" method="post">
<form:input path="gallery[${vs.index}].url" id="image-${image.id}" style="width: 525px;"
class="form:input-large"
name='url' value="${image.url}"/>
<input type="submit">
<p><form:errors path="gallery[${vs.index}].url" cssClass="alert alert-danger"/></p>
</form:form>
</c:forEach>
</div>
</div>
问题在于form:input
指定的路径以images.
开头,但由于modelAttribute
是images
,它需要以gallery
开头。
英文:
this should work
<div class="panel-body">
<div class="row">
<c:forEach items="${images.gallery}" var="image" varStatus="vs">
<form:form modelAttribute="images" action="/images/edit/${image.id}" id="image" class="image" method="post">
<form:input path="gallery[${vs.index}].url" id="image-${image.id}" style="width: 525px;"
class=" form:input-large"
name='url' value="${image.url}"/>
<input type="submit">
<p><form:errors path="gallery[${vs.index}].url" cssClass="alert alert-danger"/></p>
</form:form>
</c:forEach>
</div>
</div>
the problem was the path specified for form:input
which started with images.
but since the modeAttribute is images
it needed to start with gallery
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论