Monday, November 28, 2016

Fetch Fields using bean in SpringMVC Portlet

We already know about SpringMVC Portlet and how to handle Form Submission in SpringMVC Portlet. The two main benifits of using SpringMVC portlet are:-

1)We can handle multiple Ajax call and create our custom methods for each call.For this you can read my blog Ajax Call in SpringMVC Portlet.

2)We can get all the values of Form in a java bean object.

Suppose we have a form that contain 15 fields like name,lastname etc.So we need to write ParamUtil.getString(); for each form field.But by using SpringMVC portlet you can get all the fields in object.So today we will discuss how to get Form fields using Bean.

so lets start this step by step:-


Step 1:-Create SpringMVC Portlet 
You Can create a SpringMVC Portlet by using my previous blog.


Step 2:-Change your view
Open your view file and create a form in it.

student.jsp

Explanation:-
In the form tag we use modelAttribute :-

<form action="" method="" modelAttribute="studentValues"> 
This field contain all the form values which we fetch in our controller.


Step 3:-Create bean Class
In the form we have 3 fields studentName, collegeName and subjects so we create a java class with 3 fields and create getter and setter for these:-

StudentForm.java


Step 4:-Get value inside controller
Open your controller and paste this:-

Student.java

Explanation:-
In the studentSubmit method we use :- 

@ModelAttribute("studentValues") StudentForm formBean

  • This studentValues must match with modelAttribute inside jsp (see step 2).
  • formBean is object of your bean class.
We can easily get all the values by using formBean object and simple print on console.




Step 5:-Check the output

Deploy the portlet and Check the output:-




Project Structure:-





You can Download Source code from  Fetch Fields using bean in SpringMVC Portlet. 


Hope this will Help....

Related Post:-

No comments:

Post a Comment

Total Pageviews

Like Us on Facebook

Number Of Unique Visitor

Free counters!