📜 ⬆️ ⬇️

Introduction to Spring MVC with annotations

Yesterday I started to deal with Spring MVC 3.0. I looked for articles on Habré, I found a couple of pieces. True, they were without annotations.
The purpose of this article is to write Hello World using the ability to write configs right in the code, thanks to annotations. Well, let's get started.

Create a web project
At the core of any web project is a web.xml file, here is its code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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
java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

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

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>/WEB-INF/pages/index.jsp</welcome-file>
</welcome-file-list>

</web-app>

Here we declare the ubiquitous Sprat servlet DispatcherServlet which will process our requests, we also indicate the file where the dispatcher-servlet.xml settings lie.
Let's go to the very dispatcher-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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:component-scan base-package="az.mecid.controllers"/> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> </beans> 
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:component-scan base-package="az.mecid.controllers"/> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> </beans>

here we specify which package to scan for the presence of spring bean'ov, then go two bins that are needed to work with annotations, using the viewResolver bean we specify where our View is located, and thanks to the suffix property we can use them without extensions.

Writing configs is not the most pleasant thing, but now we’re done.
Create a page with a form in which there is an input field and submit.
  <% @ page contentType = "text / html; charset = UTF-8" language = "java"%>
 <html>
   <head> <title> Simple jsp page </ title> </ head>
   <body>

   <form action = "test.form" method = "get">
       <input type = "text" name = "name">
       <input type = "submit">
   </ form>

   </ body>
 </ html>

Now the most interesting thing is to write a controller processing this form:
package az.mecid.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloController {

@RequestMapping(value = "/test.form",method = RequestMethod.GET)
public String test(@RequestParam("name") String param,Model model){
model.addAttribute("name",param);
return "hello";
}
}

Mark our class with the Controller annotation, thanks to this we don’t need to write in XML, its name, where to map and everything else. Then we create the test method (handler of our form) and use the @RequestMapping annotation to specify where to map it and what request it should catch the post or get.Now we talk about the parameters of the test method, the abstract @RequestParam ("name") String name says that the handler receives the name field of the form that it processes, well, if it were to be done in the servlet
String param = request.getParametr("name");

The second parameter of the test method, model, is a collection that contains information in the form of a key-value.
Code model.addAttribute("name",param); equivalent to the fact that if the servlet write request.setAttribute("name",param);
The test method returns a string, this is a string, or rather the name of the View to which we want to forward.
This is equivalent to request.getRequestDispatcher("hello.jsp").forward(req,res); Due to the fact that in the configs we wrote that our all View is stored in WEB-INF / pages and have the extension .jsp it is not necessary to specify the full path with extension.
Now we need to create the pages folder in WEB-INF, and in the same place create our View hello.jsp
 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Simple jsp page</title></head> <body> Hello <%= request.getAttribute("name")%> </body> </html> 
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Simple jsp page</title></head> <body> Hello <%= request.getAttribute("name")%> </body> </html>

Well, that's all we are launching, we see the form, submit it and see the solemn Hello!
Sources can be downloaded here and here with libs

')

Source: https://habr.com/ru/post/86433/


All Articles