springmvc入门系列(一)_快速入门案例

0. SpringMVC的工作流程图

springmvc入门系列(一)_快速入门案例_第1张图片

其工作流程为:
1、用户发送请求至前端控制器DispatcherServlet
2、DispatcherServlet收到请求调用HandlerMapping处理器映射器。
3、处理器映射器根据请求url找到具体的处理器,生成处理器对象及处理器拦截器(二者组成HandlerExecutionChain),并将其一并返回给DispatcherServlet。
4、DispatcherServlet通过HandlerAdapter处理器适配器调用处理器
5、执行处理器(Controller,也叫后端控制器)。
6、Controller执行完成返回ModelAndView
7、HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet
8、DispatcherServlet将ModelAndView传给ViewReslover视图解析器
9、ViewReslover解析后返回具体View
10、DispatcherServlet对View进行渲染视图(即将模型数据填充至视图中)。
11、DispatcherServlet对用户进行响应

1. 使用IDE创建一个Maven web工程(如何创建略去。。)

2. 添加maven依赖

<properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.8maven.compiler.source>
    <maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
    
    <dependency>
      <groupId>commons-iogroupId>
      <artifactId>commons-ioartifactId>
      <version>2.4version>
    dependency>

    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>1.3.1version>
    dependency>

    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-ormartifactId>
      <version>4.3.14.RELEASEversion>
    dependency>

    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-expressionartifactId>
      <version>4.3.14.RELEASEversion>
    dependency>

    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>4.3.14.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>4.3.14.RELEASEversion>
    dependency>

    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>4.3.14.RELEASEversion>
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aopartifactId>
      <version>4.3.14.RELEASEversion>
    dependency>

    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-coreartifactId>
      <version>4.3.14.RELEASEversion>
    dependency>

    
    <dependency>
      <groupId>commons-logginggroupId>
      <artifactId>commons-loggingartifactId>
      <version>1.1.1version>
    dependency>

    
    <dependency>
      <groupId>log4jgroupId>
      <artifactId>log4jartifactId>
      <version>1.2.17version>
    dependency>
    
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>2.5version>
            <scope>providedscope>
        dependency>

        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
            <scope>providedscope>
        dependency>
  dependencies>

3. 在web.xml文件中配置SpringMVC的DispatcherServlet

<servlet>
    
    <servlet-name>springmvcservlet-name>
    
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

    
    <init-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>/WEB-INF/springmvc-config.xmlparam-value>
    init-param>
    
    <load-on-startup>1load-on-startup>
servlet>

<servlet-mapping>
    
    <servlet-name>springmvcservlet-name>
    
    <url-pattern>/url-pattern>
servlet-mapping>

4. 创建springmvc-config.xml,配置Controller


<beans xmlns="http://www.springframework.org/schema/beans"
        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-4.2.xsd">

    
    <bean name="/hello" class="com.ghq.controller.HelloController"/>

    
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
beans>

5. Controller的实现

package com.ghq.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * HelloController是一个控制器,可以处理一个单一的请求动作
 * @author snow
 */
public class HelloController implements Controller{
    private static final Log logger = LogFactory.getLog(HelloController.class);
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

        logger.info("Handler被调用");
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","Helloworld");
        mv.setViewName("/index.jsp");
        return mv;
    }
}

6. index.jsp

<html>
<body>
<h2>Hello World!h2>
    ${requestScope.msg}
body>
html>

7. 部署应用,并访问

http://localhost:9090/hello

页面显示结果如下:

springmvc入门系列(一)_快速入门案例_第2张图片

8. 基于注解开发

修改Controller类,代码如下:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


/**
 * HelloController是一个控制器,可以处理一个单一的请求动作
 * @author snow
 */
@Controller
public class HelloController {
    private static final Log logger = LogFactory.getLog(HelloController.class);

    /**
     * RequestMapping注解用来映射请求的URL和请求方法等,这里映射/hello
     * @return
     */
    @RequestMapping(value = "/hello")
    public ModelAndView hello() {

        logger.info("Handler被调用");
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","Hello canglaoshi");
        mv.setViewName("/index.jsp");
        return mv;
    }
}

修改springmvc-config.xml文件,代码如下:

<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    
    <context:component-scan base-package="com.ghq"/>

    
    <bean class="org.springframework.web.servlet.mvc.method.annotation.
                 RequestMappingHandlerMapping"/>

    
    <bean class="org.springframework.web.servlet.mvc.method.annotation.
                 RequestMappingHandlerAdapter"/>

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
beans>

重新部署项目,再次请求,访问结果略去。。

你可能感兴趣的:(springmvc)