Springmvc入门(三)如何获取前端页面传过来的内容

如何前后端交互,前端页面传了内容,如何获取?

1.请求参数名称与方法参数名称一致

先写一个index.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

	<form action="login" method="post">
		<input type="text" name="id" /><br>
		<input type="password" name="password" /><br>
		<input type="submit" value="登陆">
	</form>
	
	<a href="go?number=1">gogogo</a>
	
</body>
</html>

在编写一个Controller类

package springmvc.helloworld;

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.ResponseBody;

@Controller
public class GetParam {

	@ResponseBody
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String loginAction(String id, String password) {
		System.out.println(id + " " + password);
		return "hello";
	}

	@ResponseBody
	@RequestMapping(value = "/go", method = RequestMethod.GET)
	public String goAction(Integer number) {
		System.out.println(number);
		return "hello : " + number;
	}
}

 

注意:

方法参数的名称必须与post请求中的name的名称对应,get请求需要对应参数名称,个数必须一致,否则请求响应不了。

 

测试:输入test,123456进行验证

两个都成功,第二个我们用了Integer类型来接收number,说明springmvc中能根据方法参数的类型进行自动转换。

 

使用@RequestParam

如果请求参数名称跟方法参数名称不对应,如何使用获取前端发送的内容。

package springmvc.helloworld;

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;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class GetParam {

	@ResponseBody
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String loginAction(@RequestParam("id")String zhanghao, @RequestParam("password")String word) {
		return "hello :" + zhanghao + " " + word;
	}

	@ResponseBody
	@RequestMapping(value = "/go", method = RequestMethod.GET)
	public String goAction(Integer number) {
		System.out.println(number);
		return "hello : " + number;
	}
}

进行页面测试:页面index.jsp输入123,123

利用@RequestParam对应请求的名称,所以方法参数的名称可以与请求的名称不一致,但@RequestParam还是要对应,不如直接参数名称对应,不使用这个。但是不加这个,方法的参数的个数必须一致,多一个少一个都会导致响应不了。比如分页查询,假设页面没有传页数过来,表示是首页,传了参数过来就是参数对应的页数,写多个方法?显然不是好的处理方法。

@RequestParam方法里面有几个属性可以赋值

value:请求参数名称

defaultValue:给此值附上默认值,假设页面没传来值,则附上默认值

required:值为false,表示参数值可以不传递,默认会false,true的话,必须得传此值

@ResponseBody
	@RequestMapping(value = "/go", method = RequestMethod.GET)
	public String goAction(Integer number,@RequestParam(value="size",defaultValue="1",required=false)Integer size) {
		System.out.println(number + " " + size );
		return "hello : " + number;
	}

继续调用页面的超链接发现即使不传size的值,也是可以处理的,

控制台打印:

测试传两个值的

控制台打印:

 

@RequestParam注解可以更好的控制参数的传递,假设页面没有此参数值,可以给予默认值以及设置此值是否必须的值。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值