创建控制器的方法
package com.bjpowernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MyController {
@RequestMapping(value = "/some.do")
public ModelAndView doSome(){
//处理some.do请求
ModelAndView mv = new ModelAndView();
//添加数据
mv.addObject("msg", "欢迎使用springmvc做web开发");
mv.addObject("fun", "执行的是dosome方法");
//指定视图,指定视图的完整路径
//框架对视图执行的forward操作,request.getRequestDispather("/show.jsp".forward(...))
mv.setViewName("/show.jsp");
//返回mv
return mv;
}
}
ModelAndView返回值类型:既有数据也有视图
通过mv.addObject(键值对)来添加数据,通过mv.setViewName()来添加视图名称
show.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>show.jsp从request作用域获取数据</h3>
<h3>msg数据:${msg}</h3>
<br/>
<h3>fun数据:${fun}</h3>
</body>
</html>