The basic configuration of SpringMVC good Java programmer to learn the route

  Java learning route of SpringMVC basic configuration, the front of our understanding of the MVC pattern, in this chapter we will learn SpringMVC basic framework for the use of master SpringMVC configuration is to use SpringMVC base frame.

SpringMVC configuration process

1 , introduced maven dependent

2 , add the spring configuration

3 , configuration web.xml file

4 , using configuration controller annotations

Import Maven dependent

Here we need to spring-webmvc package

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-webmvc</artifactId>

      <version>4.3.14.RELEASE</version>

    </dependency>

Spring configuration file

In the Add spring-mvc.xml configuration file resources directory

<?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"

       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">

<! - scanning components in the package ->

   <context:component-scan base-package="com.qianfeng.springmvc">

   </context:component-scan>

   <! -  Configure view processor, by url return to a specific page address , such as: the address bar type http: // localhost: 8080 / mvc / hello will have access to the real page address: http: // localhost: 8080 / mvc /WEB-INF/jsp/hello.jsp ->

   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

      <-! View prefix ->

      <property name="prefix" value="/WEB-INF/jsp/"></property>

      <-! View suffix ->

      <property name="suffix" value=".jsp"></property>

   </bean>

   <! - Configure a static resource processor ->

   <mvc:default-servlet-handler/>

   <!--配置注解驱动-->

   <mvc:annotation-driven/>

</beans>

 

配置web.xml文件

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xmlns="http://java.sun.com/xml/ns/javaee"

         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

         id="WebApp_ID" version="3.0">

  <display-name>Archetype Created Web Application</display-name>

  <!--配置前端控制器-->

  <servlet>

    <servlet-name>dispatcherServlet</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <!--配置spring-mvc的配置文件位置-->

    <init-param>

      <param-name>contextConfigLocation</param-name>

      <param-value>classpath:spring-mvc.xml</param-value>

    </init-param>

  </servlet>

  <!--配置前端控制器管理所有web资源-->

  <servlet-mapping>

    <servlet-name>dispatcherServlet</servlet-name>

    <url-pattern>/</url-pattern>

  </servlet-mapping>

</web-app>

 

添加控制器

/**

 * 用户控制器

 */

@Controller

public class UserController {

    /**

     * 配置映射,接受请求http://localhost:8080/mvc/hello

     * 返回hello字符串,由视图处理器,拼接成http://localhost:8080/mvc/WEB-INF/jsp/hello.jsp

     */

    @RequestMapping(value = "hello",method = RequestMethod.GET)

    public String hello(){

        return "hello";

    }

}

启动项目,输入URL进行测试:

SpringMVC的执行流程

1)用户发送请求给前端控制器

2)前端控制器将请求中的url和处理器映射中的url进行比较

3)返回url对应的处理器

4)前端控制器把处理器发送给处理器适配器

5)适配器会执行处理器中的逻辑代码

6)适配器执行完成后得到逻辑视图

7)适配器返回逻辑视图给前端控制器

8)前端控制器把逻辑视图发给视图解析器

9)视图解析器解析后返回真正的视图

10)将视图进行渲染,返回给用户


总结

通过SpringMVC的配置,我们能够运行一个基本的SpringMVC程序,对于Web程序来说还需要知道如何获得用户传递的参数,如何返回数据到页面上,这些我们将在后面章节继续学习。


Guess you like

Origin blog.51cto.com/14479068/2428090