Spring form insert data in model object inside base domain model - spring

I have a model like this:
public class User{
private int id;
private City city;
//Getter - setters
}
public class City{
private int id;
private String name;
//Getter - setters
}
Now on JSP I want to show a form for User model, which should ask for user's city in a dropdown, and corresponding city object should get stored in city object in User model.
For normal fields, I know we can write something like this:
<form:input path="name" />
But how can we bind a model object inside our base model? And how Spring will know, which object it should store in that after user selects any city?

Pretty easy, you need #modelAttribute in your controller method (or simply add it to model) on loading the form and use in your case path should be city.name
Your question is more like how to reference a submodel, spring follows the bean path, in your case, if you wanna post a city with user object, you just need to do this
<form:form method="POST" commandName="user">
<form:input path="city.name" />
<form:input path="attribute of user" />
</form:form>

Related

Thymeleaf th:each cannot find object

I'm using Thymeleaf with springboot 2.1.2, and I have a problem with Thymeleaf's th:each tag.
Here's my code:
<tr th:each="addr : ${user.addressList}">
<td><input type="text" th:value="${addr.name}" th:field="${addr.name}"></td>
<td><input type="text" th:value="${addr.phone}" th:field="${addr.phone}"></td>
<td><input type="text" th:value="${addr.location}" th:field="${addr.location}"></td>
<td><a th:href="#{/user/addr/del/{id}(id=${addr.id})}">del</a></td>
</tr>
However, I encounter the following exception:
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'addr' available as request attribute
The debugger says it can't find the variable, however, I'm sure that the view has a binding object named 'user' because I use it else where in the front.
Here's my controller and my entities:
#GetMapping("/profile")
public String getProfile(HttpServletRequest request, Model model) {
HttpSession session = request.getSession(false);
String email = (String) session.getAttribute("userId");
User user = userService.getProfile(email);
model.addAttribute("user", user);
return "user/profile";
}
#Entity
public class User {
#Id
private String email;
private String userName, password;
private int pts; //积分
private int status= Flags.USER_EXIST;
#OneToMany(targetEntity = Address.class
, fetch = FetchType.LAZY, mappedBy = "user"
, cascade = {CascadeType.PERSIST,CascadeType.REMOVE})
private List<Address> addressList = new ArrayList<>();
//constructors and getters and setters below...
}
#Entity
public class Address {
#Id
#GeneratedValue //去掉这个注解会导致一系列bug
private long id;
private String name, phone, location;
#ManyToOne(targetEntity = User.class)
private User user;
//constructors and getters and setters below...
}
I follow the tutorial here and I can't find any difference between my th:each usage and the tutorial's. Could you help me?
UPDATE ============================================>
Here's my code for UserService.getProfile(String email):
#Override
public User getProfile(String email) {
Optional<User> res = userRepository.findById(email);
return res.isPresent() ? res.get() : null;
}
UPDATE again ==========================================================>
summerize what i've done so far:
1) use FetchType.EAGER in User class
2) force the repository to retrieve the relevant Address by printing the addressList:
user.getAddressList().stream().forEach(x -> System.out.println(x.getLocation()));
3) delete the th:each block and restart my app, there is no exception; when i add back the block, my app fails again...
All of these measures don't seem to help me....
Problem not related to JPA after all. The problem is how you use th:field. See documentation about correct usage: https://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#dynamic-fields
Try this:
<tr th:each="addr,addrStat : ${user.addressList}">
<td><input type="text" th:value="${addr.name}" th:field="*{addressList[__${addrStat.index}__].name}"></td>
...
You're having this error :-
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'addr' available as request attribute
Because you're using #ModelAttribute annotation on your controller. You're trying to send an object named addr to your controller after submit , However you're not sending any object by the exact name addr in/from your controller from where you're opening your requested html page.
Here's a quick example of how you send object using #ModelAttribute annotation :--
1.) Your requested controller from where you're opening the requested HTML page would be something like this: -
#RequestMapping("/test")
public String test(HttpServletRequest request,Model model){
//your code
model.addAttribute("addr",new MyTest());
return "yourRequestedPage";
}
2.) And after submit your next controller would be something like this :-
#RequestMapping("/testAfterSubmit")
public String someMethodName(#ModelAttribute("addr") MyTest myTestClass,HttpServletRequest request,Model model ) {
//your code . .
}
PS - For more information what #ModelAttribute does/is , here's a quick reference :-
What is #ModelAttribute in Spring MVC?
I thought I've found the problem...It's very tricky...I edit the th:each block, and now it's like :
<tr th:each="addr : ${user.addressList}">
<td><input type="text" th:value="${addr.name}"></td>
<td><input type="text" th:value="${addr.phone}"></td>
<td><input type="text" th:value="${addr.location}"></td>
<td><a th:href="#{/user/addr/del/{id}(id=${addr.id})}">del</a></td>
</tr>
Have you found the difference? Yes i remove the th:field tag! In the origin code, what i did is binding the Address object's attribute in the list to a iterator! Maybe it's readonly i think...so the compiler would expect an object named 'addr' in the model, of course this would fail...
This bug alarms me to separate the modification function and the presentation function into respective view...

