Spring Step By StepJava学习笔记SpringFramework

10 Spring MVC 的一次访问(基础入门篇)

2016-12-09  本文已影响275人  赖赖oO

转载请注明来源 赖赖的博客

导语

了解一下起因经过,你才能明白世事的真正含义。

Spring的基础都了解了一些,这一篇就来看看Spring应用MVC会是怎么样的吧,轻松简单,本章讲述的是一次访问开始到返回Spring MVC是如何应对的(基础版)

实例

项目工程目录结构和代码获取地址

获取地址(版本Log将会注明每一个版本对应的课程)

https://github.com/laiyijie/SpringLearning

目录结构

目录结构

如图所示,虽然目录结构看起来复杂了一些(就是文件夹深度深了一点儿而已),其实最源文件只有四个。所以不要紧张,这非常简单

运行工程

运行方式
运行结果
运行结果

我用的是 chrome浏览器,输出了hello

OK,这次从浏览器输入这个链接到返回这个hello来讲解!

项目详解

我们不妨直接找找这个输出的 hello来自哪一段代码

UserController.java

package me.laiyijie.demo.controller;

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

@Controller
public class UserController {
    
    @RequestMapping("/hi")
    @ResponseBody
    public String hello(){
        return "hello";
    }
    
}

是不是很短,简洁明了!

我们一眼看到的就是 hello!没错,就是这个函数里面处理了刚才的请求,并且返回了一个hello的字符串!
这里有三个注解含义分别如下:

进入到/demo以后是由@RequestMapping控制这个访问,那么一个访问是怎么被引到如到demo这个工程中的呢?Spring Context又是怎么生效的呢?(我们并没有用new ClassPathXmlApplicationContext("root-context.xml");来初始化一个Spring Context,为何注解就用了起来?)

浏览器中输入 http://localhost:8080/demo/hi 并访问

那让我们详细看一下web.xml是怎样的!

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_1.xsd"
    version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee">

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
</web-app>

如果你不熟悉web.xml的配置,也请不要紧张,因为这部分的配置已经非常成熟,一次配置完毕会很少改动,即使你不太了解也不会影响你的使用,这里摆出只是为了让你了解一个访问是如何进入Spring MVC进行管理的。

我们看关键一点:

<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>  

这里载入了一个配置文件,这个配置文件的名称是 servlet-context.xml 这么熟悉的名称,正是SpringContext的配置文件!

是的,正是通过web.xml我们初始化了一个SpringContext,也就是tomcat(或者Pivotal Server)帮助我们初始化了这个SpringContext代替掉我们再main函数中使用的:

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("root-context.xml"); 

Spring 容器初始化完毕,访问完全转入Spring MVC 中由我们进行控制!

为了让注解生效必然要配置一下 servlet-context.xml

servlet-context.xml

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


    <context:component-scan base-package="me.laiyijie.demo" />

    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"
        p:messageConverters-ref="stringHttpMessageConverter" />

    <bean id="stringHttpMessageConverter"
        class="org.springframework.http.converter.StringHttpMessageConverter" />

</beans>

重点只有四句话:

<context:component-scan base-package="me.laiyijie.demo" />

<mvc:annotation-driven />

pom.xml

<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>me.laiyijie</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <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.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

依赖新增一个:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.2.RELEASE</version>
</dependency> 

小结:

输入 http://localhost:8080/demo/hi 访问全程:

上一篇 下一篇

猜你喜欢

热点阅读