我正在使用Thymeleaf并试图做一些对象绑定,但如果我有一个带有列表的对象,我不知道如何做。让我解释一下:
@Entity
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
private String name;
@NotNull
@Lob
private String description;
@NotNull
private Date startDate;
private String status;
@ManyToMany
private List<Role> rolesNeeded;
@ManyToMany
private List<Collaborator> collaborators;
public Project() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<Role> getRolesNeeded() {
return rolesNeeded;
}
public void setRolesNeeded(List<Role> rolesNeeded) {
this.rolesNeeded = rolesNeeded;
}
public List<Collaborator> getCollaborators() {
return collaborators;
}
public void setCollaborators(List<Collaborator> collaborators) {
this.collaborators = collaborators;
}
}
<form method="post" action="addproject" th:object="${project}">
<div>
<label for="project_name"> Project Name:</label>
<input th:field="*{name}" type="text" name="project_name"/>
</div>
<div>
<label for="project_description">Project Description:</label>
<textarea th:field="*{description}" rows="4" name="project_description"></textarea>
</div>
<div>
<label for="project_status">Project Status:</label>
<div class="custom-select">
<span class="dropdown-arrow"></span>
<select th:field="*{status}" name="project_status">
<option value="active">Active</option>
<option value="archived">Archived</option>
<option value="not_started">Not Started</option>
</select>
</div>
</div>
<div>
<label for="project_roles">Project Roles:</label>
<ul class="checkbox-list">
<li th:each="role : ${roles}">
<input th:field="*{rolesNeeded}" type="checkbox" name="project_roles" th:value="${role}"/>
<span class="primary" th:text="${role.name}"> Developer</span>
</li>
</ul>
</div>
<div class="actions">
<input type="submit" value="Save" class="button"/>
<a href="#" class="button button-secondary">Cancel</a>
</div>
</form>
错误!!!!:对象“Project”中字段“Roles Needed”上的字段错误:拒绝值[com.imprender.instateam.model.role@145d6cd4,com.imprender.instateam.model.role@73020d6f];代码[TypeMismatch.project.RolesNeed,TypeMismatch.RolesNeed,TypeMismatch.java.util.List,TypeMismatch];参数[org.springframework.context.support.DefaultMessageSourceResolvable:代码[project.rolesNeed,rolesNeed];参数[];默认消息[rolesNeed]];默认消息[未能将属性“Roles Needed”的类型“java.lang.String[]”的属性值转换为必需的类型“java.util.List”;嵌套异常是java.lang.IllegalStateException:无法将属性“Roles Need[0]”的类型“java.lang.String”的值转换为必需的类型“com.imprender.Instateam.model.Role”:找不到匹配的编辑器或转换策略]
基本上,据我所知,checkbox输入返回一个String[],但我的对象需要一个列表,因此绑定无法完善。
如何在列表中绑定数组?(你有一个例子吗?)
谢谢你。
如果您的角色
bean具有active
boolean属性,则可以执行以下操作(简化):
<ul class="checkbox-list">
<li th:each="role,stat : ${project.rolesNeeded}">
<input th:field="*{rolesNeeded[__${stat.index}__].active}" type="checkbox"/>
<input th:field="*{rolesNeeded[__${stat.index}__].name}" type="hidden"/>
<span class="primary" th:text="${role.name}">Developer</span>
</li>
</ul>
如果没有,您可以将roleSneed
存储在隐藏字段中,并用JavaScript填充它们。
我有两个实体,它们使用一个主键互相引用,主键是一个实体的整数。我不确定我做这件事的方式是否正确。 下面是引用主键id为int的实体 下面是我们从上面的实体中将外键设置为Kmichango kandaMchango的实体。 这里是表单的一部分,我在这里提交了用户在jumuiya_michango_form.html中提供的数据 下面是我的控制器中用于链接到表单和发布数据的两个方法 在我提交表单后,我
我正在关注Spring in Action 5,在按下提交按钮后创建Taco模型时遇到问题。这是我的设计Taco控制器类: 以及我捕获的错误消息: 炸玉米饼实体如下所示: 以及我的配料实体: 这是一个html页面,必须使用所选成分创建新的Taco对象: 我该怎么修理它?谢谢你的预付款。
我是Spring MVC的新手,试图构建一个示例应用程序,我试图将数据保存到一个名为EmployeeDetails的表中,该表与表用户有一对一的关系。提交表单后,我在字段“user”的对象“employee details”中得到了这个错误字段:rejected value[Users];代码[typemismatch.employeeDetails.user,typemismatch.user,
我是SpringMVC的新手。我开发了一个执行选择,插入,更新和删除的示例应用程序。 下面是我的Bean类 下面是我的控制器类 下面是我的JSP页面 现在我面临两个问题。输入值并单击“添加学生”按钮后,收到以下错误。
上面表单的控制器 简单pojo类 这个链接没有帮助我解决类似的问题
我想在json中以的形式发布上面的字符串日期,但我收到了400个错误请求。有人能帮忙吗。我使用过,但它也没有帮助我。
我的spring应用程序无法将“日期”保存到“数据库”。错误在哪里? 错误 无法将类型为[java.lang.String]的属性值转换为属性bornDate所需的类型[java.sql.Date];嵌套的异常是java。lang.IllegalArgumentException:无法解析日期:无法解析的日期:“2016-11-02” mysql 实体 控制器 艾德曼。html
我正在使用Thymeleaf和SpringBoot构建一个web应用程序,我对这两种技术都是新手。 在我的html文件中,有一个日期字段,如下所示: 我的模型类有一个与issueDate对应的字段,如下所示: 当我从UI输入日期时,我在浏览器中看到以下异常: 根据我的经验,我理解UI将属性读取为字符串,但模型期望出现错误的类型日期。所以我需要将字符串转换为日期。然而,这应该在哪里进行?因为错误甚至