第一个SpringMVC程序

准备工作:

1、第一步先创建一个maven的工程,选择webapp项目

2、取一个artificts名字spring-mvc一路下一步,反正是要配的,具体artifacts怎么用的我还没弄明白,后面再出个随笔

3、设置目录,点击完成创建

4、将默认的依赖删掉,自己添加springMVC的jar依赖

5、添加相关依赖

 

<dependencies>
        <!--spring-ioc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>

        <!--spring-aop-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>

        <!--Spring整合web-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <!--Spring整合jdbc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>

        <!--Spring整合mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>

        <!--SpringMVC-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
      <!--Java EE--> 
       <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
      </dependency>
       <dependency>
           <groupId>javax.servlet.jsp</groupId>
           <artifactId>jsp-api</artifactId>
           <version>2.2</version>
       </dependency>
      <dependency>
          <groupId>jstl</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
      </dependency>  
</dependencies>

 

 这里需要注意有个小技巧,有的时候添加依赖的时候maven显示找不到,带红色波浪线,可以先去百度搜有没有这个版本号,修改版本号重新导入一下

比如查到mybatis-spring的版本号有1.3.1,再在version一栏添加1.3.1

springMVC执行过程:

一、配置核心控制器

在web.xml里面配置

<?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_3_0.xsd"
         version="3.0">

    <!--1、配置DispatcherServlet-->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--在web应用启动的时候执行此servlet-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

二、创建并配置核心配置文件

两种定义方式:

  • 使用默认位置,在WEB-INF下面创建springMVC-servlet.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--2、配置HandleMapping-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!--3、配置HandlerAdapter-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <!--4、配置Handler-->
    <bean name="/hello" class="controller.HelloController"/>
    <!--5、配置ViewResolver-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
        <!--6、配置View,使用jsp视图技术渲染页面-->
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    </bean>
三、用实现接口的方式编写一个controller类
package controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

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

/**
 * @Description:处理请求,返回mav
 * @Authoor: hujian
 * @Date: 2018/9/19 13:02
 * @Version 1.0.0
 */
public class HelloController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String name = req.getParameter("name");
        ModelAndView mav = new ModelAndView();
        mav.addObject("msg","Hello"+name);
        mav.setViewName("hello");
        return mav;
    }
}

四、再新建一个hello.jsp


<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/9/19
  Time: 13:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
   msg:${msg}
</body>
</html>
五、启动tomcat

加上/hello

带参数访问controller

到此处第一个MVC程序的过程就完了,第一次写博客,有什么问题多多指教,最后附上一张经典的过程图:

 

 

 

 

 

 

 

 

 

 

posted @ 2018-09-19 14:44  雪见  阅读(217)  评论(0编辑  收藏  举报