是否有任何可用於Spring項目的模型/實體窗體抽象庫?Java/Spring模型表單框架
我在Java/Spring世界看起來像「Django forms framework」或「Symfony Forms」。
其基本思想是簡化JPA實體的表單創建和更容易創建多形式處理控制器。
是否有任何可用於Spring項目的模型/實體窗體抽象庫?Java/Spring模型表單框架
我在Java/Spring世界看起來像「Django forms framework」或「Symfony Forms」。
其基本思想是簡化JPA實體的表單創建和更容易創建多形式處理控制器。
我覺得春天做你想做的事情。
如果我已經正確理解了你,你是在將一個實體綁定到一個表單(並允許用戶添加/編輯實體)之後?在這種情況下,不需要另一個框架Spring已經做得很好。一個簡單的例子:
我們的控制器是這樣的:
@Controller
@RequestMapping(value = "/addUser.html")
public class UserController {
@Autowired
private UserAccountService service;
@Autowired
@Qualifier("userValidator")
private Validator userValidator;
@ModelAttribute("user")
public User getBackingObject() {
//This gets the object we're letting the user edit.
//This can be any POJO so a JPA entity should be fine.
//Note that we're creating an object here but we could
//just as easily fetch one we already have from a database/service etc
return new User();
}
@RequestMapping(method = RequestMethod.GET)
public String showForm() {
//The form to present to the user
return "/addUser";
}
@RequestMapping(method = RequestMethod.POST)
//note: here Spring has automatically bound the entries that have been input into the webform into the User param supplied here
protected String onSubmit(User user, Errors errors, HttpServletRequest request) {
userValidator.validate(user, errors);
if (errors.hasErrors()) {
//The validator showed up some errors so send the object back to let the user correct it
return "/addUser";
}
//save our new user
service.saveUser(user);
//best practice is to redirect to another view to make sure the backing object is cleared
return "redirect:/success.html";
}
}
然後我們可以使用Spring的表單宏在JSP中創建窗體:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Add a user</title>
</head>
<body>
<form:form commandName="user">
<label for="firstname">first name</label>
<form:input path="firstname" /> <form:errors cssClass="errorText" path="firstname" />
<label for="lastname">last name</label>
<form:input path="lastname" /> <form:errors cssClass="errorText" path="lastname" />
<input type="submit" value="Save" />
</form:form>
</body>
</html>
燁,多數民衆贊成的接近形式一個表單庫,我可以找到可用,(http://static.springsource.org/spring-roo/reference/html/base-web.html)謝謝指出這一點。 – vinilios
np。還有可以使用的spring-data項目。它可能會減少您的樣板jpa代碼。 http://static.springsource.org/spring-data/data-jpa/docs/1.0.0.M1/reference/html/。希望這有助於 – surajz