SpringMVC framework | Three Kind of Writing Handler Processor


A, SpringMVC a processor

After mapper configured SpringMVC processor after processing the adapter viewresolver, processors need to write manually. There are three written about the processor, no matter how write, execute processes are ①处理映射器通过@Controller注解找到处理器, in turn ②通过@RequestMapping注解找到用户输入的url。following sections describe these three ways.

package com.gql.springmvc;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * 类说明:
 *		处理器的三种写法
 * @guoqianliang1998.
 */
@Controller
public class UserController {
	//1.SpringMVC开发方式
	@RequestMapping("/hello")
	public ModelAndView hello(){
		ModelAndView mv = new ModelAndView();
		mv.addObject("msg","hello world!");
		mv.setViewName("index.jsp");
		return mv;
	}
	
	//2.原生Servlet开发方式
	@RequestMapping("xx")
	public void xx(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		request.setAttribute("msg", "周冬雨");
		request.getRequestDispatcher("/index.jsp").forward(request, response);
	}
	
	//3.开发中常用
	@RequestMapping("yy")
	public String yy(Model model){
		model.addAttribute("msg", "双笙");
		return "forward:/index.jsp";//forward写不写都是转发,redirect代表重定向.
	}
}

1.SpringMVC development mode

	@RequestMapping("/hello")
	public ModelAndView hello(){
		ModelAndView mv = new ModelAndView();
		mv.addObject("msg","hello world!");
		mv.setViewName("index.jsp");
		return mv;
	}

2.Servlet way Native Development

	@RequestMapping("xx")
	public void xx(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		request.setAttribute("msg", "周冬雨");
		request.getRequestDispatcher("/index.jsp").forward(request, response);
	}

3. Development in the usual way

In return the string, forward write do not write on behalf of all forwarding, redirect the representatives of the redirection.

	@RequestMapping("yy")
	public String yy(Model model){
		model.addAttribute("msg", "双笙");
		return "forward:/index.jsp";
	}
Published 418 original articles · won praise 1088 · Views 240,000 +

Guess you like

Origin blog.csdn.net/weixin_43691058/article/details/104349096