Adding a Spring Data Repository disables functionality

I have these two entities.
#Entity
public class Person {
#Id #GeneratedValue
private Long id;
private String name;
#ManyToOne(cascade=CascadeType.ALL)
private Location location;
public Person() {
}
#Entity
public class Location {
#Id #GeneratedValue
private Long id;
private String place;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "location")
private Set<Person> persons;
public Location() {
}
I also have this Controller.
#Controller
public class PersonController {
private final PersonRepository repo;
public PersonController(PersonRepository repo) {
this.repo = repo;
}
#GetMapping("/")
public String newPerson(Person person){
return "home";
}
#PostMapping("/")
public String newPerson(Person person, BindingResult result){
repo.save(person);
return "redirect:/person";
}
And this Repository.
#Repository
public interface PersonRepository extends JpaRepository<Person, Long> {
Optional<Person> findFirstByName(String name);
}
I also have this backing form.
<form action="#" th:action="#{/}" th:object="${person}" method="post">
<table>
<tr>
<td>Name:</td>
<td><input type="text" th:field="*{name}" /></td>
</tr>
<tr>
<td>Location:</td>
<td><input type="text" th:field="*{location}" /></td>
</tr>
<tr>
<td><button type="submit">Submit</button></td>
</tr>
</table>
</form>
This all works fine when I submit some data. A Person object is saved and so is a Location object.
But when I add
#Repository
public interface LocationRepository extends JpaRepository<Location,
Long> {)
the Location object does not save to the database when I submit the same exact form. Why would just adding this repository cause this issue and what is the solution? Thanks.
You whould fix your form in order to write attribute of location property:
<td><input type="text" th:field="*{location.place}" /></td>
Also you don't have to put #Repository annotation on your repositories.
To elaborate on why things work as they work:
The form binding uses the ConversionService. Spring Data registers a conversion chain from String -> id type -> entity type for each repository managed domain class. So the moment you add a repository, the String submitted as value for Person.location will be interpreted as an identifier for an already existing location. It will cause a by-id lookup with the value submitted for the field named location.
This is handy in the following scenario: assume you're Location is basically a curated list of instances held in the database, e.g. a list of countries. They you don't want to arbitrarily create new ones but rather select one from the overall list, which basically boils down to having to use a dropdown box instead of a text field.
So conceptually, the fundamental things at odds are the cascades (as they indicate a composition, i.e. Location being part of the aggregate) and the existence of LocationRepository as a repository causes the managed type to implicitly becoming an aggregate root (which is fundamental DDD).
This in turn means you have to handle the lifecycle of that instance separately. A potential solution is to inspect the Location bound to the Person, check whether an instance with that place already exists (via a query method on LocationRepository) and if so, replace the one bound with the one loaded or just call LocationRepository.save(…) with the original instance to create a new one.
I still don't totally buy that the original attempt created a correct Location as from your template Spring Framework is not able to guess that what you submit as location is supposed to be the place actually. So I assume you saw a Location instance being created, but completely empty and the BindingResult actually carrying an error, claiming it couldn't transform the location form field into an instance of Location.

Update one attribute of an Entity with ModelAttribute

How can I update just one or a few atributes of an Entity with spring form and controller?
Lets say it is User Entity and has id, status, name, address etc...
I want to update just name, and address. But when I try to save ather values is null. ı dont want to show all attributes in form logically ( Id, status )
You can use hidden input element to propagate users ID to your view, e.g.
<input type="hidden" name="user-id" value="${editUserForm.id}">
Put it in a form - when a form is submitted, users ID will also be submitted with it (remember to add ID to your form model). Then retrieve user from database using this ID, set fields you want to set and update it.
EDIT:
Example:
your model:
#Entity
public class User{
private Long id;
private String name;
private String surname;
//getters & setters
}
form you use to edit some of the fields (no surname):
public class UserForm{
private Long id;
private String name;
//getters & setters, constructor
}
Controller:
#GetMapping(value="/editUser/{userId}")
public ModelAndView editUser(#PathVariable Long userId){
ModelAndView modelAndView = new ModelAndView("editUser");
User user = // retrieve user from database using userId
modelAndView.addObject("editUserForm", new UserForm(user));
return modelAndView;
}
#PostMapping(value="/editUser")
public ModelAndView postEditUser(#ModelAttribute("editUserForm") UserForm editUserForm){
User userToEdit = //retrive user from database using editUserForm.getId()
userToEdit.setName(editUserForm.getName());
//save user to database
//redirect
}
Of course logic I presented in controllers should be located in service layer, I just want to give you an idea on what to do.

Posting a complete model object to the controller when only few attributes are used in a form

I've read somewhere that for spring mvc, it is a expected behavior to get back NULL in case a form does not contain all the attributes of the model object set by the #ModelAttribute annotiation. S how can I use forms that don't have all the fields of the model object and still recieve the whole but updated object back to the post method of the controller.
A short example code of my intention:
Part of the controller:
....
#RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
public String editPost(Model model, #PathVariable Integer id) {
model.addAttribute("editPost", bPostService.getPost(id));
return "editPost";
}
#RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
public String editProcessPost(Model model, #PathVariable Integer id, #ModelAttribute BPost editPost) {
bPostService.updatePost(editPost);
model.addAttribute("posts", bPostService.getPosts());
return "redirect:/";
}
....
The entity mapped by hibernate:
#Entity
#Table(name = "sometable")
public class BPost {
#Id
#GeneratedValue
#Column(name = "id")
private int id;
#Column(name = "title")
private String title;
#Column(name = "description")
private String description;
#Column(name = "text")
private String text;
#Column(name = "anothertext")
private String anothertext;
// getters and setters
}
Part of the JSP view:
<form:form method="POST" modelAttribute="editPost" action="${pageContext.request.contextPath}/secure/post/edit/${editPost.id}">
<table>
<tbody>
<tr>
<td>title:</td>
<td><form:input path="title"></form:input></td>
</tr>
<tr>
<td>description:</td>
<td><form:input path="description"></form:input></td>
</tr>
<tr>
<td>text:</td>
<td><form:input path="text"></form:input></td>
</tr>
<tr>
<td><input value="Edit" type="submit"></td>
<td></td>
</tr>
</tbody>
</table>
</form:form>
As you can see the "anothertext" attribute is not used on the JSP, but I wan't it unchanged returned to the POST method of the controller. Is that possible?
I know some one probably already asked this question, but I cant find the answer to that.
Thank!
You might not want to use the entity as a form backing object which could have security implications. For example an malicious request could be forged to set some unwanted properties.
Therefor it's better in general to create a explicit form backing object for each form to process. It will require you to write more code but it also negates some common problems (like the one you're having).
When using a form backing object your handler looks more like:
Note that I altered the BPost argument to BPostForm.
#RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
public String editProcessPost(Model model, #PathVariable Integer id, #ModelAttribute BPostForm editPost) {
// fetch the original post
BPost post = bPostService.findById(editPost.getId());
// set the properties
post.setTitle(editPost.getTitle());
post.setDescription(editPost.getDescription());
post.setText(editPost.getText());
// update
bPostService.updatePost(post);
model.addAttribute("posts", bPostService.getPosts());
return "redirect:/";
}
P.s. Using bPostService.getPosts() to add posts to the model and immediately return a redirect seems rather pointless ;)
[EDIT] Validation
Your form backing object can be validated using declarative validation using the Hibernate annotations or settings a custom validator in the WebdataBinder.
Hibernate annotations
When using the Hibernate annotations you can place any annotation on a field or getter. For these validations to kick in you'll need to do two things.
Register a validator bean org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.
Annotate the form backing object's argument in your handler with #valid.
Example: public String editProcessPost(Model model, #PathVariable Integer id, #ModelAttribute #Valid BPostForm editPost, BindingResult result)
Note that using validation needs a BindingResult to be present in the argument list and it needs to be directly after the backing object. This BindingResult will be a container for all validation errors.
Custom validator
A custom validator is a bit more work. You will need to write your own first.
MyPostValidator extends org.springframework.validation.Validator
After writing the validator you can add it to the WebDataBinder.
#InitBinder
public void initBinder(WebDataBinder binder) {
binder.setValidator(new MyPostValidator());
}
It's much easier than this actually....I've been using this method for years
In your Controller class do the following:
// this method gets called by Spring after the GET/POST request but before the // binding of request parameters...
// for POST requests, we want the enity
#ModelAttribute("formName") // <---- Note the attribute name...this is important
public Object getFormBackingObject(HttpServletRequest request) {
if(!request.getMethod().equals("POST")) {
return null;
}
// find primary key
String id = request.getParameter("id");
return serviceObject.getMyEntity(id);
}
#RequestMapping(value="/edit/{id}", method=RequestMethod.POST)
public String editProcessPost(#PathVariable Integer id, #ModelAttribute("formName") BPostForm editPost) {
// editPost is the fully populated entity from the DB after request params
// have been bound to it.
myService.save(editPost);
return "whatever....";
}
As suggested by #Bart if possible replace the usage of entity pojo to form pojo directly in the jsp. If you want to continue with the existing approach you can use those fields as hidden parameters.
<form:hidden path="anothertext"/>
So when the form is getting submitted this value will be set automatically.
Here you may have another 2 issues.
Hidden Field with another value
Suppose you want to keep it as hidden value, but value should be different, then you can use like below.
<input type="hidden" name="anothertext" value ="{object.value}">
where object is any object available in the view scope.
Hidden Field As Object
What if you have have anothertext as object instead of plain text. For example if it is a User object with id and name as value and you have used like below under your entity
#OneToOne
#JoinColumn(name = "ownerName", referencedColumnName = "name")
private User owner;
In that case you have to use two values as hidden parameters. The id field of the object and the linking field (here it is name)
So it will go like below.
<form:hidden path="owner.id"/>
<form:hidden path="owner.name"/>
While persisting this back, hibernate will automatically merge with existing user from db with same id.

Spring + hibernate + form

I have an User entity:
#Entity
#Table(name = "user")
public class User implements Serializable{
private enum Sex {MALE, FEMALE};
#Id #GeneratedValue
private Long id;
#NotNull
private String name;
#NotNull
private String password;
}
Controller:
#Controller
public class HomeController {
#RequestMapping("/addUser")
public String showHomePage(Map<String, Object> map) {
map.put("user", new User());
return "addUser";
}
and form in jsp file
<form:form method="post" action="add" commandName="user">
<form:input path="name"/>
<form:input path="password"/>
<form:input path="confirm-password"/>
</form:form>
This form generates error because there is no confirm-password field in User.class. I could of course add confirm-password field as #Transient to User.class, but what if I would like to add captcha field to my form. Is there a different way to add additional field to form?
It is not a good practice to use your models as form entities. You should have a bean or form class to get the data from the view and another for the model.
Reason is that they have different responsibilities thus needing to to be mapped to different classes. They are often very similar, but that separation promotes a cleaner coding and avoids security breaches, as a user could try to use your model variables from the view by using a request tamperer for example(like fire bug).
These small differences between them like the one you listed above justify the creation of another class.

Resources