【发布时间】:2016-03-10 20:13:07
【问题描述】:
我有 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 控制器中传递 Account.class)。但是在控制器中我什至没有得到 400 错误。经过多次测试后,我意识到 Spring MVC 并没有绑定我的列表。我想知道怎么做?
MVC 控制器:
@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;
}
【问题讨论】:
标签: java spring hibernate jsp spring-mvc