SpringMVC之重定向与转发和视图解析器与中文乱码问题

原始的servlet方式

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="jquery-3.3.1.js"></script>
<script type="text/javascript">

</script>
</head>
<body>
<form action="<%=basePath%>/test3.htm" method="post">
	<p>用户名:<input type="text" name="user"></p>
	<p>&nbsp;码:<input type="password" name="pwd"></p>
	<p>&nbsp;龄:<input type="text" name="nianling"></p>
	<p><input type="submit" value="提交"></p>
</form>
</body>
</html>
@RequestMapping(value = "/test3")
	public void test3(String user, String pwd, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		if ("admin".equals(user) && "123".equals(pwd)) {//登录成功
//			request.getRequestDispatcher("/pages/front/success.jsp").forward(request, response);
			String basePath = request.getContextPath();
			response.sendRedirect(basePath + "/pages/front/success.jsp");
		}else {//失败
			request.getRequestDispatcher("/index.jsp").forward(request, response);

		}
	}

在这里插入图片描述
成功
在这里插入图片描述
失败
在这里插入图片描述

springMVC中的重定向与转发

转发

在return后面直接输入跳转地址或者forward:+地址(建议多用)

@RequestMapping(value = "/test4")
	public String test4(String user, String pwd){
		if ("admin".equals(user) && "123".equals(pwd)) {//登录成功
			//spring的重定向
			return "redirect:/pages/front/success.jsp";
		}else {//失败
			//spring的转发,不写forward默认为转发
//			return "forward:/pages/front/fail.jsp";
			return "/pages/front/fail.jsp";
		}
	}

重定向

在return后面redirect:+地址

视图解析器

我们发现在转发和重定向时地址反复填写,特别麻烦,所以视图解析器发挥作用了!
在这里插入图片描述

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
			http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
			http://www.springframework.org/schema/mvc 
			http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
			http://www.springframework.org/schema/context 
			http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
	
	<!-- 配置包扫描 -->
	<context:component-scan base-package="cn.java.controller.*"></context:component-scan>
	
	<!-- 配置mvc特有的注解驱动 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- 视图解析器 -->
	<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置前缀 -->
		<property name="prefix" value="/pages/"></property>
		<!-- 配置后缀 -->
		<!-- <property name="suffix" value=".jsp"></property> -->
	</bean>
	
</beans>

其实我们可以将前缀和后缀写的更详细的,但是那样代码可读性会很差,

@RequestMapping(value = "/test4")
	public String test4(String user, String pwd){
		if ("admin".equals(user) && "123".equals(pwd)) {//登录成功
			//spring的重定向
			return "redirect:/pages/front/success.jsp";
		}else {//失败
			//spring的转发,不写forward默认为转发
//			return "forward:/pages/front/fail.jsp";
			return "/pages/front/fail.jsp";
		}
	}
	//视图解析器只对转发有效对重定向无效
	@RequestMapping(value = "/test5")
	public String test5(String user, String pwd){
		System.out.println(user+pwd);
		if ("admin".equals(user) && "123".equals(pwd)) {//登录成功
			return "front/success.jsp";
		}else {//失败
			return "front/fail.jsp";
		}
	}

并且视图解析器支队转发有效,这也是我建议使用转发的原因!

中文乱码问题

servlet中的解决办法

用filter过滤器来解决:

package cn.java.filters;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

/**
 * Servlet Filter implementation class EncodingFilter
 */

@WebFilter(filterName="EncodingFilter",urlPatterns="/*")
public class EncodingFilter implements Filter {


	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text.html;charset=utf-8");
		chain.doFilter(request, response);
	}


	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}

spring中的解决办法

过滤器一定要配置在servlet前面
在web.xml中配置filter

<!-- 配置过滤器 -->
  <filter>
  	<filter-name>encodingFilter</filter-name>
  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>utf-8</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>encodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值