hooyantsing's Blog

第114次课程_Spring_MVC原理

字数统计: 2.1k阅读时长: 11 min
2020/02/28

源辰76班

第114次课程

2020.02.28

内容

Spring_MVC原理

项目路径:

1.初始化一个Spring_MVC的maven工程

配置流程pom.xml(maven工程) -> web.xml(web服务器) -> beans.xml(spring框架)    

pom.xml    /

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<project  xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.yc</groupId>
    <artifactId>C76-S3-Ply-springMVC</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
       <maven.compiler.source>1.8</maven.compiler.source>
       <maven.compiler.target>1.8</maven.compiler.target>
       <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
    </properties>
    <dependencies>
       <!--  https://mvnrepository.com/artifact/org.springframework/spring-context -->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>4.3.17.RELEASE</version>
       </dependency>
       <!--  https://mvnrepository.com/artifact/org.springframework/spring-web -->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-web</artifactId>
           <version>4.3.17.RELEASE</version>
       </dependency>
       <!--  https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-webmvc</artifactId>
           <version>4.3.17.RELEASE</version>
       </dependency>
       <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>jstl</artifactId>
           <version>1.2</version>
       </dependency>
       <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>javax.servlet-api</artifactId>
           <version>3.1.0</version>
       </dependency>
    </dependencies>
    
</project>

web.xml    src/main/webapp/WEB-INF

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
<web-app  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    <display-name>C76-S3-Ply-springmvc</display-name>
    <welcome-file-list>
       <welcome-file>hello.s</welcome-file>
    </welcome-file-list>
    <servlet>
       <servlet-name>dispatcher</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <init-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:beans.xml</param-value>
       </init-param>
       <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
       <servlet-name>dispatcher</servlet-name>
       <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

bean.xml    src/main/resources

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?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/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    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">
    
    <!-- 加入 mvc 命名空间 -->
    <!-- SpringMVC 控制器注解驱动 -->
     <mvc:annotation-driven></mvc:annotation-driven>
    <!-- 包扫描 -->
    <context:component-scan  base-package="com.yc.springmvc"></context:component-scan>
    
    <!-- 定义视图解析器 -->
    <bean id="viewResolver"  class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"  value="org.springframework.web.servlet.view.JstlView"/>
        <!-- 视图解析器视图前缀 -->
        <!--  webapp     /WEB-INF/jsp/hello.jsp     -->
        <property name="prefix"  value="/WEB-INF/jsp/"/>
        <!-- 视图解析器视图后缀 -->
        <property name="suffix"  value=".jsp"/>
    </bean>
    
</beans>

2.如何返回JSP页面

beans.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?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/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    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">
    
    <!-- 加入 mvc 命名空间 -->
    <!-- SpringMVC 控制器注解驱动 -->
     <mvc:annotation-driven></mvc:annotation-driven>
    <!-- 包扫描 -->
    <context:component-scan  base-package="com.yc.springmvc"></context:component-scan>
    
    <!-- 定义视图解析器 -->
    <bean id="viewResolver"  class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"  value="org.springframework.web.servlet.view.JstlView"/>
        <!-- 视图解析器视图前缀 -->
        <!--  webapp     /WEB-INF/jsp/hello.jsp     -->
        <property name="prefix"  value="/WEB-INF/jsp/"/>
        <!-- 视图解析器视图后缀 -->
        <property name="suffix"  value=".jsp"/>
    </bean>
    
</beans>

IndexAction.java

控制器。相当于Servlet,跳转至指定页面。

@RestController  注解的类内部方法返回的是字符串                     应用于返回”json/xml”数据

@Controller         注解的类内部方法返回的是页面名(jsp/html)    应用于跳转页面


@RequestMapping(“hello.s”)  注解用于将请求映射到方法上,支持多种映射。

