SpringMVC方法返回值的学习(注解版)
/** * 项目名称: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 { }
新建用于接受结果的前台页面mav.jsp:
<%@ 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中添加触发条件
<%@ 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代码:
/** * 项目名称: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代码如下:
<%@ 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>
相关推荐
JudeJoo 2020-08-21
meleto 2020-08-15
itjavashuai 2020-08-15
zhongliwen 2020-07-05
haidaoxianzi 2020-07-04
小鱿鱼 2020-06-26
haidaoxianzi 2020-06-17
MicroBoy 2020-06-17
牧场SZShepherd 2020-06-17
neweastsun 2020-06-16
haidaoxianzi 2020-06-14
zhongliwen 2020-06-13
@RequestBody注解实现接收http请求的json数据,将json数据转换为java对象进行绑定。加上@ResponseBody注解,就不会走视图解析器,不会返回页面,目前返回的json数据。
DumbbellYang 2020-06-13
csuzxm000 2020-06-09
haidaoxianzi 2020-06-03
qingjiuquan 2020-05-30
84560296 2020-05-30
咻pur慢 2020-05-27