(一)测试学习SpringMVC之入门篇

前言

近段时间打算把零侵入mock技术落地,所以抽时间学习一些javaweb的知识。这次给自己定的目标是搭建一个测试人员使用的mock平台,涉及前后端等知识,接下来的系列文章也算是个人学习历程的总结吧。

工具

  • Tomcat
  • IDEA
  • Maven

操作步骤

1、新建maven项目


点击Next即可,待下载完成相关配置文件再进行第2步。

2、配置pom.xml文件

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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.hfbank</groupId>
  <artifactId>hfmock</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>hfmock Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <spring.version>4.2.8.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <!-- springframe start -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!-- springframe end -->
  </dependencies>

  <build>
    <finalName>hfmock</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

3、添加spring框架



如果没有Spring选项,可参考该文章提供的解决方法。

4、新建java,test文件夹并指定属性

java文件夹管理源码,指定为Sources。test文件夹用于编写单元测试用例,指定为Tests。




指定属性后可在该文件夹目录下建立Package,Class等,否则没有这些选项,有兴趣的童靴可以对比下。


新建文件夹用于存放视图及静态资源
修改web.xml配置文件

<url-pattern>修改为/,表示拦截所有的请求。

编写controller文件
package com.controller;

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

@Controller
public class IndexController {

    @RequestMapping("/index")
    public String test(){
        return "index";
    }
}
编写css及jsp文件

test.css:表示字体为红色,背景为黄色;

span{
    color: red;
    background: yellow;
}

index.jsp:需注意css文件的相对路径。

<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2019/2/22
  Time: 17:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<!--注意 href="./statics/css/test.css" 表示先返回index.jsp的上一层目录,然后再导向statics目录-->
<link rel="stylesheet" type="text/css" href="./statics/css/test.css"/>
<head>
    <title>Title</title>
</head>
<body>
<span>SpringMVC!</span>
</body>
</html>
配置dispatcher-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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

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

    <mvc:default-servlet-handler/>

    <context:annotation-config/>

    <mvc:annotation-driven/>

    <!--静态资源映射-->
    <!--statics目录下所有文件不会被DispatcherServlet拦截,当做静态资源交给Servlet处理-->
    <mvc:resources mapping="/statics/**" location="/WEB-INF/statics/"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <!--设置JSP文件的目录位置-->
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
        <property name="exposeContextBeansAsAttributes" value="true"/>
    </bean>
</beans>
配置Tomcat服务



启动Tomcat并请求页面

启动Tomcat服务。


然后,请求 http://localhost:8082/index,页面展示如下。

至此,正式迈入了SpringMVC大门。

常见问题

Tomcat启动后,打开页面报错:HTTP Status 500 - Handler processing failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config

  • 解决方法:pom文件添加以下依赖。
 <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
 </dependency>

Tomcat启动报错:Context [] startup failed due to previous errors

  • 解决方法1:将lib的jar包拷贝到WEB-INF目录下,再重新启动Tomcat。
  • 解决方法2:添加Spring框架时,不选择自动下载jar包,而是使用pom.xml配置spring依赖包。

相关参考资料

IDEA建立Spring MVC Hello World 详细入门教程
IDEA用maven创建springMVC项目和配置

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章