我有 2 个映射到 Hibernate 的类。
1. 类实现账户
2.该类实现账户类型。
重要!一个账户可以有多种类型。
Account.class
@Entity
@Table(name = "dp_account",
uniqueConstraints={@UniqueConstraint(columnNames={"id"})})
public class Account {
@Id
@Column(name="id", nullable = false, unique = true, length = 11)
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "price", nullable = false)
private int price;
@Column(name = "customer_id", nullable = false, unique = true, length = 11)
private long customerId;
@Column(name = "customer", length = 100, nullable = false, unique = true)
private String customer;
@Column(name = "comment", length = 1000)
private String comment;
@Column(name = "date", columnDefinition="TIMESTAMP")
@Temporal(TemporalType.TIMESTAMP)
private Date date;
@Column(name = "is_deleted", nullable = false)
private boolean deleted = false;
@ManyToMany(fetch = FetchType.EAGER, targetEntity = AccountType.class, cascade = { CascadeType.ALL })
@JoinTable(name = "account_accountType",
joinColumns = { @JoinColumn(name = "account_id") },
inverseJoinColumns = { @JoinColumn(name = "type_id") })
private Set<AccountType> accountTypes;
// дальше конструкторы и геттеры/сеттеры
}
帐户类型的本质,包括名称、描述、ID 和所有者姓名。
AccountType.class
@Entity
@Table(name = "account_type")
public class AccountType {
private static final Long serialVersionUID = -4727727495060874301L;
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "name", length = 25, nullable = false, unique = true)
private String name;
@Column(name = "description", length = 255)
private String description;
@Column(name = "user_login", length = 45, nullable = false, unique = true)
private String login;
//дальше геттеры/сеттеры и конструкторы
}
问题是 - 有一个页面,我可以在其中填写表格来创建帐户
( Account.class
),我愿意 multiple select
, 填写账户类型
(AccountType.class
)。 Account.class 有字段 Set<AccountType> accountTypes
,其中每个帐户都包含一个类型列表。 (这是我试图填写的列表,然后在 MVC Controller 中传递 Account.class)。但是在 Controller 中我什至没有收到 400 错误。经过多次测试,我意识到 Spring MVC 不会绑定(bind)我的列表。我想知道如何做到这一点?
MVC Controller :
@RequestMapping(value = "/account/addAccount", method = RequestMethod.POST)
public void addAccount(@ModelAttribute("accountAttribute") Account account) {
System.out.println("Account on the top");
System.out.println(account);
ServiceFactory.getInstance().getAccountService().addAccount(account);
}
JSP:
<div style="margin-top: 40px">
<form:form action="${pageContext.request.contextPath}/account/addAccount" method="post" modelAttribute="accountAttribute">
<label>
Сумма: <input type="number" name="price">
</label>
<br> <br>
<label>
Категория:
<select multiple name="accountTypes">
<c:forEach items="${accountTypes}" var="accountType">
<option value="${accountType.name}">${accountType.name}</option>
</c:forEach>
</select>
</label>
<br><br>
<label>Комментарий:
<br> <br>
<textarea name="comment" rows="5" cols="30"></textarea>
</label>
<br><br>
<input type="submit" value="Внести">
</form:form>
</div>
更新:
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public ModelAndView welcomePage() {
ModelAndView model = new ModelAndView();
List<GrantedAuthority> grantedAuthorityList = (List<GrantedAuthority>) SecurityContextHolder.getContext().getAuthentication().getAuthorities();
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String login = authentication.getName();
if (grantedAuthorityList.contains(new SimpleGrantedAuthority("ROLE_ADMIN"))) {
model.setViewName("admin");
} else {
List<AccountType> accountTypes = ServiceFactory.getInstance().getAccountService().getAllAccountTypeByLogin(login);
model.addObject("accountTypes", accountTypes);
model.setViewName("user");
}
return model;
}
最佳答案
您正在使用 html select 元素,但您需要做的是使用 spring 标签库的 select 即:
<form:select path="accountTypes" multiple="true">
<form:options items="${accountTypes}"/>
</form:select>
这样 spring 就会绑定(bind) Controller 中的值。
尝试使用这个
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public ModelAndView welcomePage() {
ModelAndView model = new ModelAndView();
List<GrantedAuthority> grantedAuthorityList = (List<GrantedAuthority>) SecurityContextHolder.getContext().getAuthentication().getAuthorities();
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String login = authentication.getName();
if (grantedAuthorityList.contains(new SimpleGrantedAuthority("ROLE_ADMIN"))) {
model.setViewName("admin");
} else {
List<AccountType> accountTypes = ServiceFactory.getInstance().getAccountService().getAllAccountTypeByLogin(login);
model.addObject("accountTypes", accountTypes);
model.setViewName("user");
//Add here
model.addObject("accountAttribute", new Account());
}
return model;
}
关于java - MVC 和带有 Set<> 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35926305/