前言
近段时间打算把零侵入mock技术落地,所以抽时间学习一些javaweb的知识。这次给自己定的目标是搭建一个测试人员使用的mock平台,涉及前后端等知识,接下来的系列文章也算是个人学习历程的总结吧。
工具
Tomcat
IDEA
Maven
操作步骤
项目结构
1、新建maven项目
新建maven项目
点击Next即可,待下载完成相关配置文件再进行第2步。
2、配置pom.xml文件
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.hfbank
hfmock
1.0-SNAPSHOT
war
hfmock Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
4.2.8.RELEASE
junit
junit
4.11
test
javax.servlet
jstl
1.2
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-test
${spring.version}
hfmock
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
3、添加spring框架
添加springMVC
添加springMVC-勾选
如果没有Spring选项,可参考该文章提供的解决方法。
4、新建java,test文件夹并指定属性
java文件夹管理源码,指定为Sources。test文件夹用于编写单元测试用例,指定为Tests。
新建java,test文件夹
指定文件夹属性
指定属性后可在该文件夹目录下建立Package,Class等,否则没有这些选项,有兴趣的童靴可以对比下。
文件夹指定属性后颜色变化
新建文件夹用于存放视图及静态资源
新建静态资源文件夹
修改web.xml配置文件
修改web.xml
修改为/,表示拦截所有的请求。
编写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.
--%>
TitleSpringMVC!
配置dispatcher-servlet.xml文件
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">
配置Tomcat服务
配置Tomcat
配置Tomcat
配置Tomcat
配置Tomcat
启动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文件添加以下依赖。
javax.servlet
jstl
1.2
Tomcat启动报错:Context [] startup failed due to previous errors
解决方法1:将lib的jar包拷贝到WEB-INF目录下,再重新启动Tomcat。
依赖包
解决方法2:添加Spring框架时,不选择自动下载jar包,而是使用pom.xml配置spring依赖包。
相关参考资料