Quantcast
Channel: Kruders.com » Spring MVC
Viewing all articles
Browse latest Browse all 10

Spring MultiActionController using Annotation

$
0
0

Using MultiActionController class, you can write multiple actions in into one Action class and removes the creation of multiple action classes. To configure it, define @RequestMapping with mapping URL above the method name to map that method to the specified URL.

First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

<properties>
    <spring.version>3.1.0.RELEASE</spring.version>
</properties>
 
<dependencies>
 
    <!-- Spring 3 dependencies -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</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>
 
</dependencies>

1. Controller

Create a class CalculatorController.java that extends MultiActionController class.

CalculatorController.java

package com.kruders.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;
 
@Controller
public class CalculatorController{
     
    @RequestMapping("/calculator/add.htm")
    public ModelAndView add(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        Integer numberA = Integer.parseInt(request.getParameter("numberA").toString());
        Integer numberB = Integer.parseInt(request.getParameter("numberB").toString());
        Integer result = numberA + numberB;
        return new ModelAndView("calculator", "result", result.toString());
    }
     
    @RequestMapping("/calculator/subtract.htm")
    public ModelAndView subtract(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        Integer numberA = Integer.parseInt(request.getParameter("numberA").toString());
        Integer numberB = Integer.parseInt(request.getParameter("numberB").toString());
        Integer result = numberA - numberB;
        return new ModelAndView("calculator", "result", result.toString());
    }
     
    @RequestMapping("/calculator/multiply.htm")
    public ModelAndView multiply(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        Integer numberA = Integer.parseInt(request.getParameter("numberA").toString());
        Integer numberB = Integer.parseInt(request.getParameter("numberB").toString());
        Integer result = numberA * numberB;
        return new ModelAndView("calculator", "result", result.toString());
    }
     
    @RequestMapping("/calculator/divide.htm")
    public ModelAndView divide(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        Integer numberA = Integer.parseInt(request.getParameter("numberA").toString());
        Integer numberB = Integer.parseInt(request.getParameter("numberB").toString());
        Integer result = numberA / numberB;
        return new ModelAndView("calculator", "result", result.toString());
    }
}

2. View

Now create the jsp page index.jsp that delegates requests.

index.jsp

<a href="calculator/add.htm?numberA=10&numberB=5" >Add</a>
<a href="calculator/subtract.htm?numberA=10&numberB=5" >Subtract</a>
<a href="calculator/multiply.htm?numberA=10&numberB=5" >Multiply</a>
<a href="calculator/divide.htm?numberA=10&numberB=5" >Divide</a> 

Create jsp folder in WEB-INF and create calculator.jsp file in jsp folder that displays the result based on link clicked in index.jsp

calculator.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>Success Page</title>
</head>
<body>
Result : ${result}
</body>
</html>

3. Spring Bean Configuration

Create Spring Bean Configuration in WEB-INF folder and name it dispatcher-servlet.xml and add the following code.

<context:component-scan base-package="com.kruders.controller" />
 
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/jsp/</value>
    </property>
     
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

Here we use BeanNameUrlHandlerMapping to map the request url.

4. Run

Now when you run the project, following screen will be displayed as in Figure 10.1

Figure 10.1 Figure 10.1

Now when you click on Add link, following screen will be displayed as in Figure 10.2

Figure 10.2 Figure 10.2

The folder structure of the example is shown below in Figure 10.3

Figure 10.3 Figure 10.3

You can download the source code of this example here.



Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images