Bind a set in a spring form - java

I am trying to bind a spring form with a set in the command object.
In my command class AInstance I defined set as
private Set<BParameter> bParameters = new HashSet<BParameter>();
In jsp I bind it as
<form:input path="bParameters " />
<form:input path="bParameters " />
As its a Java Set so there may be many fields. When I submit this form I tried to get Set as:
Set<BParameter> bParameters = aInstance.getBParameters();
I got Set with a 0 size.
I also tried to bind as
<form:input path="bParameters[${itemsRow.index}].bParmvalues[0].parmValue" />
but got exception
Invalid property 'bParameters[0]' of bean class
What is the problem with my binding?

Use an List in the controller.
In the view you can use this straight html (not sure if this works with spring tags).
<input name="bParameters[{idx}].bParmvalues[0].parmValue" />

Its going to be an array, which Spring will translate into a List; it will also instantiate the List implementation - you don't need to do that in your Command object. Try using
private List<String> bParameters;
public void setBParameters(List<String> bParameters) {
this.bParameters= bParameters;
}
public List<String> getBParameters() {
return bParameters;
}
in your Command object. Those values are probably coming in as Strings.

I don't have problems binding as
private Set<Types> typeses = new HashSet<Types>(0);
<form:textarea path="typeses" style="width:200px;height:150px"/>
I use Spring 3.5. The only problem with this is, that it leaves []-marks on the field for some reason

I cannot find a way to bind Set into form parameters. However other solutions suggesting to "use list instead" is not good enough for me because if the field is part of JPA one-to-many relationship with eager fetching, using List will cause duplicates
Hence the best solution I find so far is by posting the form as JSON -- using Ajax -- instead. Here's how in JQuery:
var person = // .. construct form object here
$.ajax(url, {
method : 'post',
contentType : 'application/json',
data : JSON.stringify(person)
});
Note that you need to mark the contentType as application/json
On the controller side, you can bind this to a Java object by using #RequestBody annotation:
#RequestMapping(..)
public void save(#RequestBody Person person) {
..
}
More info here: https://gerrydevstory.com/2013/08/14/posting-json-to-spring-mvc-controller/

Related

How can I get default value using #ModelAttribute in Spring MVC? [duplicate]

