SpringMVC jump back Daquan summary

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq262593421/article/details/100401412

                                 SpringMVC jump back Daquan summary

 

SpringMVC access parameters and parameter passing There are many ways in the development process will inevitably forget some ways,

A long time do not use, you can copy the code to take any time to view a Demo next project, White Starter necessary! ! !

 

Common way: the request parameter name in a parameter Controller Method

	@ModelAttribute("/getName")
	public ModelAndView getName(String username, String password, Integer age) {
		//ModelAndView mav = new ModelAndView("redirect:/index.jsp");
		ModelAndView mv = new ModelAndView();
		mv.setViewName("index.jsp");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("username", username);
		map.put("password", password);
		map.put("age", age);
		mv.addObject("map", map);
		return mv;
	}

Way: shape parameter receiving process parameter page

    @RequestMapping("/login1")  
    public String login1(User user,String[] str,Model model){  
        user.getId();
        model.addAttribute("user", user);
        model.addAttribute("str", str);
        return "";
    }

Second way: by binding the page came @RequestParam parameters, and the same effect request.getParameter

    @RequestMapping("/login2")  
    public String login2(@RequestParam("username") String username,  
                        @RequestParam("password") String password,
                        Model model){  
        if (username.equals(password)){  
            model.addAttribute("username", username);  
            return "ok.jsp";  
        } else {  
            return "no.jsp";  
        }  
    }

Three ways: on the parameter after adding @RequestBody annotation may receive data transmitted from the front end of the format json

    @RequestMapping("/test1")
    @ResponseBody
    public String test(@RequestBody String[] arr){  
        for(String a:arr){  
            System.out.println(a);  
        }  
        return "index.jsp";  
    }  

Four ways: on the parameter after adding @RequestBody annotation may receive data transmitted from the front end of the format json

    @RequestMapping("/test2")
    @ResponseBody
    public void test(@RequestBody List<User> list){
      for (User user:list){
          System.out.println(user);
      }
    }

Way five: argument uses a path variable @PathVariable Binding page url path, the placeholder parameters in the URL passed to the method parameter variables

    @RequestMapping("/goUrl/{folder}/{file}")
    public String goUrl(@PathVariable String folder,@PathVariable String file){
        return  folder+"/"+file;
    }

Six ways: using the native Servlet API methods as parameters Controller

    @RequestMapping("/user/login")
    public ModelAndView userLogin3(@RequestParam HashMap<String, Object> param,
                                    HttpSession session,HttpServletRequest request,HttpServletResponse response) {
        ModelAndView mv = new ModelAndView();
        HashMap<String, Object> loginUser = param;
        if(loginUser == null) {
            mv.addObject("msg", "账号不能为空!");
            mv.setViewName("index");
        } else {
            session.setAttribute("loginUser", loginUser);
            mv.addObject("msg", "登陆成功!");
            mv.setViewName("user/index.jsp");
        }
        return mv;
    }

Seven ways: @ModelAttribute used on function parameters, in the end you can go to a page by page spread HttpServletRequest

	@RequestMapping(value = "/recive", method = RequestMethod.GET)
	public ModelAndView StartPage3(@ModelAttribute("user") User user) {
		user.setId(1);
		return new ModelAndView("xxx");
	}

Form 8: Using Map, Model and ModelMap way

    @RequestMapping("/test")
    public String test(Map<String,Object> map,Model model,ModelMap modelMap,HttpServletRequest request){
        //1.放在map里  
        map.put("names", Arrays.asList("李白","小白","小黑"));
        //2.放在model里 建议使用
        model.addAttribute("time", new Date());
        //3.放在request里  
        request.setAttribute("request", "requestValue");
        //4.放在modelMap中 
        modelMap.addAttribute("city", "北京");
        modelMap.put("gender", "male");
        return "hello";
//        页面获取
//        names:${requestScope.names }
//        time:${requestScope.time}        
//        city:${requestScope.city }
//        request:${requestScope.request}
//        gender:${requestScope.gender }        
    }

 

All the code: SpringMVCDemo .java

package com.gxwz.web;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gxwz.entity.User;

@Controller
public class SpringMVCDemo {

