Spring-接收请求参数

参数绑定

案例

在这里插入图片描述

package com.sxt.controller;

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

/**
 * 基于注解的自定义的controller
 * @author xieS
 */
@Controller
public class HelloController {
	/**
	 * 接受基本数据类型的参数
	 */
	@RequestMapping("/fun1")
	@ResponseBody
	public void fun1(Integer id, String name){
		System.out.println(id+" - - - - "+name);
	}
	/**
	 * 接受基本数据类型的参数
	 */
	@RequestMapping("/fun2")
	@ResponseBody
	public void fun2(@RequestParam(name="ids")Integer id, String name){
		System.out.println(id+" - - - - "+name);
	}
	/**
	 * 接受基本数据类型的参数
	 */
	@RequestMapping("/fun3")
	@ResponseBody
	public void fun3(@RequestParam(name="ids")Integer id,
			@RequestParam(name="name",required=true)String name){//required=true必须的
		System.out.println(id+" - - - - "+name);
	}
	/**
	 * 接受基本数据类型的参数
	 */
	@RequestMapping("/fun4")
	@ResponseBody
	public void fun4(@RequestParam(name="ids")Integer id,
			@RequestParam(name="name",required=false,defaultValue="xieS")String name){
		// defaultValue="xieS"用默认值替换
		System.out.println(id+" - - - - "+name);
	}
}

配置文件

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		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.3.xsd">
	<!-- 开启扫描 -->
	<context:component-scan base-package="com.sxt.controller"/>
	
	<!-- 开启SpringMVC注解的方式 -->
	<mvc:annotation-driven conversion-service="formattingConversionServiceFactoryBean"></mvc:annotation-driven>
	
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置前后缀 -->
		<property name="prefix" value="/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
</beans>

基本数据类型

Java基本数据类型+String
使用基本数据类型时,参数的名称必须和浏览器传来的参数的key一致,这样才能实现自动映射
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
因为浏览器传的是ids和id不匹配,所以没有取到id值,
可以加==@RequestParam==
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这样就可以取到了,但是加了==@RequestParam==之后,表示这个参数是必须传递的,如果不传递
在这里插入图片描述
就会报错:所需的整数参数“id”不存在
但是它里面还有一个属性
在这里插入图片描述
在这里插入图片描述
如果required为false则表示它不是必须的了
在这里插入图片描述
如果不传入参数的话,defaultValue里的值就会替代
在这里插入图片描述
在这里插入图片描述
传入参数的话,就还是用的传入的参数
在这里插入图片描述
在这里插入图片描述

接受其他类型

package com.sxt.pojo;

import java.util.Date;
import java.util.List;

public class User {

	private Integer id;
	private String name;
	private String address;
	private Book book;
	private String [] favorites;
	private List<String> list;
	private Date birth;
	
	public Date getbirth() {
		return birth;
	}
	public void setbirth(Date birth) {
		this.birth = birth;
	}
	public String[] getFavorites() {
		return favorites;
	}
	public void setFavorites(String[] favorites) {
		this.favorites = favorites;
	}
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public Book getBook() {
		return book;
	}
	public void setBook(Book book) {
		this.book = book;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", address=" + address + ", book=" + book + "]";
	}	
}
package com.sxt.pojo;

public class Book {

	private String bookid;
	private String bookName;
	private String author;
	public String getBookid() {
		return bookid;
	}
	public void setBookid(String bookid) {
		this.bookid = bookid;
	}
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	@Override
	public String toString() {
		return "Book [bookid=" + bookid + ", bookName=" + bookName + ", author=" + author + "]";
	}	
}

UserController

package com.sxt.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sxt.pojo.User;

@Controller
public class UserController {

	@RequestMapping("/add")
	@ResponseBody
	public void insertUser(User user){
		System.out.println(user+" " +user.getbirth());
	}
	@RequestMapping("/add1")
	@ResponseBody
	public void insertUser1(Integer id, String name, String[] favorites){
		System.out.println(id + " - - " + name);
		for (String f : favorites) {
			System.out.println(f);
		}
	}
	@RequestMapping("/add2")
	@ResponseBody
	public void insertUser2(Integer id, String name, List<String> favorites){
		System.out.println(id + " - - " + name);
		for (String f : favorites) {
			System.out.println(f);
		}
	}
	@RequestMapping("/add3")
	@ResponseBody
	public void insertUser3(User user){
		System.out.println(user);
		System.out.println(user.getList());
	}
	@RequestMapping("/add4")
	@ResponseBody
	public void insertUser4(User user){
		System.out.println(user);
		String [] favorites = user.getFavorites();
		for (String f : favorites) {
			System.out.println(f);
		}
	}
}

配置转换器

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		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.3.xsd">
	<!-- 开启扫描 -->
	<context:component-scan base-package="com.sxt.controller"/>
	
	<!-- 开启SpringMVC注解的方式 -->
	<mvc:annotation-driven conversion-service="formattingConversionServiceFactoryBean"></mvc:annotation-driven>
	
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置前后缀 -->
		<property name="prefix" value="/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	
	<!-- 配置自定义的转换器 -->
	<bean id="formattingConversionServiceFactoryBean"
		class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<property name="converters">
			<set>
				<bean class="com.sxt.converter.StringToDateConvert"/>
			</set>
		</property>
	</bean>
</beans>

在这里插入图片描述
在这里插入图片描述
user.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h1>用户管理</h1>
	<form action="add" method="post">
		<table>
			<tr>
				<td>编号</td>
				<td><input type="text" name="id"></td>
			</tr>
			<tr>
				<td>姓名</td>
				<td><input type="text" name="name"></td>
			</tr>
			<tr>
				<td>地址</td>
				<td><input type="text" name="address"></td>
			</tr>
			<tr>
				<td>书籍编号</td>
				<td><input type="text" name="book.bookId"></td>
			</tr>
			<tr>
				<td>书名</td>
				<td><input type="text" name="book.bookName"></td>
			</tr>
			<tr>
				<td>作者</td>
				<td><input type="text" name="book.author"></td>
			</tr>
			<tr>
				<td><input type="submit" value="添加"></td>
			</tr>
		</table>
	</form>
</body>
</html>

在这里插入图片描述
在这里插入图片描述

数组接收

user1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="add1" method="post">
		<table>
			<tr>
				<td>编号</td>
				<td><input type="text" name="id"></td>
			</tr>
			<tr>
				<td>用户名</td>
				<td><input type="text" name="name"></td>
			</tr>
			
			<tr>
				<td>兴趣爱好</td>
				<td>
					<input type="checkbox" name="favorites" value="zuqiu">足球
					<input type="checkbox" name="favorites" value="lanqiu">篮球 
					<input type="checkbox" name="favorites" value="pingpang">乒乓球
				</td>
			</tr>
			<tr>
				<td><input type="submit" value="注册"></td>
			</tr>
		</table>
	</form>
</body>
</html>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

集合接收

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
报错需要改动
在这里插入图片描述
在这里插入图片描述

数组接收
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

接收Date类型

在这里插入图片描述
自定义转换器

package com.sxt.converter;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;
/**
 * 自定义转换器
 * 将字符串类型转换为Date类型
 * @author xieS
 *
 */
public class StringToDateConvert implements Converter<String, Date>{

	@Override
	public Date convert(String msg) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return sdf.parse(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

springmvc.xml

	<!-- 配置自定义的转换器 -->
	<bean id="formattingConversionServiceFactoryBean"
		class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<property name="converters">
			<set>
				<bean class="com.sxt.converter.StringToDateConvert"/>
			</set>
		</property>
	</bean>

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值