What is the purpose and usage of #ModelAttribute in Spring MVC?
#ModelAttribute refers to a property of the Model object (the M in MVC ;)
so let's say we have a form with a form backing object that is called "Person"
Then you can have Spring MVC supply this object to a Controller method by using the #ModelAttribute annotation:
public String processForm(#ModelAttribute("person") Person person){
person.getStuff();
}
On the other hand the annotation is used to define objects which should be part of a Model.
So if you want to have a Person object referenced in the Model you can use the following method:
#ModelAttribute("person")
public Person getPerson(){
return new Person();
}
This annotated method will allow access to the Person object in your View, since it gets automatically added to the Models by Spring.
See "Using #ModelAttribute".
I know this is an old thread, but I thought I throw my hat in the ring and see if I can muddy the water a little bit more :)
I found my initial struggle to understand #ModelAttribute was a result of Spring's decision to combine several annotations into one. It became clearer once I split it into several smaller annotations:
For parameter annotations, think of #ModelAttribute as the equivalent of #Autowired + #Qualifier i.e. it tries to retrieve a bean with the given name from the Spring managed model. If the named bean is not found, instead of throwing an error or returning null, it implicitly takes on the role of #Bean i.e. Create a new instance using the default constructor and add the bean to the model.
For method annotations, think of #ModelAttribute as the equivalent of #Bean + #Before, i.e. it puts the bean constructed by user's code in the model and it's always called before a request handling method.
Figuratively, I see #ModelAttribute as the following (please don't take it literally!!):
#Bean("person")
#Before
public Person createPerson(){
return new Person();
}
#RequestMapping(...)
public xxx handlePersonRequest( (#Autowired #Qualifier("person") | #Bean("person")) Person person, xxx){
...
}
As you can see, Spring made the right decision to make #ModelAttribute an all-encompassing annotation; no one wants to see an annotation smorgasbord.
So I will try to explain it in simpler way. Let's have:
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
}
As described in the Spring MVC documentation - the #ModelAttribute annotation can be used on methods or on method arguments. And of course we can have both use at the same time in one controller.
1.Method annotation
#ModelAttribute("cities")
public List<String> checkOptions(){
return new Arrays.asList(new[]{"Sofia","Pleven","Ruse"});//and so on
}
Purpose of such method is to add attribute in the model. So in our case cities key will have the list new Arrays.asList(new[]{"Sofia","Pleven","Ruse"}) as value in the Model (you can think of Model as map(key:value)). #ModelAttribute methods in a controller are invoked before #RequestMapping methods, within the same controller.
Here we want to add to the Model common information which will be used in the form to display to the user. For example it can be used to fill a HTML select:
2.Method argument
public String findPerson(#ModelAttriute(value="person") Person person) {
//..Some logic with person
return "person.jsp";
}
An #ModelAttribute on a method argument indicates the argument should be retrieved from the model. So in this case we expect that we have in the Model person object as key and we want to get its value and put it to the method argument Person person. If such does not exists or (sometimes you misspell the (value="persson")) then Spring will not find it in the Model and will create empty Person object using its defaults. Then will take the request parameters and try to data bind them in the Person object using their names.
name="Dmitrij"&countries=Lesoto&sponsor.organization="SilkRoad"&authorizedFunds=&authorizedHours=&
So we have name and it will be bind to Person.name using setName(String name). So in
//..Some logic with person
we have access to this filled name with value "Dimitrij".
Of course Spring can bind more complex objects like Lists, Maps, List of Sets of Maps and so on but behind the scene it makes the data binding magic.
We can have at the same time model annotated method and request method handler with #ModelAttribute in the arguments. Then we have to union the rules.
Of course we have tons of different situations - #ModelAttribute methods can also be defined in an #ControllerAdvice and so on...
For my style, I always use #ModelAttribute to catch object from spring form jsp. for example, I design form on jsp page, that form exist with commandName
<form:form commandName="Book" action="" methon="post">
<form:input type="text" path="title"></form:input>
</form:form>
and I catch the object on controller with follow code
public String controllerPost(#ModelAttribute("Book") Book book)
and every field name of book must be match with path in sub-element of form
I know I am late to the party, but I'll quote like they say,
"better be late than never". So let us get going,
Everybody has their own ways to explain things, let me try to sum it up and simple it up for you in a few steps with an example;
Suppose you have a simple form, form.jsp:
<form:form action="processForm" modelAttribute="student">
First Name : <form:input path="firstName" />
<br/><br/>
Last Name : <form:input path="lastName" />
<br/><br/>
<input type="submit" value="submit"/>
</form:form>
<form:input path="firstName" /> <form:input path="lastName" /> These are the fields/properties in the Student class. When the form is called/initialized their getters are invoked. On form submit their setters are invoked, and their values are transferred in the bean that was indicated with modelAttribute="student" in the form tag.
We have StudentController that includes the following methods:
#RequestMapping("/showForm")
// `Model` is used to pass data between controllers and views
public String showForm(Model theModel) {
// attribute name, value
theModel.addAttribute("student", new Student());
return "form";
}
#RequestMapping("/processForm")
public String processForm(#ModelAttribute("student") Student theStudent) {
System.out.println("theStudent :"+ theStudent.getLastName());
return "form-details";
}
//#ModelAttribute("student") Student theStudent
//Spring automatically populates the object data with form data
//all behind the scenes
Now finally we have a form-details.jsp:
<b>Student Information</b>
${student.firstName}
${student.lastName}
So back to the question What is #ModelAttribute in Spring MVC?
A sample definition from the source for you,
http://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation
The #ModelAttribute is an annotation that binds a method parameter or method return value to a named model attribute and then exposes it to a web view.
What actually happens is it gets all the values of your form those were submitted by it and then holds them for you to bind or assign them to the object. It works like the #RequestParameter where we only get a parameter and assign the value to some method argument.
The difference is that #ModelAttribute holds all form data rather than a single parameter. It creates a bean for you which holds the data submitted in the form.
To recap the whole thing:
Step 1:
A request is sent and our method showForm() runs and a model, a temporary bean, is set with the name student and forwarded to the form:
theModel.addAttribute("student", new Student());
Step 2:
Form attribute modelAttribute="student" defines that on form submission the model will update the student and will hold all parameters of the form.
Step 3:
On form submission the processForm() method is invoked with parameter #ModelAttribute("student") Student theStudent: the values being hold in the form with modelAttribute="student" were fetched and assigned to the fields in the Student object.
Step 4:
And then we use it as we bid, just like showing it on the page etc like I did
I hope it helps you to understand the concept. Thanks
Take any web application whether it is Gmail or Facebook or Instagram or any other web application, it's all about exchanging data or information between the end user and the application or the UI and the back end application. Even in Spring MVC world there are two ways to exchange data:
from the Controller to the UI, and
from the UI to the Controller.
What we are interested here is how the data is communicated from the UI to Controller. This can also be done in 2 ways:
Using an HTML Form
Using Query Parameters.
Using an HTML Form:
Consider the below scenario,
When we submit the form data from the web browser, we can access that data in our Controller class as an object. When we submit an HTML form, the Spring Container does four things. It will,
first read all the data that is submitted that comes in the request using the request.getParameter method.
once it reads them, it will convert them into the appropriate Java type using integer.parseInt, double.parseDouble and all the other parse methods that are available based on the data type of the data.
once parsed, it will create a object of the model class that we created. For example, in this scenario, it is the user information that is being submitted and we create a class called User, which the Container will create an object of and it will set all the values that come in automatically into that object.
it will then handover that object by setting the values to the Controller.
To get this whole thing to work, we'll have to follow certain steps.
We first need to define a model class, like User, in which the number of fields should exactly match the number of fields in the HTML form. Also, the names that we use in the HTML form should match the names that we have in the Java class. These two are very important. Names should match, the number of fields in the form should match the number of fields in the class that we create. Once we do that, the Container will automatically read the data that comes in, creates an object of this model, sets the values and it hands it over to the Controller. To read those values inside the Controller, we use the #ModelAttribute annotation on the method parameters. When we create methods in the Controller, we are going to use the #ModelAttribute and add a parameter to it which will automatically have this object given by the Container.
Here is an example code for registering an user:
#RequestMapping(value = "registerUser", method = RequestMethod.POST)
public String registerUser(#ModelAttribute("user") User user, ModelMap model) {
model.addAttribute("user", user);
return "regResult";
}
Hope this diagrammatic explanation helped!
#ModelAttribute can be used as the method arguments / parameter or before the method declaration.
The primary objective of this annotation to bind the request parameters or form fields to an model object
Ref. http://www.javabeat.net/modelattribute-spring-mvc/
This is used for data binding purposes in Spring MVC. Let you have a jsp having a form element in it e.g
on JSP
<form:form action="test-example" method="POST" commandName="testModelAttribute"> </form:form>
(Spring Form method, Simple form element can also be used)
On Controller Side
#RequestMapping(value = "/test-example", method = RequestMethod.POST)
public ModelAndView testExample(#ModelAttribute("testModelAttribute") TestModel testModel, Map<String, Object> map,...) {
}
Now when you will submit the form the form fields values will be available to you.
Annotation that binds a method parameter or method return value to a named model attribute, exposed to a web view.
public String add(#ModelAttribute("specified") Model model) {
...
}
#ModelAttribute will create a attribute with the name specified by you (#ModelAttribute("Testing") Test test) as Testing in the given example ,Test being the bean test being the reference to the bean and Testing will be available in model so that you can further use it on jsp pages for retrieval of values that you stored in you ModelAttribute.
#ModelAttribute simply binds the value from jsp fields to Pojo calss to perform our logic in controller class. If you are familiar with struts, then this is like populating the formbean object upon submission.
The ModelAttribute annotation is used as part of a Spring MVC Web application and can be used in two scenarios.
First of all, it can be used to inject data into a pre-JSP load model. This is especially useful in ensuring that a JSP is required to display all the data itself. An injection is obtained by connecting one method to the model.
Second, it can be used to read data from an existing model and assign it to the parameters of the coach's method.
refrence https://dzone.com/articles/using-spring-mvc%E2%80%99s
At the Method Level
1.When the annotation is used at the method level it indicates the purpose of that
method is to add one or more model attributes
#ModelAttribute
public void addAttributes(Model model) {
model.addAttribute("india", "india");
}
At the Method Argument
1. When used as a method argument, it indicates the argument should be retrieved from the model. When not present and should be first instantiated and then added to the model and once present in the model, the arguments fields should be populated from all request parameters that have matching names So, it binds the form data with a bean.
#RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
public String submit(#ModelAttribute("employee") Employee employee) {
return "employeeView";
}
First of all, Models are used in MVC Spring (MVC = Model, View, Controller). This said, models are used together with "views".
What are these views? Views can be though as "html pages that are generated by our backend framework (Spring in our case) with some variable data in some parts of the html page".
So we have the model, which is an entity containing the data do be "injected" into the view.
There are several "view" libraries that you can work with Spring: among which, JSP, Thymeleaf, Mustache and others.
For example, let us assume we are using Thymeleaf (they are all similar. What's more, Spring does not even know, except for JSP, with which view libraries he is working with. All the models are served through the Servlet of Spring. This means that the Spring code will be the same for all these view libraries. The only thing you need to change is the syntax of such html pages, which are located in resources/static/templates)
resources/static/templates //All our view web pages are here
A Controller takes care of the routes. Let's say for example we have our site hosted on localhost:8080. We want a route (URL) showing us the students. Let us say that this is available at localhost:8080/students. The controller that will do this is StudentController:
#Controller //Not #RestController
public class StudentController {
#GetMapping(/students)
public String getStudentView() {
return "student";
}
}
What this code does, is saying that, if we are going to
localhost:8080/students
then the method getStudentView() is called. But notice it should return a String. However, when working with a view library, and the controller is annotated with #Controller (and not #RestController), what spring does is looking for an html view page with the name of the String that the method returns, in our case it will look for the view at
/resources/static/templates/student.html
This is good enough for static pages without data. However, if we need a dynamic page with some data, Spring offers another big advantage: the method above getStudentView(), will also pass, under the hood, a model to our view "student.html". Our model will contain data that we can access in the file "student.html" using the specific syntax from our view library. E.g., with thymeleaf:
<div th:text="${attribute1}"> </div>
This will access the attribute "attribute1" of our model.
We can pass different data through our model. This is done by assigning it various attributes. There are different ways of assigning attributes, with #ModelAttribute:
#Controller //Not #RestController
public class StudentController {
#ModelAttribute(name = "attribute1")
public int assignAttribute1() {
return 123454321
} // Think it as "model.attribute1 = 123454321"
#GetMapping(/students)
public String getStudentView() {
return "student";
}
}
The code above will assign to the model (created under the hood), an attribute with name "attribute1" (think it as of a key), with value 12354321. Something like "model.attribute1 = 123454321".
Finally, the model is passed to the view when we go to the url
localhost:8080/students
Notice: all the methods annotated with #ModelAttribute are invoked before a view is returned. The model, once all the attributes are created, is passed to our view. simply put, after the method getStudentView() is called, all the method with #ModelAttribute are called.
This being said, the html code written above, will be viewed from the browser as:
<div> 123454321 </div> // th:text is a command of
//THymeleaf, and says to substitute the text
// between the tags with the attribute "attribute1"
// of our model passed to this view.
This is the basic usage of #ModelAttribute.
There is also another important use case:
The model might be needed in the opposite direction: i.e., from the view to the controller. In the case described above, the model is passed from the controller to the view. However, let us say that the user, from our html page, sends back some data. We can catch it with out model attributes, #ModelAttribute. This has already been described by others

Hibernate validation property path for validation List with JSR-308

I have following code:
public class ReferEmailForm {
#Valid
#NotEmpty
private Set<#TypedEmail String> emails;
...
}
For some reason error from #TypedEmail in BindingResult is notified as attached to emails[] field. Is there any option to override this name? I need this to be able to use <form:errors path="emails" /> in jsp. Current solution don't generate error message on form unfortunately.
After searching for answer everywhere I just noticed how stupid mistake I did. Hibernate is resolving variable name as emails[] because Set do not have index based access. After changing type to List it returns emails[0] which is way better.

Converting #RequestBody to an object

Guys, Well I have done enough research still I can't find the solution to this.
In a nutshell, I'm simply passing url encoded form data to the Controller method and trying to convert it as a domain object which has Date and integers.
#RequestMapping(value = "/savePassport", method = RequestMethod.POST)
public #ResponseBody
AjaxResponse savePassport(#RequestBody StaffPassport passport, HttpServletResponse response) {
// Some operations.
}
The Staff Passport looks like this:
import java.sql.Date;
public class StaffPassport {
private int staffId;
private String passportNumber;
private String placeOfIssue;
private Date issueDate;
private Date expiryDate;
private String spouseName;
private String oldPassportRef;
private String visaInfo;
private String description;
//gets/sets
}
When I invoke the /savePassport, I get unsupported media exception. I guess it's related to casting.
I can't this working right. Of course I can catch individual form data using #RequestParam and manually do the casting but that's not the point of a framework isn't it?
Where am I going wrong? And you are right. I'm a beginner in Spring, but I love it.
Looks like you're using the wrong annotation. #RequestBody is for taking a request that has arbitrary content in its body,such as JSON, some application defined XML, comma separated variables.. whatever. And using a marshaller that you configure in the dispatcher servlet to turn it into objects.
If all you want to do is ask Spring to bind a plain old form post onto the backing object for you, the correct annotation to put on the method parameter is #ModelAttribute.
If you are posting a JSON Object with jQuery and you want Spring to be able to process it with #RequestBody, use JSON.stringify(....) in your data. Here an example:
var data = { "id": 3, "name": "test" }
$.post("processJsonData.html",JSON.stringify(data), function(data){
...
}
);
If you don't use the JSON.stringify() then you will submit the data as form data and Spring will tell you that you have an unsupported media type.
First of all be sure that you have
<mvc:annotation-driven />
in your Spring configuration file. This is mandatory for working with JSOn in SPring MVC.
Second, I recommend you to test wether request to the server has application/json content type. I belive Fiddler2 will help you to do so.
Third, but I'm not sure about it, Try to change Date items in your POJO from SQL type to regular java type.
UPDATE:
just looked at the Form and it seems like your "Accept" HTTP Header should be also application/json. Please test this issue with Fiddler2 as well.
I assume that you are posting JSON and want Spring to convert to StaffPassport. If you are getting an Unsupported media exception, it is because Spring could not figure out an appropriate way to perform the conversion.
For Spring to convert JSON, it needs Jackson -- make sure you have the Jackson jars in your project. If this is a Maven based project you can add the jackson-mapper-asl artifact ID to your pom.xml. This should give you the jackson-mapper and jackson-core jars.
Edit: I should mention that this applies to Spring 3 (I recently ran into this problem). I'm not sure what else is required for previous versions of Spring.
Check into HttpMessageConverter interface and its implementations. You could write your own implementation of it to convert it to the domain model you want. By the time the control gets to your method, you can access it as if your domain model object is passed.
Ok, I think I should refine my answer. I do not have direct experience of using it in a spring-mvc project but spring-integration. I am pretty sure the applicable media type (application/x-url-form-encoded) is already handled and converted to MultiMap by Spring framework; so, retrieve the values from that just like any other map with the key value being your form variable and populate your business model.
HTH.

What is #ModelAttribute in Spring MVC?

What is the purpose and usage of #ModelAttribute in Spring MVC?
#ModelAttribute refers to a property of the Model object (the M in MVC ;)
so let's say we have a form with a form backing object that is called "Person"
Then you can have Spring MVC supply this object to a Controller method by using the #ModelAttribute annotation:
public String processForm(#ModelAttribute("person") Person person){
person.getStuff();
}
On the other hand the annotation is used to define objects which should be part of a Model.
So if you want to have a Person object referenced in the Model you can use the following method:
#ModelAttribute("person")
public Person getPerson(){
return new Person();
}
This annotated method will allow access to the Person object in your View, since it gets automatically added to the Models by Spring.
See "Using #ModelAttribute".
I know this is an old thread, but I thought I throw my hat in the ring and see if I can muddy the water a little bit more :)
I found my initial struggle to understand #ModelAttribute was a result of Spring's decision to combine several annotations into one. It became clearer once I split it into several smaller annotations:
For parameter annotations, think of #ModelAttribute as the equivalent of #Autowired + #Qualifier i.e. it tries to retrieve a bean with the given name from the Spring managed model. If the named bean is not found, instead of throwing an error or returning null, it implicitly takes on the role of #Bean i.e. Create a new instance using the default constructor and add the bean to the model.
For method annotations, think of #ModelAttribute as the equivalent of #Bean + #Before, i.e. it puts the bean constructed by user's code in the model and it's always called before a request handling method.
Figuratively, I see #ModelAttribute as the following (please don't take it literally!!):
#Bean("person")
#Before
public Person createPerson(){
return new Person();
}
#RequestMapping(...)
public xxx handlePersonRequest( (#Autowired #Qualifier("person") | #Bean("person")) Person person, xxx){
...
}
As you can see, Spring made the right decision to make #ModelAttribute an all-encompassing annotation; no one wants to see an annotation smorgasbord.
So I will try to explain it in simpler way. Let's have:
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
}
As described in the Spring MVC documentation - the #ModelAttribute annotation can be used on methods or on method arguments. And of course we can have both use at the same time in one controller.
1.Method annotation
#ModelAttribute("cities")
public List<String> checkOptions(){
return new Arrays.asList(new[]{"Sofia","Pleven","Ruse"});//and so on
}
Purpose of such method is to add attribute in the model. So in our case cities key will have the list new Arrays.asList(new[]{"Sofia","Pleven","Ruse"}) as value in the Model (you can think of Model as map(key:value)). #ModelAttribute methods in a controller are invoked before #RequestMapping methods, within the same controller.
Here we want to add to the Model common information which will be used in the form to display to the user. For example it can be used to fill a HTML select:
2.Method argument
public String findPerson(#ModelAttriute(value="person") Person person) {
//..Some logic with person
return "person.jsp";
}
An #ModelAttribute on a method argument indicates the argument should be retrieved from the model. So in this case we expect that we have in the Model person object as key and we want to get its value and put it to the method argument Person person. If such does not exists or (sometimes you misspell the (value="persson")) then Spring will not find it in the Model and will create empty Person object using its defaults. Then will take the request parameters and try to data bind them in the Person object using their names.
name="Dmitrij"&countries=Lesoto&sponsor.organization="SilkRoad"&authorizedFunds=&authorizedHours=&
So we have name and it will be bind to Person.name using setName(String name). So in
//..Some logic with person
we have access to this filled name with value "Dimitrij".
Of course Spring can bind more complex objects like Lists, Maps, List of Sets of Maps and so on but behind the scene it makes the data binding magic.
We can have at the same time model annotated method and request method handler with #ModelAttribute in the arguments. Then we have to union the rules.
Of course we have tons of different situations - #ModelAttribute methods can also be defined in an #ControllerAdvice and so on...
For my style, I always use #ModelAttribute to catch object from spring form jsp. for example, I design form on jsp page, that form exist with commandName
<form:form commandName="Book" action="" methon="post">
<form:input type="text" path="title"></form:input>
</form:form>
and I catch the object on controller with follow code
public String controllerPost(#ModelAttribute("Book") Book book)
and every field name of book must be match with path in sub-element of form
I know I am late to the party, but I'll quote like they say,
"better be late than never". So let us get going,
Everybody has their own ways to explain things, let me try to sum it up and simple it up for you in a few steps with an example;
Suppose you have a simple form, form.jsp:
<form:form action="processForm" modelAttribute="student">
First Name : <form:input path="firstName" />
<br/><br/>
Last Name : <form:input path="lastName" />
<br/><br/>
<input type="submit" value="submit"/>
</form:form>
<form:input path="firstName" /> <form:input path="lastName" /> These are the fields/properties in the Student class. When the form is called/initialized their getters are invoked. On form submit their setters are invoked, and their values are transferred in the bean that was indicated with modelAttribute="student" in the form tag.
We have StudentController that includes the following methods:
#RequestMapping("/showForm")
// `Model` is used to pass data between controllers and views
public String showForm(Model theModel) {
// attribute name, value
theModel.addAttribute("student", new Student());
return "form";
}
#RequestMapping("/processForm")
public String processForm(#ModelAttribute("student") Student theStudent) {
System.out.println("theStudent :"+ theStudent.getLastName());
return "form-details";
}
//#ModelAttribute("student") Student theStudent
//Spring automatically populates the object data with form data
//all behind the scenes
Now finally we have a form-details.jsp:
<b>Student Information</b>
${student.firstName}
${student.lastName}
So back to the question What is #ModelAttribute in Spring MVC?
A sample definition from the source for you,
http://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation
The #ModelAttribute is an annotation that binds a method parameter or method return value to a named model attribute and then exposes it to a web view.
What actually happens is it gets all the values of your form those were submitted by it and then holds them for you to bind or assign them to the object. It works like the #RequestParameter where we only get a parameter and assign the value to some method argument.
The difference is that #ModelAttribute holds all form data rather than a single parameter. It creates a bean for you which holds the data submitted in the form.
To recap the whole thing:
Step 1:
A request is sent and our method showForm() runs and a model, a temporary bean, is set with the name student and forwarded to the form:
theModel.addAttribute("student", new Student());
Step 2:
Form attribute modelAttribute="student" defines that on form submission the model will update the student and will hold all parameters of the form.
Step 3:
On form submission the processForm() method is invoked with parameter #ModelAttribute("student") Student theStudent: the values being hold in the form with modelAttribute="student" were fetched and assigned to the fields in the Student object.
Step 4:
And then we use it as we bid, just like showing it on the page etc like I did
I hope it helps you to understand the concept. Thanks
Take any web application whether it is Gmail or Facebook or Instagram or any other web application, it's all about exchanging data or information between the end user and the application or the UI and the back end application. Even in Spring MVC world there are two ways to exchange data:
from the Controller to the UI, and
from the UI to the Controller.
What we are interested here is how the data is communicated from the UI to Controller. This can also be done in 2 ways:
Using an HTML Form
Using Query Parameters.
Using an HTML Form:
Consider the below scenario,
When we submit the form data from the web browser, we can access that data in our Controller class as an object. When we submit an HTML form, the Spring Container does four things. It will,
first read all the data that is submitted that comes in the request using the request.getParameter method.
once it reads them, it will convert them into the appropriate Java type using integer.parseInt, double.parseDouble and all the other parse methods that are available based on the data type of the data.
once parsed, it will create a object of the model class that we created. For example, in this scenario, it is the user information that is being submitted and we create a class called User, which the Container will create an object of and it will set all the values that come in automatically into that object.
it will then handover that object by setting the values to the Controller.
To get this whole thing to work, we'll have to follow certain steps.
We first need to define a model class, like User, in which the number of fields should exactly match the number of fields in the HTML form. Also, the names that we use in the HTML form should match the names that we have in the Java class. These two are very important. Names should match, the number of fields in the form should match the number of fields in the class that we create. Once we do that, the Container will automatically read the data that comes in, creates an object of this model, sets the values and it hands it over to the Controller. To read those values inside the Controller, we use the #ModelAttribute annotation on the method parameters. When we create methods in the Controller, we are going to use the #ModelAttribute and add a parameter to it which will automatically have this object given by the Container.
Here is an example code for registering an user:
#RequestMapping(value = "registerUser", method = RequestMethod.POST)
public String registerUser(#ModelAttribute("user") User user, ModelMap model) {
model.addAttribute("user", user);
return "regResult";
}
Hope this diagrammatic explanation helped!
#ModelAttribute can be used as the method arguments / parameter or before the method declaration.
The primary objective of this annotation to bind the request parameters or form fields to an model object
Ref. http://www.javabeat.net/modelattribute-spring-mvc/
This is used for data binding purposes in Spring MVC. Let you have a jsp having a form element in it e.g
on JSP
<form:form action="test-example" method="POST" commandName="testModelAttribute"> </form:form>
(Spring Form method, Simple form element can also be used)
On Controller Side
#RequestMapping(value = "/test-example", method = RequestMethod.POST)
public ModelAndView testExample(#ModelAttribute("testModelAttribute") TestModel testModel, Map<String, Object> map,...) {
}
Now when you will submit the form the form fields values will be available to you.
Annotation that binds a method parameter or method return value to a named model attribute, exposed to a web view.
public String add(#ModelAttribute("specified") Model model) {
...
}
#ModelAttribute will create a attribute with the name specified by you (#ModelAttribute("Testing") Test test) as Testing in the given example ,Test being the bean test being the reference to the bean and Testing will be available in model so that you can further use it on jsp pages for retrieval of values that you stored in you ModelAttribute.
#ModelAttribute simply binds the value from jsp fields to Pojo calss to perform our logic in controller class. If you are familiar with struts, then this is like populating the formbean object upon submission.
The ModelAttribute annotation is used as part of a Spring MVC Web application and can be used in two scenarios.
First of all, it can be used to inject data into a pre-JSP load model. This is especially useful in ensuring that a JSP is required to display all the data itself. An injection is obtained by connecting one method to the model.
Second, it can be used to read data from an existing model and assign it to the parameters of the coach's method.
refrence https://dzone.com/articles/using-spring-mvc%E2%80%99s
At the Method Level
1.When the annotation is used at the method level it indicates the purpose of that
method is to add one or more model attributes
#ModelAttribute
public void addAttributes(Model model) {
model.addAttribute("india", "india");
}
At the Method Argument
1. When used as a method argument, it indicates the argument should be retrieved from the model. When not present and should be first instantiated and then added to the model and once present in the model, the arguments fields should be populated from all request parameters that have matching names So, it binds the form data with a bean.
#RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
public String submit(#ModelAttribute("employee") Employee employee) {
return "employeeView";
}
First of all, Models are used in MVC Spring (MVC = Model, View, Controller). This said, models are used together with "views".
What are these views? Views can be though as "html pages that are generated by our backend framework (Spring in our case) with some variable data in some parts of the html page".
So we have the model, which is an entity containing the data do be "injected" into the view.
There are several "view" libraries that you can work with Spring: among which, JSP, Thymeleaf, Mustache and others.
For example, let us assume we are using Thymeleaf (they are all similar. What's more, Spring does not even know, except for JSP, with which view libraries he is working with. All the models are served through the Servlet of Spring. This means that the Spring code will be the same for all these view libraries. The only thing you need to change is the syntax of such html pages, which are located in resources/static/templates)
resources/static/templates //All our view web pages are here
A Controller takes care of the routes. Let's say for example we have our site hosted on localhost:8080. We want a route (URL) showing us the students. Let us say that this is available at localhost:8080/students. The controller that will do this is StudentController:
#Controller //Not #RestController
public class StudentController {
#GetMapping(/students)
public String getStudentView() {
return "student";
}
}
What this code does, is saying that, if we are going to
localhost:8080/students
then the method getStudentView() is called. But notice it should return a String. However, when working with a view library, and the controller is annotated with #Controller (and not #RestController), what spring does is looking for an html view page with the name of the String that the method returns, in our case it will look for the view at
/resources/static/templates/student.html
This is good enough for static pages without data. However, if we need a dynamic page with some data, Spring offers another big advantage: the method above getStudentView(), will also pass, under the hood, a model to our view "student.html". Our model will contain data that we can access in the file "student.html" using the specific syntax from our view library. E.g., with thymeleaf:
<div th:text="${attribute1}"> </div>
This will access the attribute "attribute1" of our model.
We can pass different data through our model. This is done by assigning it various attributes. There are different ways of assigning attributes, with #ModelAttribute:
#Controller //Not #RestController
public class StudentController {
#ModelAttribute(name = "attribute1")
public int assignAttribute1() {
return 123454321
} // Think it as "model.attribute1 = 123454321"
#GetMapping(/students)
public String getStudentView() {
return "student";
}
}
The code above will assign to the model (created under the hood), an attribute with name "attribute1" (think it as of a key), with value 12354321. Something like "model.attribute1 = 123454321".
Finally, the model is passed to the view when we go to the url
localhost:8080/students
Notice: all the methods annotated with #ModelAttribute are invoked before a view is returned. The model, once all the attributes are created, is passed to our view. simply put, after the method getStudentView() is called, all the method with #ModelAttribute are called.
This being said, the html code written above, will be viewed from the browser as:
<div> 123454321 </div> // th:text is a command of
//THymeleaf, and says to substitute the text
// between the tags with the attribute "attribute1"
// of our model passed to this view.
This is the basic usage of #ModelAttribute.
There is also another important use case:
The model might be needed in the opposite direction: i.e., from the view to the controller. In the case described above, the model is passed from the controller to the view. However, let us say that the user, from our html page, sends back some data. We can catch it with out model attributes, #ModelAttribute. This has already been described by others

How do I write a custom converter in Spring Web Flow 2?

I am using Web Flow 2.0.7 with Spring MVC and Hibernate.
My problem is about custom converters for my custom types and database connection from within my converter.
Let's say I have a type Person and the Person has a field of my custom type Title, and all Titles are already in my database. Now I have an html form, in which a user can populate a Person instance, including selecting the Title in a select drop-down box.
In the flow definition I get all Titles from the database and they are shown in the dropdown box using a custom converter, converting Title to String and later back to Title.
My question is about the process of converting back from String (which is the database ID, which I set as value on the element) to the correct Title object from my database. Basically: How to do it?
So far, I was unable to get a titleManager injected into my converter to get access to the database. This scenario was commented on in the Spring Web Flow Forum. Another solution might be to cache the Titles before rendering the view and somehow get the in-memory Title after the form was POSTed.
I would really appreciate it, if someone could enlighten me, how to handle this kind of data binding. I was unable to get it working so far and thus, I get minimal use out of the otherwise awesome webflows.
I already posted a thread on the Web Flow Board, but still missing a best-pratice, which I am unable to find by myself.
Thank you so much!
Wolfram
I use to do this.
Basically I load the list of Titles and put it in my form model. In the form model I have also a currentTitleId or selectedTitleId variable to store the value of the selected item. This field name is set in the "path" of the spring combobox and the titleList is set in the "items". Then the value that you want to bind is set in the "itemValue" and the text to be shown for that value in "itemLabel". That's it.
In my form model:
private int currentTitleId;
public long getCurrentTitleId() { return this.currentTitleId; }
public void setCurrentTitleId(long currentTitleId) { this.currentTitleId = currentTitleId; }
List titleList = getTitlesFromMyDatabaseHereOrSomewhereElse();
In my jsp:
<form:label path="currentTitleId">Title</form:label>
<form:select path="currentTitleId" items="${formModel.titleList}" itemLabel="titleDescription" itemValue="titleId" />
I assume that your Title class will be something like this:
class Title {
public long getTitleId() { return this.titleId; }
public long getTitleDescription() { return this.titleDescription; }
}
You can also customize you combobox a little more like this:
<form:select path="currentPhoneNumberId">
<form:option value="">-</form:option>
<c:forEach items="${formModel.phoneList}" var="phone">
<form:option value="${phone.phoneNumberId}">${phone.phoneNumberId} - ${phone.description}</form:option>
</c:forEach>
</form:select>
I'm not so sure about Spring Web Flow, but with the normal Spring MVC it is sufficient to register a new PropertyEditor and then this stuff works automatically
http://static.springframework.org/spring/docs/2.5.x/reference/validation.html#beans-beans-conversion-customeditor-registration
http://static.springframework.org/spring/docs/2.5.x/reference/mvc.html#mvc-ann-webdatabinder
So I would create a new PropertyEditor which gets a service or dao which would be responsible for getting the data from the database, and within the PropertyEditor you would convert the id to your key type and get the value from the database and return it. I just don't have an example right with me, but I hope you get the gist.

Categories

Resources