(一)測試學習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項目和配置

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