新建后台代码用以测试返回类型,在这里我新建的如下:
/**
* 项目名称: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类型的返回。在代码中添加如下方法:
/**
* 项目名称: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 {
/**
* Description: 测试一,返回ModelAndVie类型
* @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;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
mav
${mav }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
hello world
Hello world!
最后完整版如下,Java代码:
/**
* 项目名称: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 {
/**
*
* Description: 测试一,返回ModelAndVie类型
*
*
* @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;
}
/**
*
* Description: 跳转到界面为 类路径@RequestMapping的值+方法@RequestMapping的值组成的页面
*
*
* @param
* @return Model
* @throws
*/
@RequestMapping("test2")
public Model test2(Model model) {
model.addAttribute("model", "我的返回类型是Model");
return model;
}
/**
*
* Description: 跳转到界面为 类路径@RequestMapping的值+方法@RequestMapping的值组成的页面
*
*
* @param
* @return ModelMap
* @throws
*/
@RequestMapping("test3")
public ModelMap test3(ModelMap modelMap) {
modelMap.addAttribute("modelMap", "我的返回类型是ModelMap");
return modelMap;
}
/**
*
* Description: 跳转到界面为 类路径@RequestMapping的值+方法@RequestMapping的值组成的页面
*
*
* @param
* @return Map
* @throws
*/
@RequestMapping("test4")
public Map test4(Map map) {
map.put("map", "我的返回类型是Map");
return map;
}
/**
* Description: TODO 返回的页面可以为pdf,excel等。
* @param
* @return View
* @throws
*/
@RequestMapping("test5")
public View test5(){
return null;
}
/**
* Description: 跳转到界面为 类路径@RequestMapping的值+方法@RequestMapping的值组成的页面
* @param
* @return void
* @throws
*/
@RequestMapping("test6")
public void test6(Map map){
map.put("void","我的返回类型是void");
}
/**
* Description: TODO
* @param
* @return String
* @throws
*/
@RequestMapping("test7")
public String test7(Map map){
map.put("string", "我的返回类型是String");
return "return/string";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
hello world
Hello world!
<%--
--%>
最后附上代码结构图: