SpringMVC 后台跳转总结大全

                                 SpringMVC 后台跳转总结大全

 

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

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

 

常用的方式:将请求参数名作为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 "index.jsp";  
    }  

方式四:在参数上加上@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 }        
    }

 

所有代码: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的接参和传参的方式有很多种,在开发的过程中难免会忘记一些方法,

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

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

日月星辰TEL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值