spring mvc返回类型

来源:互联网 发布:软件服务行业 编辑:程序博客网 时间:2024/05/23 00:41

新建后台代码用以测试返回类型,在这里我新建的如下:

[java] view plaincopy
  1. /**    
  2.  * 项目名称:Spring3mvc demo   
  3.  * Copyright ? 2010-2012 spartacus.org.cn All Rights Reserved   
  4.  */    
  5. package cn.org.spartacus.spring;   
  6.   
  7. import javax.servlet.http.HttpServletRequest;   
  8. import javax.servlet.http.HttpServletResponse;  
  9. import org.springframework.stereotype.Controller;   
  10. import org.springframework.web.bind.annotation.RequestMapping;   
  11. import org.springframework.web.bind.annotation.RequestMethod;   
  12. import org.springframework.web.servlet.ModelAndView;  
  13.   
  14. /**   
  15.  * Description: TODO   
  16.  * @author hankaibo   
  17.  * @date 2012-11-4   
  18.  * @version v1.0   
  19.  */   
  20.  @Controller       
  21.  //添加注解,这样配置文件就可以找到它了。   
  22.  public class ReturnController {       
  23.  }  
1,测试ModelAndView类型的返回。在代码中添加如下方法:
[java] view plaincopy
  1. /**   
  2.  * 项目名称:Spring3mvc demo 
  3.  * Copyright ? 2010-2012 spartacus.org.cn All Rights Reserved 
  4.  */    
  5. package cn.org.spartacus.spring;    
  6.   
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9.   
  10. import org.springframework.stereotype.Controller;  
  11. import org.springframework.web.bind.annotation.RequestMapping;  
  12. import org.springframework.web.bind.annotation.RequestMethod;  
  13. import org.springframework.web.servlet.ModelAndView;  
  14.   
  15. /** 
  16.  * Description: TODO 
  17.  * @author hankaibo 
  18.  * @date 2012-11-4 
  19.  * @version v1.0 
  20.  */  
  21. @Controller     //添加注解,这样配置文件就可以找到它了。  
  22. @RequestMapping("return")  
  23. public class ReturnController {  
  24.     /** 
  25.      * <p>Description: 测试一,返回ModelAndVie类型</p> 
  26.      * @param  
  27.      * @return ModelAndView 
  28.      */  
  29.     @RequestMapping(value="test1",method=RequestMethod.GET)  
  30.     public ModelAndView test1(HttpServletRequest request,HttpServletResponse response){  
  31.         ModelAndView mav=new ModelAndView();  
  32.         mav.setViewName("mav");     //设置返回的文件名  
  33.         mav.addObject("mav""我的返回类型是ModelAndView.");  
  34.         return mav;  
  35.           
  36.     }  
  37.   
  38. }  

