<%@ 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+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
<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>密 码:<input type="password" name="pwd">p>
<p>年 龄:<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);
}
}
在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:+地址
我们发现在转发和重定向时地址反复填写,特别麻烦,所以视图解析器发挥作用了!
<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:annotation-driven>mvc:annotation-driven>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/">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";
}
}
并且视图解析器支队转发有效,这也是我建议使用转发的原因!
用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
}
}
过滤器一定要配置在servlet前面
在web.xml中配置filter
<filter>
<filter-name>encodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>