addheader

Sunday, 7 October 2012

Spring MVC SimpleFormController work flow


TUTIORALS FROM MADHAV:


     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY  


Spring MVC "SimpleFormController"  

  Spring MVC  :


Tepes of controllers:
  1. ·         Controller 
  2.           AbstractCommandController
  3. ·         SimpleFormController
  4. ·         WizardFormController
  5. ·         MultiActionController
Exampl 5:  
                                //this program will describes using  “simpleformcontroler

  1. index.html:
    <!DOCTYPE html>
    <html>
      <head>
        <title>indexl.html</title>
                   
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="this is my page">
        <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
       
        <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
      </head>
     
      <body>
        This is my HTML page. <br>
        <a href="">click hear for SimpleFsormController</a>
      </body>
    </html>
    FormDetials.jsp
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
                    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Registration Page</title>
    </head>
    <body bgcolor="pink">
    <form:form method="POST" commandName="user">
                    <table>
                                    <tr>
                                                    <td>Enter Ur Name :</td>
                                                    <td><form:input path="name" /></td>
                                    </tr>
                   
                                   
                                    <tr>
                                                    <td>Enter Address</td>
                                                    <td><form:input path="address" /></td>
                                    </tr>
                                   
                                    <tr>
                                                    <td>Enter Mail ID</td>
                                                    <td><form:input path="mailId" /></td>
                                    </tr>
                                    <tr>
                                                    <td colspan="2"><input type="submit" value="submit"></td>
                                    </tr>
                    </table>
    </form:form>
    </body>
    </html>
    SuccessPage.jsp
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Success Page</title>
    </head>
    <body bgcolor="gray">
    Your Details
    <hr>
    User Name   : ${user.name} <br/>
    Address  : ${user.address} <br/>
    Mail Id  : ${user.mailId}  <br/>
    </body>
    </html>
    Stuednt.java
    package madhav;
    public class Student {
                    private String name;
                    private String address;
                    private String mailId;
                   
                   
                    public String getName() {
                                    return name;
                    }
                    public void setName(String name) {
                                    this.name = name;
                    }
                    public void setAddress(String address) {
                                    this.address = address;
                    }
                    public String getAddress() {
                                    return address;
                    }
                    public void setMailId(String mailId) {
                                    this.mailId = mailId;
                    }
                    public String getMailId() {
                                    return mailId;
                    }
                   
    }
    Controler.java
    package madhav;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.SimpleFormController;
    @SuppressWarnings("deprecation")
    public class Controller extends SimpleFormController {
                    public Controller()               {
                                    setCommandClass(Student.class);
                                    System.out.println("Student controller");
                                    setCommandName("user");
                                    System.out.println("user");
                    }             
                    protected ModelAndView onSubmit(Object command) throws Exception {
                                    Student user = (Student) command;
                                    //userService.add(user);
                                    System.out.println("model and view");
                                    return new ModelAndView("SuccessPage","user",user);
                                   
                    }
                   
    }
    Dispatcher-servlet.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xmlns:p="http://www.springframework.org/schema/p"
                    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
                    <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver"
                                      p:prefix="/WEB-INF/jspfiles/" p:suffix=".jsp" />
       
                    <bean name="/userRegistration.htm" class="madhav.Controller"
                     p:formView="FormDetails" p:successView="SuccessPage"/>
                   
                   
                   
    </beans>
    Web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>SpringExample6</display-name>
      <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    OUT PUT SHORTS:



    Ex 5:
     

No comments:

Post a Comment