Java技术专题

Springmvc入门

2018-10-07  本文已影响8人  爱撒谎的男孩

Springmvc入门

作用

Springmvc的核心组件

  1. DispatcherServlet :前端控制器,请求入口
  2. HandlerMapping : 控制器,其中存放着处理请求的组件,请求派发
  3. Controller :控制器,处理请求
  4. ModelAndView :模型,封装业务处理结果和视图
  5. ViewResolver : 视图解析器,显示视图

springmvc处理流程

  1. 用户发出请求,请求交给前端控制器(DispatcherServlet)处理
  2. DispatcherServlet通过HandlerMapping找到Controller中相对应的组件处理请求
  3. 执行Controller组件约定方法处理请求,在约定方法调用模型组件(Service,Dao)完成业务逻辑,约定方法返回一个ModelAndView对象,封装了处理结果和视图名称信息
  4. 控制器接收了ModelAndView之后,调用视图解析器(ViewResolver)组件,定位到指定的View(JSP)并且传递处理结果,生成响应界面结果
springmvc处理流程

创建一个Springmvc项目

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
</dependencies>
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

    <!-- 开启组件扫描 -->
    <context:component-scan base-package="cn.tedu.springmvc"></context:component-scan>

    <!-- 配置视图解析器:
        prefix:表示前缀
        suffix:表示后缀
        如果controller中返回的是success这个字符串,那么对应的视图是/WEB-INF/JSP/success.jsp这个视图
    -->

    <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/JSP/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>
    <!-- 配置前端控制器DispatcherServlet -->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <!-- 配置springMVC配置文件的路径,这里如果想要使用默认的可以不用配置 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!-- classpath表示根路径,这里找的项目根路径下的applicationContext.xml -->
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>

        <!-- 当tomcat启动的时候就加载,设置启动的优先级 -->
        <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <!-- 配置分发规则,这个是用来控制所有的请求,只要是请求后缀为.do的都会拦截分发-->
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/hello.do")
    public String testHello(){
        System.out.println("Hello World");
        return "success";  //返回视图,这里通过视图解析器之后对应的是 /WEB-INF/JSP/success.jsp
    }
}
上一篇下一篇

猜你喜欢

热点阅读