前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringMVC 后台跳转总结大全

SpringMVC 后台跳转总结大全

作者头像
静谧星空TEL
发布2021-04-27 10:00:37
6670
发布2021-04-27 10:00:37
举报
文章被收录于专栏:云计算与大数据技术

SpringMVC 后台跳转总结大全

SpringMVC的接参和传参的方式有很多种,在开发的过程中难免会忘记一些方法,

很久不使用了,可以拿代码复制到项目工程下作为Demo随时查看,小白入门开发必备!!!

常用的方式:将请求参数名作为Controller中方法的形参

代码语言:javascript
复制
	@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 map = new HashMap();
		map.put("username", username);
		map.put("password", password);
		map.put("age", age);
		mv.addObject("map", map);
		return mv;
	}

方式一:方法的形参接收页面参数

代码语言:javascript
复制
    @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效果一样

代码语言:javascript
复制
    @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格式的数据

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

方式四:在参数上加上@RequestBody注解后就可以接收到前端传来的json格式的数据

代码语言:javascript
复制
    @RequestMapping("/test2")
    @ResponseBody
    public void test(@RequestBody List list){
      for (User user:list){
          System.out.println(user);
      }
    }

方式五:使用路径变量@PathVariable绑定页面url路径的参数,将URL中的占位符参数传入到方法参数变量中

代码语言:javascript
复制
    @RequestMapping("/goUrl/{folder}/{file}")
    public String goUrl(@PathVariable String folder,@PathVariable String file){
        return  folder+"/"+file;
    }

方式六:使用原生的Servlet API 作为Controller 方法的参数

代码语言:javascript
复制
    @RequestMapping("/user/login")
    public ModelAndView userLogin3(@RequestParam HashMap param,
                                    HttpSession session,HttpServletRequest request,HttpServletResponse response) {
        ModelAndView mv = new ModelAndView();
        HashMap 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传到页面中去

代码语言:javascript
复制
	@RequestMapping(value = "/recive", method = RequestMethod.GET)
	public ModelAndView StartPage3(@ModelAttribute("user") User user) {
		user.setId(1);
		return new ModelAndView("xxx");
	}

方式八:使用Map、Model和ModelMap的方式

代码语言:javascript
复制
    @RequestMapping("/test")
    public String test(Map 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 }        
    }

所有代码:SpringMVCDemo .java

代码语言:javascript
复制
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 map = new HashMap();
		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 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 param,
                                    HttpSession session,HttpServletRequest request,HttpServletResponse response) {
        ModelAndView mv = new ModelAndView();
        HashMap 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 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的接参和传参的方式有很多种,在开发的过程中难免会忘记一些方法,

很久不使用了,可以拿代码复制到项目工程下作为Demo随时查看,小白入门开发必备!!!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SpringMVC 后台跳转总结大全
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档