Spring MVC学习笔记——给Controller和视图传值

  一、给Controller传值,值将显示在控制台

  1.第一种:使用@RequestParam,改HelloController.java

//RequestMapping表示用哪一个url来对应
	@RequestMapping({"/hello","/"})
	public String hello(@RequestParam("username") String username){
		System.out.println("hello");
		System.out.println(username);
		return "hello";
	}

   然后在浏览器中输入请求

http://localhost:8080/SpringMVC_hello/hello?username=123

   控制台可以看到传的值

  但是使用了RequestParam,如果在请求中不传值的话,会报400错误,因为默认把参数作为了地址的一部分

 

  2.第二种,把RequestParam删除,直接String username

	//RequestMapping表示用哪一个url来对应
	@RequestMapping({"/hello","/"})
	public String hello(String username){
		System.out.println("hello");
		System.out.println(username);
		return "hello";
	}

 

 

   这种可以不传值,不传值时候为null

 

  二、再从Controller给视图传值

  1.用Map来传值

  修改HelloController.java文件,用Map<String,Object> context,不过不太建议用Map

package org.common.controller;

import java.util.Map;

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

@Controller
public class HelloController {
	
	//RequestMapping表示用哪一个url来对应,从Controller给视图传值
	@RequestMapping({"/hello","/"})
	public String hello(String username,Map<String,Object> context){
		context.put("username", username);		//从Controller给视图传值
		System.out.println(username);
		System.out.println("hello");
		return "hello";
	}
	
}

 

 

  hello.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>
	<h1>hello!! ${username}!!</h1>
</body>
</html>

 输入

http://localhost:8080/SpringMVC_hello/hello?username=123

 

 

   2.用Model来传值

  HelloController.java文件改为

public String hello(String username,Model model){
	model.addAttribute("String", username);		//使用Model从Controller给视图传值

   或者addAttribute只有一个参数,这时是默认使用username的类型(String)作为参数

//等于model.addAttribute("String",username);
model.addAttribute(username);

 

  完整文件

package org.common.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {
	
	//RequestMapping表示用哪一个url来对应
	@RequestMapping({"/hello","/"}) 
	public String hello(String username,Model model){
		System.out.println("hello");
		model.addAttribute("username", username);
		
		//等于model.addAttribute("String",username);
		model.addAttribute(username);
		//model.addAttribute(new User());等于model.addAttribute("user",new User());
		System.out.println(username);
		return "hello";
	}
	
	@RequestMapping("/welcome")
	public String welcome(){
		System.out.println("welcome");
		return "welcome";
	}
	
}

   hello.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>
	<h1>hello!! ${username}!!</h1>
	${string}
</body>
</html>

 

转载于:https://www.cnblogs.com/tonglin0325/p/5515494.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值