gpt4 book ai didi

java - 尝试在
having object list in modelAttribute 中包含对象的属性时出错

转载 作者:行者123 更新时间:2023-12-02 00:54:29 29 4
gpt4 key购买 nike

我正在开发一个 Spring MVC 项目。我有一个 View ,其中列出了 Person 类型的所有对象。列表已成功显示,其中包含所有属性。但是,由于我现在需要在用户选择时将单个对象传递回 Controller ,因此我已将列表包含在表单标记中,以便我可以将其与 modelAttribute 绑定(bind)并将对象发送回 Controller 。当我尝试包含 form:hidden 字段来绑定(bind)属性时,出现以下错误:

错误 o.s.w.s.tags.form.HiddenInputTag - bean 类 [java.util.ArrayList] 的属性“uid”无效:Bean 属性“uid”不可读或具有无效的 getter 方法:getter 的返回类型是否匹配setter 的参数类型?org.springframework.beans.NotReadablePropertyException:bean类[java.util.ArrayList]的无效属性“uid”:Bean属性“uid”不可读或具有无效的getter方法:getter的返回类型是否与参数类型匹配 setter 的?

Person 对象存在所有 setter 和 getter,并且如果我不包含 form:hidden,属性将成功显示

型号:

public class Person {

Integer id;

String uid;

String fullname;

String firstname;

String lastname;

String fathername;

String email;

String status;

List<String> environments;

List<String> institutions;

public boolean isNew() {
return (this.id == null);
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUid() {
return uid;
}

public void setUid(String uid) {
this.uid = uid;
}

public String getFullname() {
return fullname;
}

public void setFullname(String fullname) {
this.fullname = fullname;
}

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public String getFathername() {
return fathername;
}

public void setFathername(String fathername) {
this.fathername = fathername;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}


public List<String> getEnvironments() {
return environments;
}

public void setEnvironments(List<String> environments) {
this.environments = environments;
}
public List<String> getInstitutions() {
return institutions;
}

public void setInstitutions(List<String> institutions) {
this.institutions = institutions;
}
}

Controller :

@RequestMapping(value = "/persons/search", method = RequestMethod.POST)
public String listPersons(@RequestParam("searchString") String searchStr, Model model) {

logger.debug("listPersons()");
userService.initialize();
myPersons = userService.searchPersons(searchStr);
model.addAttribute("persons", myPersons);
return "users/listPersons";
}

查看 (listPersons.jsp)

<%@ page session="false"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<!DOCTYPE html>
<html lang="en">

<jsp:include page="../fragments/header.jsp" />

<body>

<div class="container">

<c:if test="${not empty msg}">
<div class="alert alert-${css} alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<strong>${msg}</strong>
</div>
</c:if>

<h1>All Persons</h1>

<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>UID</th>
<th>Full Name</th>
<th>Email</th>
<th>Institutions</th>
<th>Environments</th>
<th>Action</th>
</tr>
</thead>
<spring:url value="/persons/cards" var="showPersonCardsUrl" />
<form:form method="post" action="${showPersonCardsUrl}" modelAttribute="persons">
<c:forEach var="person" items="${persons}" varStatus="status">
<tr>
<td>
${person.id}
</td>
<td>${person.uid}</td>
<form:hidden path="uid" />
<td>${person.fullname}</td>
<td>${person.email}</td>
<td><c:forEach var="institution" items="${person.institutions}" varStatus="loop">
${institution}
<c:if test="${not loop.last}">,</c:if>
</c:forEach>
</td>
<td><c:forEach var="environment" items="${person.environments}" varStatus="loop">
${environment}
<c:if test="${not loop.last}">,</c:if>
</c:forEach>
</td>
<td>
<spring:url value="/persons/${person.uid}" var="showPersonUrl" />
<spring:url value="/persons/${person.uid}/delete" var="deletePersonUrl" />
<spring:url value="/persons/${person.uid}/update" var="updatePersonUrl" />
<spring:url value="/persons/${person.uid}/cards" var="showPersonCardsUrl" />

<button class="btn btn-info" onclick="location.href='${showPersonUrl}'">Query</button>
<button class="btn btn-primary" onclick="location.href='${updatePersonUrl}'">Update</button>
<button class="btn btn-danger" onclick="this.disabled=true;post('${deletePersonUrl}')">Delete</button>
<button class="btn btn-info" type="submit">Cards</button>
</td>
</tr>
</c:forEach>
</form:form>
</table>

</div>

<jsp:include page="../fragments/footer.jsp" />

</body>
</html>

而不是

<form:hidden path="uid" />

我也尝试过

<form:hidden path="persons.person.uid"/>

还有这个

<form:hidden path="users[${status.index}].uid"/>

但没有任何作用。我需要将 person 对象及其 uid 传递回 Controller ,以便我可以进行进一步的处理。我可能做错了什么?

最佳答案

隐藏的表单标记将尝试解析 modelAttribute 中的路径。 在 from 标签中,例如,您的 modelAttribute persons ,因此隐藏表单将尝试访问persons.uid如果您使用 persons.person.uid 它将解析为 persons.persons.person.uid等等。

就您而言,我建议使用常规输入:

<input type="hidden" name="persons[${status.index}]" value="${person.uid}"/>

这样,当提交表单时,它将发送一个名为 people 的数组,每个数组中都有 uid 值。

如果你还坚持使用<form:hidden> 那么您需要重新审视如何构建表单。

关于java - 尝试在 <form :hidden/> having object list in modelAttribute 中包含对象的属性时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57832018/

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