----------------------------------------------
We can deal with the forms in our application are implementing the controller class as the subclasses of AbstructWizardFormController or SimpleFormController.
FromView –
------------------------
The server face generates the form to get input is called form view or the JSP that generates the input form is called as form view.
Success View –
------------------------
When the user provides the data in the above form, the application must store the data in the database and generates the output including that completed the operating successfully, the JSP that is responsible to generating the output is called as success view.
SimpleFormController
------------------------
The internal code of SimpleFormController assumes the browser has send the request for form if the request of type GET and it assumes the form is submitted if the request of type POST.
Procedure for using SimpleFormController –
--------------------------------------------------
Provide a form view -
------------------------------
JSP code (npform.jsp):-
< %@ taglib prefix=”c” uri=”http://java.sun.com/jstl/core” %>
< %@ taglib prefix=”spring” uri=”http://www.springframework.org/tags” %>
< %@ page isELIgnored=”true” %>
< form method=”post” action=”sfh.html”>
Prod id: < input type=”text” name=”pid”>< /br>
< spring:bind path=”pcb.pid”>
< c:forEach var=”emsg” items=”${status.errorMessages}”>
< c:out value=”${emsg}”/>
< /c:forEach>
< /spring:bind>
Prod Name: < input type=”text” name=”pname”>< /br>
< spring:bind path=”pcb.pname”>
< c:forEach var=”emsg” items=”${status.errorMessages}”>
< c:out value=”${emsg}”/>
< /c:forEach>
< /spring:bind>
Price: < input type=”text” name=”price”>< /br>
< spring:bind path=”pcb.price”>
< c:forEach var=”emsg” items=”${status.errorMessages}”>
< c:out value=”${emsg}”/>
< /c:forEach>
< /spring:bind>
< input type=”submit” value=”Store”>< /br>
< /form>
Provide Success View –
--------------------------------------
JSP code (pds.jsp):-
< %@ taglib prefix=”c” uri=”http://java.sun.com/jstl/core” %>
< %@ taglib prefix=”spring” uri=”http://www.springframework.org/tags” %>
< %@ page isELIgnored=”true” %>
Successfully Store the Product < br>
Prod Id = ${requestScope.peb.pid} < br>
Prod Name = ${requestScope.peb.pname} < br>
Prod Price = ${requestScope.peb.price} < br>
Provide the command bean class –
-------------------------------------------
package pack;
public class ProductCB {
int pid;
String pname;
float price;
//add setter and getter for all properties
}
Provide code for validation –
--------------------------------------------
//add validator code
Provide Code for Controller class –
---------------------------------------------
package pack;
public class PDSController extends SimpleFormController {
public PDSController () {
this.setCatchSecond(1);
this.setCommandName(“pcb”);
this.setCommandClass(ProductCB.class);
this.setFormView(“npform”);
this.setSuccessView(“pds”);
}
public void doSubmitAction(Object cmd) throws Exception {
ProductCB cb=(ProductCB) cmd;
System.out.println(“Prod id=”+cb.getPid());
}
}
The method doSubmitAction() will be executed only when the data provided by user is valid. If this method throws no exception then the successView will be executed.
Provide the info about the controller in spring-servlet.xml file -
-------------------------------------------------------------------------------
spring-servlet.xml file –
< bean name=”/sfh.htm” class=”pack.PDSController”>< /bean>
SimpleFormController activities when it deals with the form -
-------------------------------------------------------------------------------
Procedure for using AbstructWizardFormController – (Multi Form Handle)
---------------------------------------------------------------------------------
Provide the info about the controller in spring-servlet.xml file -
---------------------------------------------------------------------------------
spring-servlet.xml file –
< bean name=”/mfh.htm” class=”pack.MFController”>< /bean>
Provide the command bean class –
-----------------------------------------------------------
package pack;
public class UserRegCB {
String username;
Stirng pwdOne;
Stirng pwdTwo;
int age;
Stirng fatherName;
Stirng motherName;
Stirng address;
Stirng email;
Stirng contactNo;
//add setter and getter for all properties
}
Provide Code for Controller class –
-----------------------------------------------------------------
package pack;
public class MFController extends
AbstructWizardFormController {
public MFController () {
this.setCommandName(“urcb”);
this.setCommandClass(UserRegCB.class);
String pages[] = new String[3];
pages[0] = “frmz”;
pages[1] = “frmone”;
pages[2] = “frmtwo”;
this.setPages(pages);
}
protected void validatePage(Object cmd, Errors errors, int page) {
switch(page){
case 0: //call page zero validation
break;
case 1: //call page one validation
break;
case 2: //call page two validation
break;
}
}
protected ModelAndView processFinish(HttpServletRequest request,
HttpServletReqponse response, Object cmd, BindException be) {
UserRegCB cb=(UserRegCB) cmd;
//call business logic service class
ModelAndView mav = new ModelAndView();
mav.addAllObjects(be.getModel());
mav.setViewName(“ur”);
}
}
In the above application we are provided the business logic in the controller class. This is a recommended approach; we must provide a separate set of classes called as Business Service classes for the implementation of the business logic.
Provide a form view -
--------------------------------------
JSP code (frmz.jsp):-
< %@ taglib prefix=”c” uri=”http://java.sun.com/jstl/core” %>
< %@ taglib prefix=”spring” uri=”http://www.springframework.org/tags” %>
< %@ page isELIgnored=”true” %>
< form method=”post” action=”mfh.html”>
< input type=”hidden” name=”_page0” value=”true”/>
< input type=”hidden” name=”_target1” value=”true” />
User Name: < input type=”text” name=”userName”>< /br>
< c:forEach var=”emsg” items=”${status.errorMessages}”>
< c:out value=”${emsg}”/>
< /c:forEach>
< /spring:bind>
Password: < input type=”text” name=”pwdOne”>< /br>
< spring:bind path=”pcb.pwdOne”>
< c:forEach var=”emsg” items=”${status.errorMessages}”>
< c:out value=”${emsg}”/>
< /c:forEach>
< /spring:bind>
Retype Password: < input type=”text” name=”pwdTwo”>< /br>
< spring:bind path=”pcb.pwdTwo”>
< c:forEach var=”emsg” items=”${status.errorMessages}”>
< c:out value=”${emsg}”/>
< /c:forEach>
< /spring:bind>
< input type=”submit” value=”Next”>< /br>
< /form>
JSP code (frmone.jsp):-
------------------------------------------
< %@ taglib prefix=”c” uri=”http://java.sun.com/jstl/core” %>
< %@ taglib prefix=”spring” uri=”http://www.springframework.org/tags” %>
< %@ page isELIgnored=”true” %>
< form method=”post” action=”mfh.html”>
< input type=”hidden” name=”_page1” value=”true”/>
< input type=”hidden” name=”_target2” value=”true” />
Age: < input type=”text” name=”age”>< /br>
< spring:bind path=”pcb.age”>
< c:forEach var=”emsg” items=”${status.errorMessages}”>
< c:out value=”${emsg}”/>
< /c:forEach>
< /spring:bind>
Father’s Name: < input type=”text” name=”fatherName”>< /br>
< /spring:bind path=”pcb.fatherName”>
< c:forEach var=”emsg” items=”${status.errorMessages}”>
< c:out value=”${emsg}”/>
< /c:forEach>
< .spring:bind>
Mother Name: < input type=”text” name=”motherName”>< /br>
< spring:bind path=”pcb.motherName”>
< c:forEach var=”emsg” items=”${status.errorMessages}”>
< c:out value=”${emsg}”/>
< /c:forEach>
< /spring:bind>
< input type=”submit” value=”Next”>< /br>
< /form>
JSP code (frmtwo.jsp):-
------------------------------------
< %@ taglib prefix=”c” uri=”http://java.sun.com/jstl/core” %>
< %@ taglib prefix=”spring” uri=”http://www.springframework.org/tags” %>
< %@ page isELIgnored=”true” %>
< form method=”post” action=”mfh.html”>
< input type=”hidden” name=”_page2” value=”true”/>
< input type=”hidden” name=”_finish” value=”true” />
Address: < input type=”text” name=”address”>< /br>
< spring:bind path=”pcb.address”>
< c:forEach var=”emsg” items=”${status.errorMessages}”>
< c:out value=”${emsg}”/>
< /c:forEach>
< /spring:bind>
Email: < input type=”text” name=”email”>< /br>
< spring:bind path=”pcb.email”>
< c:forEach var=”emsg” items=”${status.errorMessages}”>
< c:out value=”${emsg}”/>
< /c:forEach>
< /spring:bind>
Contact Number: < input type=”text” name=”contactNo”>< /br>
< spring:bind path=”pcb.contactNo”>
< c:forEach var=”emsg” items=”${status.errorMessages}”>
< c:out value=”${emsg}”/>
< /c:forEach>
< /spring:bind>
< input type=”submit” value=”Register”>< /br>
< /form>
Provide Success View –
----------------------------------------
JSP code (ur.jsp):-
< %@ taglib prefix=”c” uri=”http://java.sun.com/jstl/core” %>
< %@ taglib prefix=”spring” uri=”http://www.springframework.org/tags” %>
< %@ page isELIgnored=”true” %>
< % System.out.println(“--------ur.jsp--------“); %>
Successfully Register User < br>
User name = ${requestScope.urcb.userName} < br>
Age = ${requestScope.urcb.age} < br>
email = ${requestScope.urcb.email} < br>
The controller classes uses / depends upon the Servlet API and Spring API i.e. the controller classes can be used only as part of spring based web application. If we provide the business logic in the controller classes then we can’t reuse the business logic in the other application.
No comments:
Post a Comment