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
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<portlet:actionURL name="student" var="student">
<portlet:param name="action" value="studentSubmit"></portlet:param>
</portlet:actionURL>
<form action="${student}" method="POST" modelAttribute="studentValues">
Name:: <input type="text" name = "studentName"><br>
College::<input type="text" name="collegeName"><br>
Selected Subjects:
<select multiple name="subjects">
<option value="java">JAVA</option>
<option value="c">C</option>
<option value="c++">C++</option>
<option value="dotNet">Dot Net</option>
</select>
<br>
<input type="submit" value="SUBMIT">
</form>
view raw student.jsp hosted with ❤ by GitHub

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
package com.bean;
import java.util.List;
public class StudentForm {
private String studentName;
private String collegeName;
private List<String> subjects;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getCollegeName() {
return collegeName;
}
public void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
public List<String> getSubjects() {
return subjects;
}
public void setSubjects(List<String> subjects) {
this.subjects = subjects;
}
}


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

Student.java
package com.student;
import com.bean.StudentForm;
import java.util.List;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
@Controller(value = "Student")
@RequestMapping("VIEW")
public class Student {
//Default Render Method
@RenderMapping
public String handleRenderRequest(RenderRequest request,RenderResponse response, Model model) {
return "student";
}
@ActionMapping(params = "action=studentSubmit")
public void studentSubmit(@ModelAttribute("studentValues") StudentForm formBean,ActionRequest request, ActionResponse response) {
String studentName = formBean.getStudentName();
String college = formBean.getCollegeName();
List<String> subjects = formBean.getSubjects();
System.out.println("Student Name=>"+studentName);
System.out.println("College =>"+college);
for (String sub : subjects) {
System.out.println("List Subjects are = >"+sub);
}
}
}
view raw Student.java hosted with ❤ by GitHub

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

1041698

Number Of Unique Visitor

Free counters!