Getting SpringMVC Case Analysis and Implementation

Getting Case Study:

    1. execution flow
        1. When you start the tomcat server, due to the configuration of the load_on_startup label, it will create DispatcherServlet object will load springmvc.xml profile
        2. Turn on annotations scanning, HelloController object is created
        3. index. jsp transmission request, the request will arrive first DispatcherServlet core controller specific configuration method performed to find the annotation @RequestMapping
        the return value of the method, then a view of the parser configuration, lookup jsp file name specified under the specified directory to
        5.Tomcat server to render the page, responds
    2. flowchart
Here Insert Picture Description
    3.RequestMapping annotation
        action 1.RequestMapping annotation is to establish a correspondence between the request URL and processing method
        2.RequestMapping annotations may act on methods and classes
            acting class on: first level access the directory
            acting on the method: the second level access directory
            details: / represents the root directory application start
                    $ {pageContext.request.contextPath} also can be omitted, but can not write / path
        3. RequestMapping property:
            URL specified in the request path: path
            value: Like path
            Method: Specifies the request of the method, an enumeration type
            params: restriction request parameter specified conditions
            headers: request header in the transmission request must be included

SpringMVC entry procedures Case

    1. Create WEB plant, the introduction of the jar package development, need to import the following coordinates

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
  <spring.version>5.0.2.RELEASE</spring.version>
</properties>

<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</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>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
  </dependency>
  <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.0</version>
    <scope>provided</scope>
  </dependency>
</dependencies>

    2. Controller (DispatcherServlet) core configuration
        in the web.xml configuration file

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!--springMVC的核心控制器-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--配置Servlet的初始化参数哦,读取springmvc的配置文件,创建spring容器-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springMVC.xml</param-value>
    </init-param>
    <!--配置servlet启动时加载对象-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

    3. Write springmvc.xml profile

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!--开启注解扫描-->
    <context:component-scan base-package="cn.ycl"></context:component-scan>
    <!--视图解析器对象-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--开启springMVC框架对注解的支持-->
    <mvc:annotation-driven/>
</beans>

    4. Preparation HelloController index.jsp and controller classes
        1.index.jsp

<body>
    <h3>入门程序</h3>
    <a href="hello">入门程序</a>
</body>

        2.HelloController

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

//控制器类
@Controller
public class HelloController {
    @RequestMapping(path = "/hello")
    public String sayHello(){
        System.out.println("Hello,springMVC!!");
        return "success";
    }
}

    5. Create a directory under WEB-INF folder pages, written success.jsp success page

<h3>返回成功</h3>

    6. Start the server for testing

Published 21 original articles · won praise 2 · Views 261

Guess you like

Origin blog.csdn.net/weixin_45636641/article/details/104174370