spring3 mvc:方法返回值的学习

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**   
 * 项目名称:Spring3mvc demo  
 * Copyright ? 2010-2012 spartacus.org.cn All Rights Reserved  
 */   
package cn.org.spartacus.spring;  
   
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse; 
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestMethod;  
import org.springframework.web.servlet.ModelAndView; 
   
/**  
 * Description: TODO  
 * @author hankaibo  
 * @date 2012-11-4  
 * @version v1.0  
 */  
 @Controller      
 //添加注解,这样配置文件就可以找到它了。  
 public class ReturnController {      
 

  1,测试ModelAndView类型的返回。在代码中添加如下方法:

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

  新建用于接受结果的前台页面mav.jsp:

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

  在success.jsp中添加触发条件

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

  地址栏输入测试,完成。

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

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

  jsp代码如下:

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

  最后附上代码结构图:

posted @   汪强胜  Views(272)  Comments(0Edit  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示