Controller example

最近学习了controller,以下实现,Controller如何接收请求,以及进行页面重定向

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
public class TestController {
	//test
	//example http://127.0.0.1/test.do?param1=12&param2=23   
	@RequestMapping(value = "test.do",//可修改成自己想要的名称
			method = { RequestMethod.GET,RequestMethod.POST })
	public String test(HttpServletRequest request,Model model){
		String param1 = request.getParameter("param1");//从请求中获取值,获取值为12
		String param2 = request.getParameter("param2");//从请求中获取值,获取值为23
		//将获取的参数值放到model中,放到页面的form表单中
		/**
		 * <form action="跳转的地址" method="post">
		 *   <input type="hidden" name="param1" value="${param1}" /> 
		 *   <input type="hidden" name="param2" value="${param2}" /> 
		 * </form>
		 */
		model.addAttribute("param1", param1);
		model.addAttribute("param2", param2);
		return "test";//跳转到test.jsp页面
	}
	//当接收到其他平台的请求后再将页面跳转到另外的平台
	@RequestMapping(value = "/receive.do", method = { RequestMethod.GET,RequestMethod.POST })
	public String redirect(HttpServletRequest request){
		String url="http://www.baidu.com";//修改成需要跳转平台的url
		//获取参数
		String param1 = request.getParameter("param1");
		String param2 = request.getParameter("param2");
		//拼接重定向参数,参数名可以修改
		String redirect=url+"?param1="+param1+"&param2="+param2;
		return "redirect:"+redirect;
	}
}

 

你可能感兴趣的:(controller)