新建用于接受结果的前台页面mav.jsp:
[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>mav</title>  
  8. </head>  
  9. <body>  
  10.     ${mav }  
  11. </body>  
  12. </html>  

在success.jsp中添加触发条件
[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>hello world</title>  
  8. </head>  
  9. <body>  
  10.     Hello world!  
  11.     <c:redirect url="/app/return/test1" />  
  12. </body>  
  13. </html>  

地址栏输入测试,完成。

最后完整版如下,Java代码:

[java] view plaincopy
  1. /**   
  2.  * 项目名称:Spring3mvc demo 
  3.  * Copyright ? 2010-2012 spartacus.org.cn All Rights Reserved 
  4.  */  
  5. package cn.org.spartacus.spring;  
  6.   
  7. import java.util.Map;  
  8.   
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. import org.springframework.stereotype.Controller;  
  13. import org.springframework.ui.Model;  
  14. import org.springframework.ui.ModelMap;  
  15. import org.springframework.web.bind.annotation.RequestMapping;  
  16. import org.springframework.web.bind.annotation.RequestMethod;  
  17. import org.springframework.web.servlet.ModelAndView;  
  18. import org.springframework.web.servlet.View;  
  19.   
  20. /** 
  21.  * Description: TODO 
  22.  *  
  23.  * @author hankaibo 
  24.  * @date 2012-11-4 
  25.  * @version v1.0 
  26.  */  
  27. @Controller  
  28. // 添加注解,这样配置文件就可以找到它了。  
  29. @RequestMapping("return")  
  30. public class ReturnController {  
  31.     /** 
  32.      * <p> 
  33.      * Description: 测试一,返回ModelAndVie类型 
  34.      * </p> 
  35.      *  
  36.      * @param 
  37.      * @return ModelAndView 
  38.      */  
  39.     @RequestMapping(value = "test1", method = RequestMethod.GET)  
  40.     public ModelAndView test1(HttpServletRequest request, HttpServletResponse response) {  
  41.         ModelAndView mav = new ModelAndView();  
  42.         mav.setViewName("return/mav"); // 设置返回的文件名  
  43.         mav.addObject("mav""我的返回类型是ModelAndView.");  
  44.         return mav;  
  45.   
  46.     }  
  47.   
  48.     /** 
  49.      * <p> 
  50.      * Description: 跳转到界面为 类路径@RequestMapping的值+方法@RequestMapping的值组成的页面 
  51.      * </p> 
  52.      *  
  53.      * @param 
  54.      * @return Model 
  55.      * @throws 
  56.      */  
  57.     @RequestMapping("test2")  
  58.     public Model test2(Model model) {  
  59.         model.addAttribute("model""我的返回类型是Model");  
  60.         return model;  
  61.   
  62.     }  
  63.   
  64.     /** 
  65.      * <p> 
  66.      * Description: 跳转到界面为 类路径@RequestMapping的值+方法@RequestMapping的值组成的页面 
  67.      * </p> 
  68.      *  
  69.      * @param 
  70.      * @return ModelMap 
  71.      * @throws 
  72.      */  
  73.     @RequestMapping("test3")  
  74.     public ModelMap test3(ModelMap modelMap) {  
  75.         modelMap.addAttribute("modelMap""我的返回类型是ModelMap");  
  76.         return modelMap;  
  77.     }  
  78.   
  79.     /** 
  80.      * <p> 
  81.      * Description: 跳转到界面为 类路径@RequestMapping的值+方法@RequestMapping的值组成的页面 
  82.      * </p> 
  83.      *  
  84.      * @param 
  85.      * @return Map 
  86.      * @throws 
  87.      */  
  88.     @RequestMapping("test4")  
  89.     public Map test4(Map map) {  
  90.         map.put("map""我的返回类型是Map");  
  91.         return map;  
  92.     }  
  93.       
  94.     /** 
  95.      * <p>Description: TODO 返回的页面可以为pdf,excel等。</p> 
  96.      * @param  
  97.      * @return View 
  98.      * @throws 
  99.      */  
  100.     @RequestMapping("test5")  
  101.     public View test5(){  
  102.         return null;  
  103.     }  
  104.       
  105.     /** 
  106.      * <p>Description: 跳转到界面为 类路径@RequestMapping的值+方法@RequestMapping的值组成的页面</p> 
  107.      * @param  
  108.      * @return void 
  109.      * @throws 
  110.      */  
  111.     @RequestMapping("test6")  
  112.     public void test6(Map<String,Object> map){  
  113.         map.put("void","我的返回类型是void");  
  114.     }  
  115.     /** 
  116.      * <p>Description: TODO</p> 
  117.      * @param  
  118.      * @return String 
  119.      * @throws 
  120.      */  
  121.     @RequestMapping("test7")  
  122.     public String test7(Map<String,Object> map){  
  123.         map.put("string""我的返回类型是String");  
  124.         return "return/string";  
  125.     }  
  126. }  

jsp代码如下:
[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>hello world</title>  
  8. </head>  
  9. <body>  
  10.     Hello world!  
  11.     <c:redirect url="/app/return/test1" />  
  12.     <%--   
  13.     <c:redirect url="/app/return/test2" />  
  14.     <c:redirect url="/app/return/test3" />  
  15.     <c:redirect url="/app/return/test4" />  
  16.     <c:redirect url="/app/return/test5" />  
  17.     <c:redirect url="/app/return/test6" />  
  18.     <c:redirect url="/app/return/test7" />  
  19.      --%>  
  20. </body>  
  21. </html>  
最后附上代码结构图:

0 0
原创粉丝点击