package com.springapp.config; /** * Created with IntelliJ IDEA. * User: diversant * Date: 18.06.13 * Time: 13:05 * To change this template use File | Settings | File Templates. */ public class MvcConfig { }
package com.springapp.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration @ComponentScan(basePackages="com.springapp") @EnableWebMvc public class MvcConfig extends WebMvcConfigurerAdapter { @Bean public ViewResolver getViewResolver(){ InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/pages/"); resolver.setSuffix(".jsp"); return resolver; } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); } }
@Configuration
- we say that this class is configurational for Spring, that is, it includes the configuration of bins.
@ComponentScan
- specify the package to which the necessary directives for interacting with bins will be searched.
@EnableWebMvc
- we indicate that this class is configurational for Spring MVC.
@Bean
- we say that the annotated method is a bin.
@Override
known to everyone who has worked at least a little with Java.
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Spring 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>/</url-pattern> </servlet-mapping> </web-app>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Spring MVC</display-name> <context-param> <param-name>contextClass</param-name><param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value></context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>SpringDispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextClass</param-name><param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value></init-param> <init-param> <param-name>contextConfigLocation</param-name><param-value>com.springapp.config</param-value></init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringDispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <session-config> <session-timeout>30</session-timeout> </session-config> </web-app>
package com.springapp.controllers; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @Controller @RequestMapping("/") public class HelloController { @RequestMapping(method = RequestMethod.GET) public String printWelcome(ModelMap model) { model.addAttribute("message", "Hello world!"); return "hello"; } }
@Controller
- actually we say that this is a controller. Nothing to add
@RequestMapping("/")
this annotation, we indicate the scope for this controller (if this annotation is applicable to a class) or a specific broadcast address (if it is a method). You can also specify at which particular request one or another address will be transmitted (the method parameter). In this case, we say that this controller will see all the addresses (what is in the browser) and try to translate them to the desired page. For more clarity, I will give an example.
<html> <body> <h1>${message}</h1> </body> </html>
<html> <body> <h1>${message}</h1> <form method="post"> <input name="value1" type="text"/> + <input name="value2" type="text"/> <input type="submit"/> </form> <h3>Result: ${result}</h3> </body> </html>
package com.springapp.controllers; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @Controller @RequestMapping("/") public class HelloController { @RequestMapping(method = RequestMethod.GET) public String printWelcome(ModelMap model) { model.addAttribute("message", "Hello world!"); return "hello"; } @RequestMapping(method = RequestMethod.POST) public String calcSum(Model m, @RequestParam(value = "value1") Double value1, @RequestParam(value = "value2") Double value2 ){ m.addAttribute("result", (value1+value2)); return "hello"; } }
@RequestMapping
annotation @RequestMapping
specify the POST processing method, and also use the @RequestParam
annotation @RequestParam
obtain information from the form fields (value1, value2).
Source: https://habr.com/ru/post/183854/