Spring MVC Example SimpleFormController

Spring MVC Example by using SimpleFormController.

Steps....
1. Create project by using IDE(any) - i am using MyEclipse.


2. Create package and Add Spring jar files

3. Configure web.xml with following details.

<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>   
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 
  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfingLocation</param-name>
        <param-value>WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

</web-app>

4. Create another configuration file with servlet-name-servlet(spring-servlet)

5. create folder pages and add following beans to spring-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-3.1.xsd">

    <bean id="jspView"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
       
</beans>


6. Develop Contact Class and provide Setters and Getters to fields.

package com.javafws.blog.model;

public class Contacts implements java.io.Serializable {

    // Fields
    private Short id;
    private String firstname;
    private String lastname;
    private String telephone;
    private String email;


    // Generate Setters and Getters.
}


7. Develop FormView and place inside the pages folder.

AddContact.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">

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

Please Enter at least one field
<br>
<form:form method="post" action="AddContact.html" commandName="contacts">
    <table>
     <tr>
        <td><form:label path="id">ID</form:label></td>
        <td><form:input path="id" /></td>
    </tr>
    <tr>
        <td><form:label path="firstname">First Name</form:label></td>
        <td><form:input path="firstname" /></td>
    </tr>
    <tr>
        <td><form:label path="lastname">Last Name</form:label></td>
        <td><form:input path="lastname" /></td>
    </tr>
    <tr>
        <td><form:label path="telephone">Telephone</form:label></td>
        <td><form:input path="telephone" /></td>
    </tr>
    <tr>
        <td><form:label path="email">Email</form:label></td>
        <td><form:input path="email" /></td>
    </tr>   
    <tr>
        <td colspan="2">
            <input type="submit" value="Add Contact"/>
        </td>
    </tr>
</table> 

</form:form>
 8.  To do any work in spring we develop controller
AddContactController.java

package com.javafws.blog.controllers;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.javafws.blog.model.Contacts;

@SuppressWarnings("deprecation")
public class AddContactController extends SimpleFormController {

    public AddContactController() {
        setCommandClass(Contacts.class);
        setCommandName("contacts");
    }
   
    @Override
    protected ModelAndView onSubmit(HttpServletRequest request,
        HttpServletResponse response, Object command, BindException errors)  {
       
        Contacts contacts = (Contacts) command;
       
        Short id = contacts.getId();
        String firstname = contacts.getFirstname();
        String lastname = contacts.getLastname();
        String telephone = contacts.getTelephone();
        String email = contacts.getEmail();
       
        ModelAndView mav = new ModelAndView("AddedSuccess","contacts",contacts);
       
        mav.addObject(id);
        mav.addObject(firstname);
        mav.addObject(lastname);
        mav.addObject(telephone);
        mav.addObject(email);
       
        return mav;
    }
}


9. Configure it in spring-servlet.xml file in the following.

    <bean name="/AddContact.html"
        class="com.javafws.blog.controllers.AddContactController">
    </bean>


10. Dovelope SuccessView available in AddContactController.java (ModelAndView("AddedSuccess","contacts",contacts)
AddedSuccess.jsp

Hello ${contacts.firstname}
<br>
Your Entered Details are...
<br>
ID : ${contacts.id}
<br>
First Name : ${contacts.firstname}
<br>
Last Name : ${contacts.lastname}
<br>
Telephone : ${contacts.telephone}
<br>
Email : ${contacts.email}
<br>

<a href="AddContact.html">Add Another Contact</a>



Finally URL is http://localhost:8090/sprmvcex1/AddContact.html 

Download Source Code: Download

 

Tags: ,

2 Responses to “Spring MVC Example SimpleFormController”

Unknown said...
November 25, 2015 at 9:53 PM

Thank you!! . It will be very helpful for us.
Great information on spring mvc. Keep sharing your knowledge.
I would like to share useful things for Spring MVC job seekers spring mvc Interview Questions .


farrayager said...
February 27, 2022 at 11:44 AM

Borgata Hotel Casino & Spa – One of the Best Hotels in Atlantic City
Borgata Hotel Casino & 바카라 커뮤니티 Spa was pci 슬롯 awarded one of Atlantic City's 올인구조대 Top 모바일벳365 10 Hotels, ranked by Travel Guide's win bet win Editors' Choice in 2021. Their casino and resort


Post a Comment

Thanks for your comments

Subscribe

© 2014 Java Frameworks. All rights reserved.
Designed by Blogger