Spring-MVC简单测试
Posted Vodka~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring-MVC简单测试相关的知识,希望对你有一定的参考价值。
1.环境配置
1.maven-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>org.example</groupId>
<artifactId>GranduationDesignment</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>GranduationDesignment 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.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- 测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<!--日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
<!--J2EE-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- jsp -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<!-- 模板语言 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--mysql驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<!--springframework-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<!--其他需要的包-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
</project>
1.在WEB-INF目录下的: web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>GraduationDesign</display-name>
<!-- 部署 DispatcherServlet -->
<servlet>
<servlet-name>GraduationServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 表示容器再启动时立即加载servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>GraduationServlet</servlet-name>
<!-- 处理所有URL -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.在WEB-INF目录下的: GraduationServlet-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!-- 设置spring自动化扫描注解范围 -->
<context:component-scan base-package="com.vodka" />
<!-- viewResolver -->
<bean id="viewResolever" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/WebPage/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
2.目录结构
3.请求处理类
package com.vodka.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
/**
* @author Vodka
* @date 2022/03//10:34
*/
@Controller
public class RequestMappingInstances
//处理登录请求,带指定参数的请求,才可以访问 Login() 方法, name="xxx",是注释
@RequestMapping(value="/Login",name = "登录处理")
public ModelAndView Login()
ModelAndView modelAndView = new ModelAndView();
List<String> MyString = new ArrayList<>();
MyString.add("Vodka");
MyString.add("Jucy");
MyString.add("Lora");
MyString.add("Jack");
modelAndView.addObject("NameList",MyString); //消息设置
modelAndView.setViewName("Login"); //设置视图 (即跳转地址)
return modelAndView; //处理相关请求后,返回消息和视图模型
//处理注册请求,consumes指定处理请求提交的内容类型, produces用于指定返回的内容类型
@RequestMapping(value="/Register" ,name = "注册处理")
public String Register(HttpSession httpSession)
httpSession.setAttribute("name","Vodka");
return "Register"; //处理相关请求后,返回页面名称,因为在Servlet配置里,已经配置好前缀和后缀,所以直接写名称
4.页面
1.index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>
<%--jstl核心库--%>
<%@ taglib prefix="v" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<a href="$pageContext.request.contextPath/Login">登录界面</a>
<a href="$pageContext.request.contextPath/Register">注册界面</a>
</body>
</html>
2.Login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>
<%--引入的jstl核心库--%>
<%@ taglib prefix="v" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>登录界面</title>
</head>
<body>
<v:forEach items="$NameList" var="name">
<h1>欢迎$name来到登录界面</h1>
</v:forEach>
</body>
</html>
3.Register.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%--引入的jstl核心库--%>
<%@ taglib prefix="v" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>注册界面</title>
</head>
<body>
<h1>欢迎$sessionScope.get("name")来到注册界面</h1>
</body>
</html>
5.效果图
以上是关于Spring-MVC简单测试的主要内容,如果未能解决你的问题,请参考以下文章
Spring Transactions 导致 spring-mvc 集成测试失败