spring mvc返回类型
来源:互联网 发布:软件服务行业 编辑:程序博客网 时间:2024/05/23 00:41
新建后台代码用以测试返回类型,在这里我新建的如下:
[java] view plaincopy
- /**
- * 项目名称: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 {
- }
[java] view plaincopy
- /**
- * 项目名称: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:
[html] view plaincopy
- <%@ 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中添加触发条件
[html] view plaincopy
- <%@ 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代码:
[java] view plaincopy
- /**
- * 项目名称: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代码如下:
[html] view plaincopy
- <%@ 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>
0 0
- spring mvc返回类型
- spring mvc返回类型
- spring mvc返回值类型
- spring mvc返回值类型
- spring mvc 返回值类型设定
- Spring mvc转向以及设置返回类型
- spring mvc @ResponseBody 返回枚举类型
- Spring MVC Controller 支持的返回类型
- spring mvc返回json 类型的 数据
- spring mvc controller返回值的类型
- Spring MVC返回BLOB类型的图片
- spring mvc 返回json类型设置
- Spring MVC学习:处理方法返回值的可选类型
- spring mvc 返回Json数据时日期类型转换问题
- Spring MVC学习:处理方法返回值的可选类型
- Spring MVC处理方法返回值的可选类型
- spring mvc 控制器(Controller)中可以返回的类型
- Spring mvc交互层返回类型学习笔记
- 最少转机——图的广度优先遍历
- 十一月总结(11.30)
- 十一月总结
- C语言学习第一课,新手比较疑惑的几个问题
- C#生成二维码(含解码)
- spring mvc返回类型
- 内存映射MMAP和DMA
- Java LinkedList基本用法
- Oracle 学习过程之用户管理
- instance message is a forward declaration
- AOP 技术概览
- java保留两位小数4种方法
- mac安装redis
- 函数版5个数的升序降序