接上篇,结构无大变化。
新建后台代码用以测试返回类型,在这里我新建的如下:
1.
2. package cn.org.spartacus.spring;
3.
4. import javax.servlet.http.HttpServletRequest;
5. import javax.servlet.http.HttpServletResponse;
6. import org.springframework.stereotype.Controller;
7. import org.springframework.web.bind.annotation.RequestMapping;
8. import org.springframework.web.bind.annotation.RequestMethod;
9. import org.springframework.web.servlet.ModelAndView;
10.
11.
12. @Controller
13. //添加注解,这样配置文件就可以找到它了。
14. public class ReturnController {
15. }
1,测试ModelAndView类型的返回。在代码中添加如下方法:
1.
2. package cn.org.spartacus.spring;
3.
4. import javax.servlet.http.HttpServletRequest;
5. import javax.servlet.http.HttpServletResponse;
6.
7. import org.springframework.stereotype.Controller;
8. import org.springframework.web.bind.annotation.RequestMapping;
9. import org.springframework.web.bind.annotation.RequestMethod;
10. import org.springframework.web.servlet.ModelAndView;
11.
12.
13. @Controller //添加注解,这样配置文件就可以找到它了。
14. @RequestMapping("return")
15. public class ReturnController {
16.
17. @RequestMapping(value="test1",method=RequestMethod.GET)
18. public ModelAndView test1(HttpServletRequest request,HttpServletResponse response){
19. ModelAndView mav=new ModelAndView();
20. mav.setViewName("mav"); //设置返回的文件名
21. mav.addObject("mav", "我的返回类型是ModelAndView.");
22. return mav;
23.
24. }
25.
26. }
新建用于接受结果的前台页面mav.jsp:
1. <</span>%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
2. <</span>%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
3.
4. <</span>html>
5. <</span>head>
6. <</span>meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7. <</span>title>mav</</span>title>
8. </</span>head>
9. <</span>body>
10. ${mav }
11. </</span>body>
12. </</span>html>