Spring MVC 中的 forward 和 redirect

http://zachary-guo.iteye.com/blog/1503352

详细参考: http://panyongzheng.iteye.com/blog/1942839
两种返回方式都可以是ModelAndView或者String类型
forward
String url="list";
return new ModelAndView(url);

redirect
String url="redirect:/list.do";
return new ModelAndView(url);




package com.pas.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class LoginController {

	@RequestMapping(value = "/forward.do")
	public String forward(@RequestParam("userName") String userName,
			@RequestParam("password") String password,
			@RequestParam("role") String role) {

		System.out.println("Login.....................................");
		return "index";//使用forward方式
	}
	
	@RequestMapping(value = "/redirect.do")
	public String redirect(@RequestParam("userName") String userName,
			@RequestParam("password") String password,
			@RequestParam("role") String role) {

		System.out.println("Login.....................................");
		return "redirect:index.jsp";//重定向方式
	}
	
	
}


或者这种方式:
public ModelAndView login(@RequestParam("userName") String userName,
			@RequestParam("password") String password,
			@RequestParam("role") String role) {
		System.out.println("Login.....................................");
		ModelAndView view = null;
		if (StringUtils.equalsIgnoreCase("pandy", userName)
				&& StringUtils.equalsIgnoreCase("pandy", password)) {
			view = new ModelAndView(new RedirectView("index.jsp"));
		} else {
			view = new ModelAndView("../login");
		}
		
		return view;
	}

猜你喜欢

转载自panyongzheng.iteye.com/blog/1942853