	private String result;  //定义跳转链接
	
	// 最常用的方式:将请求参数名作为Controller中方法的形参
	@ModelAttribute("/getName")
	public ModelAndView getName(String username, String password, Integer age) {
		//ModelAndView mav = new ModelAndView("redirect:/index.jsp");
		ModelAndView mv = new ModelAndView();
		mv.setViewName("index.jsp");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("username", username);
		map.put("password", password);
		map.put("age", age);
		mv.addObject("map", map);
		return mv;
	}
    
	// 方式一:方法的形参接收页面参数
    @RequestMapping("/login1")  
    public String login1(User user,String[] str,Model model){  
        user.getId();
        model.addAttribute("user", user);
        model.addAttribute("str", str);
        return "";
    }
    
	// 方式二: 通过@RequestParam绑定页面传来的参数,和request.getParameter效果一样
    @RequestMapping("/login2")  
    public String login2(@RequestParam("username") String username,  
                        @RequestParam("password") String password,
                        Model model){  
        if (username.equals(password)){  
            model.addAttribute("username", username);  
            return "ok.jsp";  
        } else {  
            return "no.jsp";  
        }  
    }  
    
    // 方式三:在参数上加上@RequestBody注解后就可以接收到前端传来的json格式的数据
    @RequestMapping("/test1")
    @ResponseBody
    public String test(@RequestBody String[] arr){  
        for(String a:arr){  
            System.out.println(a);  
        }  
        return "";  
    }  
    
    // 方式四:在参数上加上@RequestBody注解后就可以接收到前端传来的json格式的数据
    @RequestMapping("/test2")
    @ResponseBody
    public void test(@RequestBody List<User> list){
      for (User user:list){
          System.out.println(user);
      }
    }
    
    // 方式五:使用路径变量@PathVariable绑定页面url路径的参数,将URL中的占位符参数传入到方法参数变量中
    @RequestMapping("/goUrl/{folder}/{file}")
    public String goUrl(@PathVariable String folder,@PathVariable String file){
        return  folder+"/"+file;
    }
    
    // 方式六:使用原生的Servlet API 作为Controller 方法的参数
    @RequestMapping("/user/login")
    public ModelAndView userLogin3(@RequestParam HashMap<String, Object> param,
                                    HttpSession session,HttpServletRequest request,HttpServletResponse response) {
        ModelAndView mv = new ModelAndView();
        HashMap<String, Object> loginUser = param;
        if(loginUser == null) {
            mv.addObject("msg", "账号不能为空!");
            mv.setViewName("index");
        } else {
            session.setAttribute("loginUser", loginUser);
            mv.addObject("msg", "登陆成功!");
            mv.setViewName("user/index.jsp");
        }
        return mv;
    }
    
	// 方式七:@ModelAttribute在函数参数上使用,在页面端可以通过HttpServletRequest传到页面中去
	@RequestMapping(value = "/recive", method = RequestMethod.GET)
	public ModelAndView StartPage3(@ModelAttribute("user") User user) {
		user.setId(1);
		return new ModelAndView("xxx");
	}
	
    // 方式八:使用Map、Model和ModelMap的方式
    @RequestMapping("/test")
    public String test(Map<String,Object> map,Model model,ModelMap modelMap,HttpServletRequest request){
        //1.放在map里  
        map.put("names", Arrays.asList("李白","小白","小黑"));
        //2.放在model里 建议使用
        model.addAttribute("time", new Date());
        //3.放在request里  
        request.setAttribute("request", "requestValue");
        //4.放在modelMap中 
        modelMap.addAttribute("city", "北京");
        modelMap.put("gender", "male");
        return "hello";
//        页面获取
//        names:${requestScope.names }
//        time:${requestScope.time}        
//        city:${requestScope.city }
//        request:${requestScope.request}
//        gender:${requestScope.gender }        
    }
	

}

 

SpringMVC access parameters and parameter passing There are many ways in the development process will inevitably forget some ways,

A long time do not use, you can copy the code to take any time to view a Demo next project, White Starter necessary! ! !

 

 

 

Guess you like

Origin blog.csdn.net/qq262593421/article/details/100401412