我有两个在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;
}
答案 0 :(得分:1)
您正在使用html select元素,但您需要做的是使用spring tag library&select;
<form:select path="accountTypes" multiple="true">
<form:options items="${accountTypes}"/>
</form:select>
这样spring会绑定控制器中的值。
尝试使用此
@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;
}
答案 1 :(得分:0)
问题是您需要注册一个属性编辑器或转换器,它能够将您的帐户类型名称转换为AccountType对象。您的视图发送accountTypes元素的字符串值。 Spring需要知道如何将String值绑定到Object。
public class AccountTypeEditor extends PropertyEditorSupport{
public void setAsText(String text){
AccountType accountType = // some logic to find account type by name
setValue(accountType);
}
}
在你的控制器中,你需要一个@InitBinder方法来注册你的编辑器
@InitBinder
public void initBinder(WebDataBinder binder){
binder.registerCustomEditor(AccountType.class , new AccountTypeEditor());
}
}
答案 2 :(得分:0)
我能够解决这个问题,我开始分别通过设置和分别对象本身的帐号。问题是我从jsp帐户传递,我想要设置,这是不可能的,因为我只选择名称类型帐户。这是解决我的问题:
JSP:
<form:form action="${pageContext.request.contextPath}/account/addAccount" method="post" modelAttribute="accountAttribute">
<label>
Сумма: <input type="number" name="price">
</label>
<br> <br>
<select multiple name="accountTypeNameSet" size="4s">
<c:forEach items="${accountTypes}" var="accountType">
<option >${accountType}</option>
</c:forEach>
</select>
<label>Комментарий:
<br> <br>
<textarea name="comment" rows="5" cols="30"></textarea>
</label>
<br><br>
<input type="submit" value="Внести">
</form:form>
控制器:
@RequestMapping(value = "/account/addAccount", method = RequestMethod.POST)
public void addAccount(@ModelAttribute("accountAttribute") Account account, @RequestParam(value="accountTypeNameSet") Set<String> accountTypeNameSet) {
// implementation here
// accountTypeNameSet - This is the list of names account type, which user selected. On the basis of these names we can pull your invoice from database.