A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

一、SpringMVC中的处理器
配置完SpringMVC的处理器映射器,处理适配器,视图解析器后,需要手动写处理器。关于处理器的写法有三种,无论怎么写,执行流程都是①处理映射器通过@Controller注解找到处理器,继而②通过@RequestMapping注解找到用户输入的url。下面分别介绍这三种方式。
[Java] 纯文本查看 复制代码
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开发方式
[Java] 纯文本查看 复制代码
	@RequestMapping("/hello")
	public ModelAndView hello(){
		ModelAndView mv = new ModelAndView();
		mv.addObject("msg","hello world!");
		mv.setViewName("index.jsp");
		return mv;
	}

2.Servlet原生开发方式
[Java] 纯文本查看 复制代码
	@RequestMapping("xx")
	public void xx(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		request.setAttribute("msg", "周冬雨");
		request.getRequestDispatcher("/index.jsp").forward(request, response);
	}

3.开发中常用的方式
在return的字符串中,forward写不写都是代表转发,redirect则代表重定向。
[Java] 纯文本查看 复制代码
	@RequestMapping("yy")
	public String yy(Model model){
		model.addAttribute("msg", "双笙");
		return "forward:/index.jsp";
	}


161 个回复

正序浏览

努力,加油哦~
回复 使用道具 举报

努力,加油哦~
回复 使用道具 举报

努力,加油哦~
回复 使用道具 举报
1467584 发表于 2020-3-5 14:38
66666666666666666666666666666

努力,加油哦~
回复 使用道具 举报

努力,加油哦~
回复 使用道具 举报

努力,加油哦~
回复 使用道具 举报

努力,加油哦~
回复 使用道具 举报
lzq123 发表于 2020-3-5 18:54
66666666666666666666666666666666666

努力,加油哦~
回复 使用道具 举报
1467584 发表于 2020-3-5 14:38
66666666666666666666666666666

努力,加油哦~
回复 使用道具 举报
九月丫 发表于 2020-3-5 17:17
6666666666666666666666666666666666666666666

努力,加油哦~
回复 使用道具 举报
lzq123 发表于 2020-3-5 18:54
66666666666666666666666666666666666

努力,加油哦~
回复 使用道具 举报
我是小圆圆 发表于 2020-3-5 16:51
改变是痛苦的,但成长的过程更是喜悦的,一定要加油!

努力,加油哦~
回复 使用道具 举报
hongping 发表于 2020-3-6 10:07
感谢分享  加油哦~

努力,加油哦~
回复 使用道具 举报
daoqin 发表于 2020-3-6 10:21
棒棒哒,感谢分享

努力,加油哦~
回复 使用道具 举报
daoqin 发表于 2020-3-6 10:21
棒棒哒,感谢分享

努力,加油哦~
回复 使用道具 举报
hongping 发表于 2020-3-6 10:07
感谢分享  加油哦~

努力,加油哦~
回复 使用道具 举报

努力,加油哦~
回复 使用道具 举报

努力,加油哦~
回复 使用道具 举报

努力,加油哦~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马