SpringMVC快速入门

参考资料

博客园:平凡希

官方文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc

入门程序

入门准备

依赖-配置文件。

依赖

<dependencies>

        <!--jsp标签库-->

        <dependency>

            <groupId>jstl</groupId>

            <artifactId>jstl</artifactId>

            <version>1.2</version>

        </dependency>

        <!--日志管理-->

        <dependency>

            <groupId>commons-logging</groupId>

            <artifactId>commons-logging</artifactId>

            <version>1.2</version>

        </dependency>

 

        <!--aop联盟-->

        <dependency>

            <groupId>aopalliance</groupId>

            <artifactId>aopalliance</artifactId>

            <version>1.0</version>

        </dependency>

        <!--核心-->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-core</artifactId>

            <version>4.2.8.RELEASE</version>

        </dependency>

        <!--aspects-->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-aspects</artifactId>

            <version>4.2.8.RELEASE</version>

        </dependency>

        <!--aop-->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-aop</artifactId>

            <version>4.2.8.RELEASE</version>

        </dependency>

        <!--tx事务-->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-tx</artifactId>

            <version>4.2.8.RELEASE</version>

        </dependency>

        <!--容器管理-->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-beans</artifactId>

            <version>4.2.8.RELEASE</version>

        </dependency>

        <!--context  好像跟扫描包有关系-->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-context</artifactId>

            <version>4.2.8.RELEASE</version>

        </dependency>

        <!--表达式语言-->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-expression</artifactId>

            <version>4.2.8.RELEASE</version>

        </dependency>

        <!--spring web下的工具包-->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-web</artifactId>

            <version>4.2.8.RELEASE</version>

        </dependency>

 

        <!--spring mvc-->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-webmvc</artifactId>

            <version>4.2.8.RELEASE</version>

        </dependency>

 

        <!--测试包-->

        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>4.12</version>

        </dependency>

        <!--spring整合测试-->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-test</artifactId>

            <version>4.2.8.RELEASE</version>

        </dependency>

        <!--ServletAPI编译时期-->

        <dependency>

            <groupId>javax.servlet</groupId>

            <artifactId>servlet-api</artifactId>

            <version>2.5</version>

            <scope>provided</scope>

        </dependency>

</dependencies>

 

 

创建Handler

Handler处理器,相当于Action或者Controller

package com.lifeibai.web.controller;

 

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

/**

 * Author:Alan

 *

 * @Date: 2018-04-29  17:15:56

 */

public class HelloWorldController extends AbstractController {

    protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest , HttpServletResponse httpServletResponse){

        return new ModelAndView("index.jsp");

    }

}

  

配置文件

web.xml,配置DispatcherServlet

首先配置前端控制器(DispatcherServlet),这个是SpringMVC所有请求的入口

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"

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

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

                http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

           version="2.5">

    <servlet>

        <servlet-name>action</servlet-name>

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

        <init-param>

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

            <param-value>classpath:spring-mvc.xml</param-value>//这里的配置指向下面的文件

        </init-param>

        <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet-mapping>

        <servlet-name>action</servlet-name>

        <url-pattern>*.action</url-pattern>

    </servlet-mapping>

</web-app>

  

配置spring-mvc.xml

这里需要配置一下,Handler和其对应的id。 这里的id就是Handler(Action)的访问路径。

Spring-mvc.xml。 这里的文件名称随便起,但是需要在web中配置引用

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<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-3.2.xsd">

    <!--配置controller-->

    <bean id="/hi.action" class="com.lifeibai.web.controller.HelloWorldController"></bean>

</beans>

  

注意:id的名称一定要带/,否则访问不了。 因为这里使用的是默认的处理器映射器

配置视图index.jsp

在webapp目录中创建一个index。Jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

  <head>

    <title>$Title$</title>

  </head>

  <body>

  <h1>Hello,Spring-MVC!!!</h1>

  </body>

</html>

  

访问

启动tomcat

打开浏览器访问:http://localhost:8080/spring_mvc_demo001/hi.action

 

 

 

posted @ 2018-05-14 17:16  里非白  阅读(214)  评论(0编辑  收藏  举报