SpringMVC 跳转结果的方式.练习笔记

HelloController.java

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.servlet.ModelAndView;

@Controller
public class HelloController{
	/**
	 * 1.设置ModelAandView对象。根据View的名称,和视图解析器跳转带指定的页面
	 * 页面:视图解析器的前缀+view name +视图解析器的后缀
	 */
	@RequestMapping("hello")
	public ModelAndView handleRequest(HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		ModelAndView mav=new ModelAndView();
		//封装要显示到视图的数据
		mav.addObject("msg", "hello springmvc controller");
		//视图名
		mav.setViewName("hello");
		return mav;
	}
	/**
	 * 2.通过servletAPI对象来实现。不需要视图解析器的配置
	 * 	 1)通过HttpServletResponse来进行输出。
	 *	 2)通过HttpServletResponse实现重定向。
	 *	 3) 通过HttpServletResponse实现转发。
	 */
	@RequestMapping("/hello1")
	public void hello1(HttpServletRequest req,
			HttpServletResponse resp) throws Exception{
			//1)通过HttpServletResponse来进行输出。
			//resp.getWriter().println("hello spring mvc use httpservlet api!");
			//2)通过HttpServletResponse实现重定向。
			//resp.sendRedirect("index.jsp");
			//3)通过HttpServletResponse实现转发
			req.setAttribute("msg", "servlet api forward");
			req.getRequestDispatcher("index.jsp").forward(req, resp);
	}
	/**
	 * 通过Spring mvc 来实现转发和重定向 --- 没有试图解析器
	 * @return
	 */
	@RequestMapping("/hello2")
	public String hello2(){
		//转发1
		//return "index.jsp";
		//转发2
		System.out.println("转发2");
		return "forward:index.jsp";
		//重定向
		//System.out.println("实现重定向");
		//return "redirect:index.jsp";
	}
	
	/**
	 * 通过Spring mvc 来实现转发和重定向 --- 有试图解析器
	 * @return
	 */
	@RequestMapping("/hello3")
	public String hello3(){
		//转发1
		//return "hello";
		//转发2
		return "forward:hello.do";
		//重定向  不需要视图解析器
		//return "redirect:hello.do";
	}
	
}


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 指明配置文件的位置 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:mvc.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
     <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>
web-inf/jsp/hello.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>hello</title>
  </head> 
  <body>
    ${msg } <br/>
    web-inf/hello.jsp  
  </body>
</html>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值