1.设置ModelAndView对象。根据View和视图解析器跳转到指定页面(视图解析器前缀+viewname+视图解析器后缀)

@Override
public ModelAndView handleRequest(HttpServletRequest req,
HttpServletResponse resp) throws Exception {
ModelAndView mv = new ModelAndView();
mv.setViewName("hello");
mv.addObject("msg", "first spring mvc app");
return mv;
}

2.通过ServletAPI对象来实现,不需要配置视图解析器

通过HttpServletRequest输出

resp.getWriter().print("Use httpServletAPI");

通过HttpServletRequest实现重定向

@RequestMapping(value = "/hi.do")
public void hello(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.sendRedirect("hello.jsp");
}

通过HttpServletRequest实现转发

@RequestMapping(value = "/hi.do")
public void hello(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setAttribute("msg", "testRequestDispatcher");
req.getRequestDispatcher("hello.jsp").forward(req, resp);
}

3.通过springMVC来实现重定向和转发

转发

@RequestMapping("/hi2")
public String hello2() {
//转发1
//return "index";
//转发2
return "forward:index.jsp";
}

重定向

@RequestMapping("/hi3.do")
public String hello3(ModelMap map) {
map.addAttribute("msg", "helloworld");
return "redirect:index.jsp";
}

SpringMVC框架学习笔记(4)——结果跳转方式的更多相关文章

  1. SpringMVC框架学习笔记(1)——HelloWorld

    搭建SpringMVC框架 1.添加jar包 jsp-api.jar servlet-api.jar jstl.jar commons-logging-1.1.1.jar spring-beans-4 ...

  2. 兄弟萌,这份SpringMVC框架学习笔记真的建议反复看,写的太细了

    概述 是Spring为展现层提供的基于MVC设计理念的Web框架,通过一套MVC注解,让POJO成为处理请求的控制器,而无需实现任何接口 支持REST风格的URL请求 采用松散耦合的可插拔组件结构,比 ...

  3. SpringMVC框架学习笔记(6)——拦截器

    SpringMVC拦截器需要实现接口HandlerInterceptor 有3个方法,分别在请求处理前.请求处理后和在DispatcherServlet处理后执行 实现代码: package inte ...

  4. SpringMVC框架学习笔记(5)——数据处理

    1.提交数据的处理 a)提交的域名称和参数名称一致 http://localhost:8080/foward/hello.do?name=zhangsan 处理方法 @RequestMapping(v ...

  5. SpringMVC框架学习笔记(2)——使用注解开发SpringMVC

    1.配置web.xml <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.sp ...

  6. SpringMVC框架学习笔记——各种异常、报错解决

    1.Target runtime com.genuitec.runtime.generic.jee60 is not defined. 找到导入项目的.setting文件夹org.eclipse.ws ...

  7. SpringMVC框架学习笔记(3)——controller配置汇总

    1.通过URL对应bean <!-- 配置handlerMapper 映射器 --> <bean class="org.springframework.web.servle ...

  8. phalcon(费尔康)框架学习笔记

    phalcon(费尔康)框架学习笔记 http://www.qixing318.com/article/phalcon-framework-to-study-notes.html 目录结构   pha ...

  9. Yii框架学习笔记(二)将html前端模板整合到框架中

    选择Yii 2.0版本框架的7个理由 http://blog.chedushi.com/archives/8988 刚接触Yii谈一下对Yii框架的看法和感受 http://bbs.csdn.net/ ...

随机推荐

  1. Java I/O---字符与字节转换流---FileReader&FileWriter:

      public class SubTransStreamDemo { /** * @param args * @throws IOException */ public static void ma ...

  2. Xamarin Android Fragment的两种加载方式

    android Fragment的重点: 3.0版本后引入,即minSdk要大于11 Fragment需要嵌套在Activity中使用,当然也可以嵌套到另外一个Fragment中,但这个被嵌套的Fra ...

  3. Java设计模式之(一)------单例模式

    1.什么是单例模式? 采取一定的办法保证在整个软件系统中,单例模式确保对于某个类只能存在一个实例.有如下三个特点: ①.单例类只能有一个实例 ②.单例类必须自己创建自己的实例 ③.单例类必须提供外界获 ...

  4. window.location.hash 使用说明

    本文给大家详细汇总了关于window.location.hash的知识点,属性以及用法等等,非常的实用,并附上了例子,有需要的小伙伴可以参考下.   location是javascript里边管理地址 ...

  5. Ruby学习之深入类

    在讨论对象模型时,对类做了初步了解,关于类本身,还有许多知识需要学习. 类定义 Ruby中,可以用class关键字或者Class.new方法来定义一个类,在Ruby中,类定义的同时就是在运行代码,类和 ...

  6. Python装饰器的解包装(unwrap)

    在Python 3.4 中,新增一个方法unwrap,用于将被装饰的函数,逐层进行解包装. inspect.unwrap(func, *, stop=None) unwrap方法接受两个参数:func ...

  7. golang 数组反转

    我做hackerearth上题目记录,具体的题目描述是这样的: Given the size and the elements of array A, print all the elements i ...

  8. Java工程读取resources中资源文件路径问题

    正常在Java工程中读取某路径下的文件时,可以采用绝对路径和相对路径,绝对路径没什么好说的,相对路径,即相对于当前类的路径.在本地工程和服务器中读取文件的方式有所不同,以下图配置文件为例. 本地读取资 ...

  9. 解决myeclipse部署按钮不能点

    找到MyEclipse的工作路径(一般点开myeclipse是会显示),然后到这个目录中去“\.metadata\.plugins\org.eclipse.core.runtime\.settings ...

  10. windows 安全模型简介

    操作系统中有些资源是不能由用户代码直接访问的,比如线程进程,文件等等,这些资源必须由系统级代码由RING3层进入到RING0层操作,并且返回一些标识供用户程序使用,一般调用某个函数陷入到内核,这样的函 ...