Spring MVC接收值传值

本文详细介绍了在Spring MVC框架中如何处理各种类型的参数输入,包括使用HttpServletRequest、方法参数、对象、ModelAndView、ModelMap、@ModelAttribute、Session及重定向等进行数据接收与传递的方法。同时,展示了如何通过不同方式返回视图信息。

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

直接上代码把
那些配置开启扫描的就不贴了,前面博客里有

//这里写的就是单纯的传值接收值,可以自己加些逻辑
@Controller
@RequestMapping("/demo")
public class TestController {
//	1.使用request接收参数
	@RequestMapping("/test1.do")
	public ModelAndView test1(HttpServletRequest request) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println(username);
		System.out.println(password);
		return new ModelAndView("jsp/hello");
	}
//	2.使用方法参数接收参数
	@RequestMapping("/test2.do")
	public ModelAndView test2(String username,@RequestParam("password") String pwd) {
		System.out.println(username);
		System.out.println(pwd);
		return new ModelAndView("jsp/hello");
	}
	
//	3.使用对象接收参数
	@RequestMapping("/test3.do")
	public ModelAndView test3(User user) {
		System.out.println(user.getUsername());
		System.out.println(user.getPassword());
		return new ModelAndView("jsp/hello");
	}


//	1.使用ModelAndView传出数据
	@RequestMapping("/test4.do")
	public ModelAndView test4() {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("success", "true");
		data.put("message", "操作成功");
		return new ModelAndView("jsp/hello",data);
	}
	
//	2.使用ModelMap传出数据
	@RequestMapping("/test5.do")
	public ModelAndView test5(ModelMap modelMap) {
		modelMap.addAttribute("success", false);
		modelMap.addAttribute("message", "操作失败");
		return new ModelAndView("jsp/hello");
	}
//	3.使用@ModelAttribute传出bean水性
	@ModelAttribute("age")
	public int getAge() {
		return 18;
	}
	
//	4.使用@ModelAttribute传输参数值
	@RequestMapping("/test6.do")
	public ModelAndView test6(@ModelAttribute("username") String username,
			String password) {
		return new ModelAndView("jsp/hello");
	}
	
//	5.使用Session
	@RequestMapping("/test7.do")
	public ModelAndView test7(HttpServletRequest request,User user) {
		HttpSession session = request.getSession();
		session.setAttribute("langs", "java");
		return new ModelAndView("jsp/hello");
	}
	
//	6.返回String:逻辑视图信息/WEB-INF/jsp/hello.jsp
	@RequestMapping("/test8.do")
	public String test8(User user,ModelMap modelMap) {
		modelMap.addAttribute("user",user);
		return "jsp/hello";
	}
	
//	7.系统错误页面
	@RequestMapping("/test9.do")
	public String test9() {
		return "jsp/error";
	}

//	8.使用RedirectView重定向
	@RequestMapping("/test10.do")
	public ModelAndView test10(User user) {
		if ("shm".equals(user.getUsername())) {
			return new ModelAndView("jsp/hello");
		}else {
			return new ModelAndView(new RedirectView("test9.do"));
		}
	}

//	9.使用redirect重定向
	@RequestMapping("/test11.do")
	public String test11(User user) {
		if ("shm".equals(user.getUsername())) {
			return "jsp/hello";
		}else {
			return "redirect:test9.do";
		}
	}
}

index.jsp(不要放在WEB-INF下,与WEB-INF同级,否则访问不到)

<body>
	<h1>测试表单</h1>
	<form action="demo/test(1,2,3...).do" method="post">
		账号:<input type="text" name="username"/><br>
		密码:<input type="password" name="password"/><br>
		<input type="submit" value="登录">
	</form>
</body>

hello.jsp

<body>
	<a>Hello,SpringMVC</a>
	<h1>success: ${success}</h1>
	<h1>消息: ${message}</h1>
	<h1>年龄: ${age}</h1>
	<h1>用户名: ${username}</h1>
	<h1>语言: ${sessionScope.langs}</h1>
	<h1>对象: ${user.username}</h1>
</body>

error.jsp

<body>
	<h1>error!!!</h1>
</body>

User

/**
 * 创建javabean用来分装前端传值
 * @author Administrator
 *
 */
public class User {
	private Integer id;
	private String username;
	private String password;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}

测试
test1.do
在这里插入图片描述
在这里插入图片描述
test2.do
在这里插入图片描述
在这里插入图片描述
test3.do
在这里插入图片描述
test4.do
在这里插入图片描述
test5.do
在这里插入图片描述
test6.do
在这里插入图片描述
test7.do
在这里插入图片描述
test8.do
在这里插入图片描述
test9.do
在这里插入图片描述
test10.do
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
test11.do
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值