• 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>
    
  • 相关阅读:
    JavaScript 立即执行函数和闭包
    Visual Studio2017 离线安装
    D19 Sping Boot 入门 Sping框架--Java Web之书城项目(九) AJAX
    D18 Sping Boot 入门 Sping框架--Java Web之书城项目(八) 过滤器
    D17 Sping Boot 入门 Sping框架--Java Web之Filter过滤器
    在Django中写mqtt脚本并使用
    Django对models层数据库的增删改查操作
    前端调用mqtt不能使用1883端口的问题
    vue结合mqtt
    mqtt服务器的安装(2)--mosquitto
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5515494.html
Copyright © 2020-2023  润新知