通俗来讲:浏览器的Url内输入”…/hello.s”时调用”hello()”方法,返回”hello”并与beas.xml的定义字符串组装成”jsp/hello.jsp”,最后按照路径找到页面且提供给用户。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.yc.springmvc.web;
import  org.springframework.stereotype.Controller;
import  org.springframework.web.bind.annotation.GetMapping;
import  org.springframework.web.bind.annotation.PostMapping;
import  org.springframework.web.bind.annotation.RequestMapping;
import  org.springframework.web.bind.annotation.RequestMethod;
import  org.springframework.web.bind.annotation.RestController;
/*
* @RestController 注解的类内部方法返回的是字符串
* @Controller         注解的类内部方法返回的是页面名(jsp/html)
*/
//@RestController
@Controller
public class IndexAction {
    
    @RequestMapping("hello.s")
    public String hello() {
       return "hello";
    }
    
}

3.RequestMapping详解

IndexAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.yc.springmvc.web;
import  org.springframework.stereotype.Controller;
import  org.springframework.web.bind.annotation.GetMapping;
import  org.springframework.web.bind.annotation.PostMapping;
import  org.springframework.web.bind.annotation.RequestMapping;
import  org.springframework.web.bind.annotation.RequestMethod;
import  org.springframework.web.bind.annotation.RestController;
/*
* @RestController 注解的类内部方法返回的是字符串
* @Controller         注解的类内部方法返回的是页面名(jsp/html)
*/
//@RestController
@Controller
public class IndexAction {
    
    //@RequestMapping("默认参数value==path")
    
    @RequestMapping("hello.s")
    public String hello() {
       return "hello";
    }
    
    /*
     * 带目录的地址(虚拟地址)
     */
    @RequestMapping("/page/hello1")
    public String hello1() {
       return "hello";
    }
    
    /*
     * 多个地址
     */
    @RequestMapping({"abc","efg"})
    public String hello2() {
       return "hello";
    }
    
    /*
     * method 限定HTTP方法类型
     */
    @RequestMapping(path="hello3",method=RequestMethod.GET)
    public String hello3() {
       return "hello";
    }
    
    @GetMapping("hello4")
    public String hello4() {
       return "hello";
    }
    
    //相当于@RequestMapping(path="hello5",method=RequestMethod.GET)
    @PostMapping("hello5")
    public String hello5() {
       return "hello";
    }
}

HelloAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.yc.springmvc.web;
import  org.springframework.stereotype.Controller;
import  org.springframework.web.bind.annotation.GetMapping;
import  org.springframework.web.bind.annotation.RequestMapping;
/*
* @RequestMapping("")加在类上表示,在该类下所有的方法的映射地址,共同的虚拟目录
*/
@RequestMapping("user")
@Controller
public class HelloAction {    
    @GetMapping("hello")
    public String hello6() {
       return "hello";
    }
    @GetMapping("hello1")
    public String hello7() {
       return "hello";
    }
    @GetMapping("hello2")
    public String hello8() {
       return "hello";
    }
    
    /*
     * params限定请求必须带的参数,否则页面报错
     * path==value
     */
    @RequestMapping(value="show",params="user")
    public String hello9() {
       return "hello";
    }
    @RequestMapping(value="show1",params="user==root")
    public String hello10() {
       return "hello";
    }
}

4.jetty服务器

4.1.加入以下代码

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<project  xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.yc</groupId>
    <artifactId>C76-S3-Ply-springMVC</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
       <maven.compiler.source>1.8</maven.compiler.source>
       <maven.compiler.target>1.8</maven.compiler.target>
       <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
    </properties>
    
    <build>
       <plugins>
           <!-- run as ==> builder... ==>  jetty:run -->
           <plugin>
               <groupId>org.eclipse.jetty</groupId>
               <artifactId>jetty-maven-plugin</artifactId>
               <version>9.4.15.v20190215</version>
               <configuration>
                  <httpConnector>
                      <port>80</port>
                  </httpConnector>
                  <!-- 组件扫描时间间隔 -->
                  <scanIntervalSeconds>2</scanIntervalSeconds>
                  <!-- 手动启动服务器 -->
                  <reload>manual</reload>
                  <!-- 自定义上下文路径 -->
                  <contextPath>/</contextPath>
               </configuration>
           </plugin>
       </plugins>
    </build>
    
</project>

4.2.运行服务器

.png

-1.png

CATALOG