java web springmvc_(一)测试学习JavaWeb之SpringMVC入门篇

前言

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

工具

Tomcat

IDEA

Maven

操作步骤

98f361277ec4

项目结构

1、新建maven项目

98f361277ec4

新建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框架

98f361277ec4

添加springMVC

98f361277ec4

添加springMVC-勾选

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

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

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

98f361277ec4

新建java,test文件夹

98f361277ec4

指定文件夹属性

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

98f361277ec4

文件夹指定属性后颜色变化

新建文件夹用于存放视图及静态资源

98f361277ec4

新建静态资源文件夹

修改web.xml配置文件

98f361277ec4

修改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.

--%>

Title

SpringMVC!

配置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服务

98f361277ec4

配置Tomcat

98f361277ec4

配置Tomcat

98f361277ec4

配置Tomcat

98f361277ec4

配置Tomcat

启动Tomcat并请求页面

启动Tomcat服务。

98f361277ec4

启动Tomcat

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

98f361277ec4

请求页面

至此,正式迈入了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。

98f361277ec4

依赖包

解决方法2:添加Spring框架时,不选择自动下载jar包,而是使用pom.xml配置spring依赖包。

相关参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值