SpringMVC(二)传值

1、HelloController.java

通过model.addAttribute(key,value)进行传值

package zttc.itat.controller;  
import org.springframework.stereotype.Controller;  
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(username);//默认使用对象的类型作为key,即
model.addAttribute("string",username)
       System.out.println(username);
           return "hello";  
     }   
    @RequestMapping{{"/welcome"}}  
     public String welcome(){  
           System.out.println("welcome");  
           return "welcome";  
      }   
}  

2、hello.jsp

<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title></title>  
</head>  
<body>  
<h1>hello!! ${username}</h1>  
<h2>hello!!!! ${string}</h2>  
</body>

在客户端地址栏输入:localhost……/hello?username=value

原文地址:https://www.cnblogs.com/Donnnnnn/p/5746744.html