前言
近段時間打算把零侵入mock技術(shù)落地,所以抽時間學(xué)習(xí)一些javaweb的知識。這次給自己定的目標(biāo)是搭建一個測試人員使用的mock平臺,涉及前后端等知識,接下來的系列文章也算是個人學(xué)習(xí)歷程的總結(jié)吧。
工具
- Tomcat
- IDEA
- Maven
操作步驟

項目結(jié)構(gòu)
1、新建maven項目

新建maven項目
點擊Next即可,待下載完成相關(guān)配置文件再進(jìn)行第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框架

添加springMVC

添加springMVC-勾選
如果沒有Spring選項,可參考該文章提供的解決方法。
4、新建java,test文件夾并指定屬性
java文件夾管理源碼,指定為Sources。test文件夾用于編寫單元測試用例,指定為Tests。

新建java,test文件夾

指定文件夾屬性
指定屬性后可在該文件夾目錄下建立Package,Class等,否則沒有這些選項,有興趣的童靴可以對比下。

文件夾指定屬性后顏色變化
新建文件夾用于存放視圖及靜態(tài)資源

新建靜態(tài)資源文件夾
修改web.xml配置文件

修改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的上一層目錄,然后再導(dǎo)向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/>
<!--靜態(tài)資源映射-->
<!--statics目錄下所有文件不會被DispatcherServlet攔截,當(dāng)做靜態(tài)資源交給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"/>
<!--設(shè)置JSP文件的目錄位置-->
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
<property name="exposeContextBeansAsAttributes" value="true"/>
</bean>
</beans>
配置Tomcat服務(wù)

配置Tomcat

配置Tomcat

配置Tomcat

配置Tomcat
啟動Tomcat并請求頁面
啟動Tomcat服務(wù)。

啟動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依賴包。
相關(guān)參考資料
IDEA建立Spring MVC Hello World 詳細(xì)入門教程
IDEA用maven創(chuàng)建springMVC項目和配置
