SpringMVC- Implemented using annotations

  1. Create a module that imports the dependencies required by SpringMVC.
  2. Add Web support, and manually import dependencies in IDEA, create lib package imports, as described in the previous Blog post.
  3. configurationweb.xmlIt is fixed and will not change.

      
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <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-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
Copy the code
  1. configurationspringmvc-servlet.xmlAnd this one is pretty much fixed

      
<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/mvc"
       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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <! -- Scan the specified package to validate the annotations under the specified package -->
    <context:component-scan base-package="controller"/>
    <! Let springMVC not handle static resources -->
    <mvc:default-servlet-handler/>
    <! -- Enable MVC annotation support -->
    <mvc:annotation-driven/>

    <! -- View resolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <! -- prefix -- -- >
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <! - the suffix - >
        <property name="suffix" value=".jsp"/>
    </bean>


</beans>

Copy the code
  1. Write business control classesannotationHello.java

The business control class is scanned and mapped using @Controller and @RequestMapping.

package controller;

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

@Controller
public class annotationHello {
    @RequestMapping("/hello") // Map the address
    public String hello(Model model){
        //model: the returned model
        model.addAttribute("message"."helloSpringMVC");
        return "hello";// WEB-INF/jsp/hello.jsp}}Copy the code

  1. Writing views hello.jsp

<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${message}
</body>
</html>

Copy the code
  1. Configure Tomcat and test it

Conclusion:

  1. SpringMVC- The difference between using the annotation implementation and the original SpringMVC implementation is the configuration: the configuration of the annotations is fixed, and the first configuration version needs to be changed.
  2. When you need to add some Controller business, the annotation implementation just needs to add a method to the business control class, just need to modify the business control class.
  3. When you need to add some Controller business, the configuration implementation will need to modify the SpringMVC-servlet.xml file and the business control class.