spring_MVC_02 值传递

本文详细介绍了如何使用SpringMVC实现页面提交数据的获取与返回,包括配置默认参数、定制页面传入的参数名以及向客户端返回数据的方法。通过实例展示了如何在控制器中使用@RequestParam注解来获取请求参数,并且介绍了使用Model进行数据传递的优点。此外,还说明了如何获取request对象和session对象等,提供了完整的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Spring MVC 实现页面提交数据的获取与返回

 

使用@RequestParam(required=true) ,表示页面请求必须传入参数

package com.hqh.student.controller;

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

//配置为控制器
@Controller
public class FirstController {
	
	//配置请求映射策略
	@RequestMapping(value={"/hello"})
	public String hello(@RequestParam(required=true) int id) {
		System.out.println("FirstController.hello()");
		return "hello";
	}
}

如果请求的URL没有传入参数,会报错,提示没有传递需要的参数值

正确的访问方式: 

http://localhost:8080/spring_mvc_01/hello?id=1

 

配置默认参数

package com.hqh.student.controller;

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

//配置为控制器
@Controller
public class FirstController {
	
	//配置请求映射策略
	@RequestMapping(value={"/hello"})
	public String hello(@RequestParam(required=true,defaultValue="100") int id) {
		System.out.println(id);
		return "hello";
	}
}

 

如果没有传递参数,使用默认值,而不会报错

http://localhost:8080/spring_mvc_01/hello

结果:100

如果传入了参数,则使用传递的参数,默认值无效

http://localhost:8080/spring_mvc_01/hello?id=9 

结果:9

 

定制页面传入的参数名

package com.hqh.student.controller;

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

//配置为控制器
@Controller
public class FirstController {
	
	//配置请求映射策略
	@RequestMapping(value={"/hello"})
	public String hello(@RequestParam(value="userId") int id) {
		System.out.println(id);
		return "hello";
	}
}

 指定页面传递的参数名称为userId,只有当参数名为userId时,才能正确访问

http://localhost:8080/spring_mvc_01/hello?userId=1

结果:1

 

 

向客户端返回数据

通过MAP传递数据,只要在方法的参数中定义一个Map,然后往Map中put值,页面通过key就能取到

太NB了!

@RequestMapping(value={"/say"})
public String say(String name,Map<String,Object> retMap) {
	System.out.println("name="+name);
	retMap.put("say", "spring mvc is very good!");
	return "say";
}

 WEB-INF/jsp/say.js

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h1>${say}</h1>
</body>
</html>

 

请求:http://localhost:8080/spring_mvc_01/say?name=zhangsan

后台获取到的数据:name=zhangsan

页面获取到的数据:spring mvc is very good!

 

 使用Model进行数据传递(推荐)

@RequestMapping(value="/shout")
public String shout(Model model) {
	//没有key,key为Object.即,如果是String,则key为string
	model.addAttribute("struts2 is bad!");
	//通过key,value进行传递
	model.addAttribute("spring_mvc", "so easy!");
	return "shout";
}

 WEB-INF/jsp/shout.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h1>${string}</h1>
	<h1>${spring_mvc}</h1>
</body>
</html>

 URL:http://localhost:8080/spring_mvc_01/shout

返回给页面的结果:

struts2 is bad!

so easy!

 

 获取request对象,session对象等

//获取request对象
@RequestMapping(value={"/execute"})
public String execute(HttpServletRequest request) {
 //spring会自动将request对象传入
 HttpSession session = request.getSession();
 String uri = request.getRequestURI();
 System.out.println(uri);
 return "shout";
}

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值