Spring MVC - AnnotationController

Person.java
------------------------------

package com.anil.domain;

public class Person {

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}
-----------------------------------------------
PersonService.java
----------------------------------------------
package com.anil.service;

import com.anil.domain.Person;

public interface PersonService {

    public void add(Person person);
}
---------------------------------------------
PersonServiceImpl.java
--------------------------------------------
package com.anil.service;

import com.anil.domain.Person;

public class PersonServiceImpl implements PersonService {

    public void add(Person anil) {
        //Persist the user object here.
        System.out.println("Person added successfully");

    }

}
-------------------------------------------------------------
PersonController.java
-------------------------------------------------------------
package com.anil.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.anil.domain.Person;
import com.anil.service.PersonService;

@Controller
@RequestMapping("/personRegistration.htm")
@SessionAttributes("person")
public class PersonController {

    private PersonService personService;

    @Autowired
    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }
   
    @RequestMapping(method = RequestMethod.GET)
    public String showPersonForm(ModelMap model)
    {
        Person p = new Person();
        model.addAttribute(p);
        return "personForm";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String onSubmit(@ModelAttribute("person") Person person) {
        personService.add(person);
        return "redirect:personSuccess.htm";
    }
   
}
----------------------------------------------------------------------------
PersonSuccessController.java
---------------------------------------------------------------------------
package com.anil.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class PersonSuccessController {

    @RequestMapping("/personSuccess.htm")
    public String redirect()
    {
        return "personSuccess";
    }
}
---------------------------------------------------------------------------
PersonForm.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>

        <form:form method="POST" commandName="person">
            <table>
                <tr>
                    <td>
                        Person Name :
                    </td>
                    <td>
                        <form:input path="name" />
                    </td>
                </tr>
                <tr>
                    <td>
                        Age :
                    </td>
                    <td>
                        <form:input path="age" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="submit">
                    </td>
                </tr>
            </table>
        </form:form>

    </body>
</html>
----------------------------------------------------------------
personSucceess.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>
Person Details
<hr>
User Name   : ${person.name} <br/>
Age      : ${person.age} <br/>

</body>
</html>
---------------------------------------------------------------------------------------
anil-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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />


    <bean id="personService" class="com.anil.service.PersonServiceImpl" />

    <context:component-scan base-package="com.anil.web" />

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

</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>SpringExample11</display-name>
    <servlet>
        <servlet-name>anil</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>anil</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>
--------------------------------------------------------------------------------------------------------------
index.jsp
--------------------------------------------------------------------------------------------------------------
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% response.sendRedirect("personRegistration.htm"); %>

No comments:

Post a Comment