Spring 4 MVC Multi Action Controller Annotation

spring+4+mvc+multi+action+controller+annotation

Click here to download eclipse supported ZIP file



This is home.html JSP file and it is used display the output for the application.



<html>
<body>
<h1>Spring 4 MVC Multi Action Controller Annotation</h1>
<h3>
<a href="./customer/add.htm">Click here to get add message !!!</a>
</h3>
<h3>
<a href="./customer/update.htm">Click here to get update message!!!</a>
</h3>
<h3>
<a href="./customer/delete.htm">Click here to get delete message!!!</a>
</h3>
<h3>
<a href="./customer/list.htm">Click here to get  list message!!!</a>
</h3>
</body>
</html>

This is CustomerPage.jsp JSP file and it is used display the output for the application.



<html>
<body>
<h1>Spring MVC Multi Actions Annotation Example</h1>
<h2>${msg}</h2>
</body>
</html>


This is CustomerController.java file having the controller logic and it will have the services defined in it.


 

    
package com.cv.spring.common.controller;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

/**
@author Chandra Vardhan
*/

@Controller
public class CustomerController{
 
  @RequestMapping("/customer/add.htm")
  public ModelAndView add(HttpServletRequest request,
    HttpServletResponse responsethrows Exception {
 
    return new ModelAndView("CustomerPage""msg","add() method");
 
  }
 
  @RequestMapping("/customer/delete.htm")
  public ModelAndView delete(HttpServletRequest request,
    HttpServletResponse responsethrows Exception {
 
    return new ModelAndView("CustomerPage""msg","delete() method");
 
  }
 
  @RequestMapping("/customer/update.htm")
  public ModelAndView update(HttpServletRequest request,
    HttpServletResponse responsethrows Exception {
 
    return new ModelAndView("CustomerPage""msg","update() method");
 
  }
 
  @RequestMapping("/customer/list.htm")
  public ModelAndView list(HttpServletRequest request,
    HttpServletResponse responsethrows Exception {
 
    return new ModelAndView("CustomerPage""msg","list() method");
 
  }
 
}



This is pom.xml file having the entries of dependency jars and information to build the application .


	
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cv.spring.common</groupId> <artifactId>Spring4MVCMultiActionControllerAnnotation</artifactId> <packaging>war</packaging> <version>1.0</version> <name>SpringMVC Maven Webapp</name> http://maven.apache.org <properties> <spring.version>4.2.0.RELEASE</spring.version> <hibernate.version>4.3.5.Final</hibernate.version> <log4j-version>1.2.17</log4j-version> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> provided </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <warSourceDirectory>src/main/webapp</warSourceDirectory> <warName>Spring4MVCMultiActionControllerAnnotation</warName> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>


This is mvc-dispatcher-servlet.xml spring configuration file and these entries are used in the application.


 

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.cv.spring.common.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>


This is web.xml deployment descriptor file and it describes how the web application should be deployed.


	
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<display-name>Spring Web MVC Application</display-name>

<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<welcome-file-list>
<welcome-file>home.html</welcome-file>
</welcome-file-list>
</web-app>


This is log4j.properties file having the entries for logging the information into the console/file.



#By default enabling Console appender
# Root logger option
log4j.rootLogger=INFO, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%c]:%L -->> %m%n

# Redirect log messages to a log file
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C:\servlet-application.log
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

No comments:

Post a Comment