【springMVC】获取客户端数据

1 前言

        springMVC 获取客户端数据的方法主要有以下3种:

  • 通过 HttpServletRequest 对象的 getParameter() 方法获取
  • 通过匹配参数名获取
  • 通过 POJO 获取

        实验需要导入的 JAR 包如下:

        笔者工作目录如下:  

        首先介绍下本文各节公共的文件,主要有 web.xml、applicationContext.xml、success.jsp,不同的是 Test.java 和 index.jsp,将在各节都介绍。

        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" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> 
  	<!-- 首页网页 -->
	<welcome-file-list>
		<welcome-file>/WEB-INF/view/index.jsp</welcome-file>
	</welcome-file-list>
  
  	<!-- 配置核心(前端)控制器 DispatcherServlet -->
	<servlet>
  		<servlet-name>dispatcherServlet</servlet-name>
  		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  		<init-param>
  			<!-- 加载IOC容器配置文件 -->
  			<param-name>contextConfigLocation</param-name>
  			<param-value>classpath:applicationContext.xml</param-value> 
  		</init-param>
  	</servlet>
  	<servlet-mapping>
  		<servlet-name>dispatcherServlet</servlet-name>
  		<url-pattern>/</url-pattern>
  	</servlet-mapping>
  
  	<!-- 配置CharacterEncodingFilter,用于解决后台中文乱码 -->
	<filter>
		<filter-name>characterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>characterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

        注意:characterEncodingFilter 用于解决客户端传递的中文参数在服务器端乱码。 

        applicationContext.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"
	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-4.0.xsd">	
	<!-- 扫描组件,将加@Controller注解的类作为SpringMVC的控制层 -->
	<context:component-scan base-package="com.test"></context:component-scan>
	
	<!-- 配置视图解析器 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/view/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>	
</beans>

        success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>success</title>
</head>
<body>
	<h1>成功</h1>
</body>
</html>

2  通过 HttpServletRequest 对象获取客户端数据

        index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>首页</title>
</head>
<body>
	<form action="user" method="post"> 
		用户号:<input type="text" name="uid"><br/>
		用户名:<input type="text" name="uname"><br/>
		<input type="submit" value="提交"/>
	</form>
</body>
</html>

        Test.java

package com.test;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class Test {
	
	@RequestMapping(value="user",method=RequestMethod.POST)
	public String addUser(HttpServletRequest request) {
		String id=request.getParameter("uid");
		String name=request.getParameter("uname");
		System.out.println("id="+id+", name="+name);
		return "success";
	}
}

        在地址栏输入:http://localhost:8080/SpringMVC/,并填写表单,如下:

        提交后,跳转到 success.jsp 页面,并且控制台显示:【id=1001, name=张三】 。

3 通过匹配参数名获取客户端数据

        参数名匹配是指:客户端传递的参数名服务器端请求方法的形参名进行匹配,将传递的参数赋值给同名形参

        本节主要介绍 Test.java,index.jsp 同第2节。

3.1 直接匹配

        Test.java

package com.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class Test {
	
	@RequestMapping(value="user",method=RequestMethod.POST)
	public String addUser(Integer uid,String uname) {
		System.out.println("id="+uid+", name="+uname);
		return "success";
	}
}

        注意:若传递的参数形参匹配失败,程序都不会报错。

  • 传递的参数匹配失败:存在传递的参数在形参列表中匹配不到同名形参,如:传递的参数中多个 address 参数
  • 形参匹配失败:存在形参数在传递的参数中匹配不到同名参数,如:形参中多个 address 参数

 3.2 @RequestParam

        根据传递的参数名与 @RequestParam 的 value 属性值进行匹配,将传递的参数赋值给被 @RequestParam 标注的 value 值同名的形参。

        Test.java

package com.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class Test {
	
	@RequestMapping(value="user",method=RequestMethod.POST)
	public String addUser(@RequestParam(value="uid")Integer id,@RequestParam(value="uname")String name) {
		System.out.println("id="+id+", name="+name);
		return "success";
	}
}

        注意:

  • 若 @RequestParam 中只有 value 一个属性,可以省略 value,直接显示其值。
  • 形参列表中,若有一个参数被 @RequestParam 标注,其他参数也要被 @RequestParam 标注,否则会报错。
  • 若存在传递的参数匹配失败,如:传递的参数中多个 address 参数,程序不会报错。
  • 若存在 value 匹配失败,如:多了个值为 address 的 value ,程序会报错。

        思考:也许读者会疑问,由于 value 值必须要匹配成功,程序员就必须事先知道用户传递的参数名,那为什么不用3.1中方法呢?3.1不更简单么?在实际应用中,若客户端传递的参数名发生更改,使用3.1中方法,服务器端的方法形参名及其内部形参引用都得同步更改,而3.2中方法只需要更改 value。

        拓展

        (1)required属性

        当 value 匹配失败时,若将 @RequestParam 的 required 属性设置为 false,程序也不会报错。使用方式如下:

@RequestMapping(value="user",method=RequestMethod.POST)
public String addUser(@RequestParam("uid")Integer id, @RequestParam(value="address",required=false)String add) {

        (2)defaultValue属性

        当 value 匹配失败时,若给 @RequestParam 的 defaultValue 属性赋值,程序也不会报错,并且会给被标注的形参赋默认值。使用方式如下:

@RequestMapping(value="user",method=RequestMethod.POST)
public String addUser(@RequestParam("uid")Integer id,@RequestParam(value="address",defaultValue="北京")String add) {

4 通过 POJO 获取客户端数据

        POJO(Plain Ordinary Java Object)是【简单的Java对象】的简称,实际就是普通 JavaBeans,其中有一些属性及其 getter 和 setter 方法,没有业务逻辑,可以作为 VO(value-object)或 DTO(Data Transform Object)来使用。

        通过 POJO 获取客户端数据,即请求方法中通过一个 POJO 形参接收并封装客户端传递的参数

4.1 普通 POJO

        本节主要介绍 User.java、Test.java,index.jsp 同第2节。

        User.java

package com.test;

public class User {
	private Integer uid;
	private String uname;
	
	public Integer getUid() {
		return uid;
	}
	
	public void setUid(Integer uid) {
		this.uid = uid;
	}
	
	public String getUname() {
		return uname;
	}
	
	public void setUname(String uname) {
		this.uname = uname;
	}

	@Override
	public String toString() {
		return "User [uid=" + uid + ", uname=" + uname + "]";
	}
}

        注意:属性名必须与传递参数名一致。 

        Test.java

package com.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class Test {
	
	@RequestMapping(value="user",method=RequestMethod.POST)
	public String addUser(User user) {
		System.out.println(user);
		return "success";
	}
}

        在地址栏输入:http://localhost:8080/SpringMVC/,并填写表单,如下:

        提交后,跳转到 success.jsp 页面,并且控制台显示:

User [uid=1001, uname=张三] 

4.2 带级联关系的 POJO

        带级联关系的POJO是指:POJO 的某属性也是一个 POJO,如:User 的 address 属性包含 province 和 city 两个属性,也是一个POJO。

        index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>首页</title>
</head>
<body>
	<form action="user" method="post"> 
		用户号:<input type="text" name="uid"><br/>
		用户名:<input type="text" name="uname"><br/>
		所在省:<input type="text" name="address.province"><br/>
		所在市:<input type="text" name="address.city"><br/>
		<input type="submit" value="提交"/>
	</form>
</body>
</html>

        注意:级联属性的 name 值采用级联表示,如:address.province、address.city,其前缀必须与 User 的 address 属性同名,后缀必须与 Address 的属性同名。

        Test.java

package com.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class Test {
	
	@RequestMapping(value="user",method=RequestMethod.POST)
	public String addUser(User user) {
		System.out.println(user);
		return "success";
	}
}

        User.java

package com.test;

public class User {
	private Integer uid;
	private String uname;
	private Adderss address;
	
	public Integer getUid() {
		return uid;
	}
	
	public void setUid(Integer uid) {
		this.uid = uid;
	}
	
	public String getUname() {
		return uname;
	}
	
	public void setUname(String uname) {
		this.uname = uname;
	}

	public Adderss getAddress() {
		return address;
	}

	public void setAddress(Adderss address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "User [uid=" + uid + ", uname=" + uname + ", address=" + address + "]";
	}
}

        Address.java

package com.test;

public class Adderss {
	private String province;
	private String city;
	
	public String getProvince() {
		return province;
	}
	
	public void setProvince(String province) {
		this.province = province;
	}
	
	public String getCity() {
		return city;
	}
	
	public void setCity(String city) {
		this.city = city;
	}
	
	@Override
	public String toString() {
		return "Adderss [province=" + province + ", city=" + city + "]";
	}
}

        在地址栏输入:http://localhost:8080/SpringMVC/,并填写表单,如下:

        提交后,跳转到 success.jsp 页面,并且控制台显示:

User [uid=1001, uname=张三, address=Adderss [province=湖北, city=武汉]]

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

little_fat_sheep

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值