Springmvc annotation configuration

1. In the web. XML file, register the servlet and servlet-mapping configuration

<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup< / a > 1load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
Copy the code
By default, the classpath in maven refers to the root directory load-onStartup: in the resources directory. The smaller the sooner DispatcherServlet: front-end controllerCopy the code

Add the Spring MVC configuration file

<? 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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

  <context:component-scan base-package="Full path of package"/ > <! Let Spring MVC not handle static resources --> < MVC:default-servlet-handler/> <! Support MVC annotation driver to enable@RequestMappingEffective annotations and complete the mapping relationships through annotations instead Must be registered DefaultAnnotationHandlerMapping and a AnnotationMethodHandlerAdapter instance with the context The annotation-driven configuration automatically automates the injection of both instances --> < MVC :annotation-driven/> <! View interpreter --> <beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <! -- Set the view access address prefix --> <property name="prefix" value="/WEB-INF/jsp/"/ > <! -- Set the suffix --> <property name="suffix" value=".jsp"/>
  </bean>
</beans>
Copy the code
Specify the configuration of automatic scanning packages to validate annotations under the specified packages, which are centrally managed by the SpringIOC containerCopy the code

3. Write Controller under scan package

Take HelloController as an example

package controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { @RequestMapping("/hello") public String hello(Model model) { // Pass data to the view through the Model object model.adDattribute (" MSG ", "springMVC "); return "hello"; }}Copy the code

4. Write the view file in the preset directory

The default directory corresponds to the prefix directory in the view interpreter configuration (note that the last/must exist in the configuration)

<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<head>
    <title>Hello</title>
</head>
<body>
${msg}
</body>
</html>
Copy the code