0021SpringMVC环境搭建及入门程序编写

环境搭建:

1、创建项目

创建maven项目,勾选上Create from archetype,然后选中webapp再点击下一步,如下图:

 

解决项目创建过慢问题:

在创建maven项目过程中加入一组键值对:

archetypeCatalog   :  internal

 

2、增加目录结构并标记作用

最终创建出来的项目结构如下图:

 

我们发现创建出来的maven 项目的目录结构是不全的,我们需要在main下边创建文件夹:java、resources,如下图:

 

但是当我们在java上右键想要创建class的时候,发现没有class选项可以创建,我们需要右键java—>Mark Directory as SourceRoot,如下图:

同理右键resources Mark Directory as ResourcesRoot

 

3、pom.xml中引入需要的版本依赖

首先,properties增加spring.version,进行版本绑定

<properties>
  <spring.version>5.0.2.RELEASE</spring.version>
</properties>

 

<!--添加springspringmvc依赖-->
<!--  aop   -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>${spring.version}</version>
</dependency>

<!--  aspects   -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aspects</artifactId>
  <version>${spring.version}</version>
</dependency>
<!--  beans   -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>${spring.version}</version>
</dependency>
<!--  contexts   -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${spring.version}</version>
</dependency>
<!--  core    -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>${spring.version}</version>
</dependency>
<!--  expression   -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-expression</artifactId>
  <version>${spring.version}</version>
</dependency>
<!--  orm   -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-orm</artifactId>
  <version>${spring.version}</version>
</dependency>
<!--  jdbc   -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>${spring.version}</version>
</dependency>
<!--  test   -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>${spring.version}</version>
</dependency>
<!--  tx   -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>${spring.version}</version>
</dependency>
<!--  web   -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>${spring.version}</version>
</dependency>
<!--  webmvc   -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>${spring.version}</version>
</dependency>

4、要使用springmvc的框架,就需要先配置上前端控制器(servlet),所以是在web.xml中进行配置

<!--配置springmvc的前端控制器-->
<servlet>
  <servlet-name>dispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>dispatcherServlet</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>
 
5、resources目录下增加springmvc的配置文件springmvc.xml,如下图:

6、部署并启动项目

需要先自行下载、安装好tomcat并配置好tomcat的环境变量

 

 

 

 

--------------------------------至此环境搭建完成--------------------------------------

编写入门程序:

主要分为以下几步:

1、index.jsp

2、HelloController.java

3、springmvc.xml(开启注解扫描以及配置视图解析器)

4、web.xml(配置前端控制器以及参数配置加载springmvc.xml和服务启动时初始化前端控制器,此时也加载springmvc.xml)

5、对应路径下放返回结果页面,如success.jsp

具体实现的过程思路如下:

1、  自带的index.jsp没有相关的头信息,如果是中文会乱码,可以删了,在webapp下重新建一个index.jsp,里边写<a>标签超链接,具体代码如下:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/12/4
  Time: 14:04
  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>
    <h3>入门程序</h3>
    <a href="hello">入门程序</a>
</body>
</html>

2、  写一个HelloController类,类上加@Controller注解,该类返回一个字符串,会根据返回的字符串找到对应的页面要让类交给spring容器管理,如果是xml配置的形式,需要在spingmvc.xml中开启组件扫描功能,但是没有context相关的命名空间,需要引入相关的命名空间

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

HelloController具体代码如下:

package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        System.out.println("调用HelloController");
        return  "success";
    }
}

 

3、  Springmvc.xml要生效,就要被加载,在web.xml中使用前端控制器的<init-param>去配置,如下:

<servlet>
  <servlet-name>dispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:springmvc.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>

 

load-on-startup的作用是让容器启动的时候就创建DispatcherServlet对象,如果不配的时候只有在第一次发请求的时候才会创建DispatcherServlet对象

 

springmvc.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:cache="http://www.springframework.org/schema/cache"
       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:lang="http://www.springframework.org/schema/lang"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
      http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
      http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">
    <!--开启注解扫描-->
    <context:component-scan base-package="com.example" />
    <!--视图解析器,根据Controller返回的字符串找对应的文件-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--文件路径-->
        <property name="prefix" value="/WEB-INF/pages/" />
        <!--文件后缀-->
        <property name="suffix" value=".jsp" />
    </bean>

    <!--开启springmvc框架注解的支持-->
    <mvc:annotation-driven />

</beans>

 

web.xml完整配置如下:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

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

  <!--配置springmvc的前端控制器-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <!--加载springmvc.xml,使其配置生效-->
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!--启动时就创建DispatcherServlet对象,所以启动时就会加载springmvc.xml-->
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
 
</web-app>

4、  在WEB-INF下创建一个pages文件夹,该文件夹下放success.jsp页面,代表返回成功页面

5、  但是springmvc如何根据返回字符串到指定路径下找指定页面呢?需要在springmvc.xml中配置一个视图解析器,内容如下:

<!--视图解析器,根据Controller返回的字符串找对应的文件-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!--文件路径-->
    <property name="prefix" value="/WEB-INF/pages/" />
    <!--文件后缀-->
    <property name="suffix" value=".jsp" />
</bean>

另外还可以配置上springmvc框架注解的支持,后边会用,入门时也可以不写

<!--开启springmvc框架注解的支持-->
<mvc:annotation-driven />

 

猜你喜欢

转载自www.cnblogs.com/xiao1572662/p/11983281.html