SpringMVC框架接收数据
-
我们使用SpringMVC框架我们有想过如何接收页面传递过来的数据呢,本篇我们就来为大家讲解接收页面数据的四种方式
-
首先创建一个maven项目
-
导入我们的依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
- 我使用了maven中集成Tomcat插件的方式所以加插件,如果不会加
https://blog.youkuaiyun.com/weixin_45089791/article/details/99067243
这篇我有讲到,下面是依赖
<!-- 加入tomcat插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port> <!-- 访问端口-->
<path>添加你的项目路径</path> <!-- 访问路径,一般就是你的项目名-->
</configuration>
</plugin>
- 第一种方式: 使用httpservletrequest接口的方式
//第一种接受页面传参方式使用httpservletrequest的方式
// method = RequestMethod.GET是指定该方法接收什么样的请求,不指定就是都可以接收
@RequestMapping(value = "/show01", method = RequestMethod.GET)
public void show(HttpServletRequest request) {
System.out.println("进入controller");
String name = request.getParameter("name");
String password = request.getParameter("password");
System.out.println(name + "\t" + password);
}
- 第二种方式: 使用和页面name属性相同的参数名和类型
//第二种方式,直接使用方法上和页面name相同的参数进行获取,使用这种方法必须保证参数名和页面的name相同
@RequestMapping(value = "/show02")
public void show01(String name, String password) {
System.out.println(name + "\t" + password);
}
- 第三种方式: 使用对应实体类的set 方法获取页面的数据
//第三种方式使用对应实体类的set 方法获取页面的数据
@RequestMapping(value = "/show03")
public void show02(User user) {
System.out.println(user.getName() + "\t" + user.getPassword());
}
- 第四种方式: 使用给参数上加注解的方式
//第四种使用在参数上加注解的方式,这种方式参数名可与
// 页面的name不对应,但是注解中的参数必须和页面name对应
@RequestMapping(value = "/show04")
public void show03(@RequestParam("name") String userName, @RequestParam("password") String userPassword){
System.out.println(userName+"\t"+userPassword);
}
下面进行案例展示
- 实体类代码,用来配合第三种传参方式的,代码如下
/*
*
* 这是验证mvc框架页面传参到controller使用实体类的set 方式传递
*
* */
public class User {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
- 处理器代码
import javax.servlet.http.HttpServletRequest;
/*
* 这是验证接收页面数据的一个controller
*
* */
@Controller
public class ControllerData {
//第一种接受页面传参方式使用httpservletrequest的方式
// method = RequestMethod.GET是指定该方法接收什么样的请求,不指定就是都可以接收
@RequestMapping(value = "/show01", method = RequestMethod.GET)
public void show(HttpServletRequest request) {
System.out.println("进入controller");
String name = request.getParameter("name");
String password = request.getParameter("password");
System.out.println(name + "\t" + password);
}
//第二种方式,直接使用方法上和页面name相同的参数进行获取,使用这种方法必须保证参数名和页面的name相同
@RequestMapping(value = "/show02")
public void show01(String name, String password) {
System.out.println(name + "\t" + password);
}
//第三种方式使用对应实体类的set 方法获取页面的数据
@RequestMapping(value = "/show03")
public void show02(User user) {
System.out.println(user.getName() + "\t" + user.getPassword());
}
//第四种使用在参数上加注解的方式,这种方式参数名可与
// 页面的name不对应,但是注解中的参数必须和页面name对应
@RequestMapping(value = "/show04")
public void show03(@RequestParam("name") String userName, @RequestParam("password") String userPassword){
System.out.println(userName+"\t"+userPassword);
}
}
- SpringMvc.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 设置扫描器指定扫描路径-->
<!-- <context:component-scan base-package="com.wdhcr.annotation.controller"></context:component-scan>-->
<context:component-scan base-package="com.wdhcr"></context:component-scan>
<!-- 是我们的controller层可以用注解-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
- web.xml文件配置
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringmvcAnnotation.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
- 页面输入框
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>验证页面传递数据</title>
</head>
<body>
<form action="show04.do" method="get">
用户名:<input type="text" name="name">
密码:<input type="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
- 好了,到这里就为大家介绍了SpringMVC接收页面信息的四种方式。。