SpringMVC__配置视图解析以及服务端重定向和服务端跳转简单代码

  • 1、不响应 使用void类型以及加上@ResponseBody注解
  • 2、ModelAndView‘ 使用.setViewName("/index.jsp");指定跳转页面名称
  • 3、直接指向页面名称 返回值类型为String,返回值直接指向要跳转的页面名称
  • 4、客户端重定向 使用redirect:地址,进行页面重定向
  • 5、使用原生的HttpServletRequest、HttpServletResponse 在controller定义这两个变量,具体根据需求去使用

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
	<!-- 开启扫描 -->
	<context:component-scan base-package="com.controller"/>
	
	<!-- 开启SpringMVC注解的方式 -->
	<mvc:annotation-driven/>
	
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 设置前后缀 -->
		<property name="prefix" value="/jsp/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
</beans>

Controller

package com.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
/*@RequestMapping("/con") //这里也可以配置访问这整一个Controller路径*/
public class MyController{
	
	/**
	 * 服务器端跳转/请求转发
	 * url没有变化,进入视图解析器
	 * @return
	 */
	@RequestMapping("/indexA")
	public String indexA() {
		// TODO Auto-generated method stub
		return "index";//默认为"forward:/路径"可以不加forward
	}
	
	/**
	 * 客户端重定向/页面重定向
	 * 这里使用redirect:路径
	 * 必须是绝对路径,进入视图解析器
	 * @return
	 */
	@RequestMapping("/indexB")
	public String indexB() {
		// TODO Auto-generated method stub
		return "redirect:/jsp/index.jsp";
	}
	
	/**
	 * 服务器给客户端直接响应一个字符串信息
	 * @return
	 */
	@RequestMapping("/indexC")
	@ResponseBody//加上不进入视图解析,直接显示在页面
	public String indexC() {
		// TODO Auto-generated method stub
		return "这是在页面显示哦";
	}
	
	/**
	 * 使用HttpServletRequest、HttpServletResponse
	 * 进行服务端跳转
	 * 这里是不进入视图解析器的
	 * 路径需要使用绝对路径
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/indexD")
	public void indexD(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.getRequestDispatcher("jsp/index.jsp").forward(req, resp);
	}
	
	/**
	 * 使用HttpServletRequest、HttpServletResponse
	 * 进行客户端重定向
	 * 这里是不进入视图解析器的
	 * 路径需要使用绝对路径
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/indexE")
	public void indexE(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		resp.sendRedirect("jsp/index.jsp");
	}
	
	/**
	 * 定义请求方式
	 * 采用resful风格的POST
	 * 进行服务端跳转
	 * @return
	 */
	@RequestMapping(value="/indexH",method=RequestMethod.POST)
	public String indexH() {
		// TODO Auto-generated method stub
		System.out.println("进入了indexH()");
		return "index";
	}

	
	
	/**
	 * 定义请求方式
	 * 采用resful风格的GET
	 * 进行服务端跳转
	 * @return
	 */
	@RequestMapping(value="/indexF",method=RequestMethod.GET)
	public String indexF() {
		// TODO Auto-generated method stub
		System.out.println("进入了indeF()");
		return "index";
	}
	
	
	
	/**
	 * 使用@GetMapping相当于@RequestMapping(value="/indexF",method=RequestMethod.GET)
	 * 设置请求方式为GET
	 * @return
	 */
	@GetMapping("/indexG")
	public String indexG() {
		// TODO Auto-generated method stub
		System.out.println("进入了indexG()");
		return "index";
	}
	
	/**
	 * 使用@PostMapping相当于@RequestMapping(value="/indexF",method=RequestMethod.POST)
	 * 设置请求方式为POST
	 * @return
	 */
	@PostMapping("/indexI")
	public String indexI() {
		// TODO Auto-generated method stub
		System.out.println("进入了indexI()");
		return "index";
	}
	
	/**
	 * 使用ModelAndView
	 * 返回一个ModelAndView对象
	 * 
	 * @return
	 */
	@RequestMapping("/sel")
	public ModelAndView selIndex() {
		// TODO Auto-generated method stub
		System.out.println("----返回ModelAndView----");
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/index.jsp");
		return mav;
	}

	
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SpringDemo08</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
	  <servlet-name>spring-mvc</servlet-name>
	  <!-- 中央处理器or前端控制器 -->
	  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	  <init-param>
  		<param-name>contextConfigLocation</param-name>
  		<!-- 配置文件 -->
  		<param-value>classpath:applicationContext.xml</param-value>
  	   </init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>spring-mvc</servlet-name>
  	<!-- 映射路径 -->
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偷偷学习被我发现

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值