How to do it...

In order to create our first controllers, do the following steps:

  1. Locate pom.xml inside the ch03 folder and configure it to include the entire Spring 5.0 core, Spring Web MVC module, servlet and JSP APIs, JTSL, and standard taglib dependencies:
<properties> 
  <spring.version>5.0.0.BUILD-SNAPSHOT</spring.version> 
  <servlet.api.version>3.1.0</servlet.api.version> 
</properties> 
<dependencies> 
  <dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-context</artifactId> 
    <version>${spring.version}</version> 
  </dependency> 
  <dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-core</artifactId> 
    <version>${spring.version}</version> 
  </dependency> 
 
  <dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-beans</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>javax.servlet</groupId> 
    <artifactId>javax.servlet-api</artifactId> 
    <version>${servlet.api.version}</version> 
    <scope>provided</scope> 
  </dependency> 
 
  <dependency> 
    <groupId>javax.servlet.jsp</groupId> 
    <artifactId>javax.servlet.jsp-api</artifactId> 
    <version>2.3.1</version> 
    <scope>provided</scope> 
  </dependency> 
 
  <dependency> 
    <groupId>javax.servlet</groupId> 
    <artifactId>jstl</artifactId> 
    <version>1.2</version> 
  </dependency> 
  <dependency> 
    <groupId>taglibs</groupId> 
    <artifactId>standard</artifactId> 
    <version>1.1.2</version> 
  </dependency> 
 
  // refer to sources 
 
</dependencies>
  1. Since this project complies with the JavaConfig specification, create the usual SpringWebInitializer to enable MVC configuration and auto-detect all the annotations used by the internal components. Configure @ComponentScan to recognize the base-package org.packt.dissect.mvc.
  1. Configure also the root ApplicationContext and the SpringDispatcherConfig to auto-detect the @Controller annotations contained in the package org.packt.dissect.mvc.controller through the @ComponentScan.
  2. Now, create the first type of controller, which is the SimpleController. A simple controller is used to take control of every incoming request using only one URL. The centralized top-level URL setting filters the incoming request and easily identifies the type of HTTP method and headers essential in managing those requests. Let us name this controller SimpleController, having this implementation:
@Controller 
@RequestMapping("/simple.html") 
public class SimpleController { 
  
  @RequestMapping(method=RequestMethod.GET) 
  public String processGetReq(Model model){ 
    String transactionType = "Simple GET Transaction"; 
    model.addAttribute("transactionType", transactionType); 
    return "get"; 
  } 
  
  @RequestMapping(method=RequestMethod.POST) 
  public String processPostReq(Model model){ 
    String transactionType = "Simple POST Transaction"; 
    model.addAttribute("transactionType", transactionType); 
    return "post"; 
  } 
} 

On the other hand, this SimpleController illustrates how to call two handler methods sharing only one URL, but of different HTTP methods.

  1. From the preceding code, create the views get and post using ResourceBundleViewResolver. Create and open srcmain esourcesconfigviews.properties and add the following mappings:
post.(class)=org.springframework.web.servlet.view.JstlView 
post.url=/page/post_view.jsp 
 
get.(class)=org.springframework.web.servlet.view.JstlView 
get.url=/page/get_view.jsp
  1. Using ReloadableResourceBundleMessageSource, create and open srcmain esourcesconfigmessages_en_US.properties and add the following label mappings:
#Title Labels 
post_title=POST VIEW 
get_title=GET VIEW 
  1. After changing the properties file, create the physical view srcmainwebapppagepost_view.jsp for the POST transaction using the following template:
<%@ page language="java" contentType="text/html;   charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<%@ taglib prefix="spring"    uri="http://www.springframework.org/tags" %> 
<!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><spring:message code="post_title" /></title> 
</head> 
<body> 
    <h1>${ transactionType }</h1> 
</body> 
</html> 
  1. For the GET transaction, create srcmainwebapppageget_view.jsp, having the following script:
<%@ page language="java" contentType="text/html; 
charset=ISO-8859-1"  pageEncoding="ISO-8859-1"%> 
<%@ taglib prefix="spring" 
uri="http://www.springframework.org/tags" %> 
<!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><spring:message code="get_title" /></title> 
</head> 
<body> 
    <h1>${ transactionType }</h1> 
</body> 
</html> 
  1. To execute the handler methods, create a facade controller like this SimpleTestController, as shown here:
@Controller 
@RequestMapping("/simplecontroller.html") 
public class SimpleTestController { 
  
  @RequestMapping(method=RequestMethod.GET) 
  public String viewTransactions(){ 
    return "simple_list"; 
  } 
} 
  1. Implement a physical page for the simple_list view of the previous controller. Name it as srcmainwebapppageget_view.jsp, with the following script:
<%@ page language="java" contentType="text/html; 
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<%@ taglib prefix="spring" 
  uri="http://www.springframework.org/tags" %> 
<!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><spring:message code="simple_facade" /></title> 
</head> 
<body> 
<a href="${pageContext.request.contextPath}/simple.html"> 
   GET Transaction</a> 
<br/> 
<form action="${pageContext.request.contextPath}  
      /simple.html" method="post" > 
<input type="submit" value="POST Transaction" /> 
</form> 
</body> 
</html>
The ${pageContext.request.contextPath} is an EL expression that retrieves the context path of the deployed web application. This is recommended instead of hardcoding the context.
  1. Configure the messageSource and viewResolver to include the preceding added components.
  2. Save all files. clean, build, and deploy the project to your Tomcat 9 application server using our Maven deployment library. Open a browser and run https://localhost:8443/ch03/simplecontroller.html:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.179.220