欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2的拦截器无法跳转到jsp页面

程序员文章站 2022-05-28 23:45:34
...

今天我看尚硅谷的struts2视频,出现一个和老师不同的错误,百度了很多也解决不了

第一个代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="product-save.action" method="post">
		ProductName:<input type="text" name="productName"/>
		<br><br>
		ProductDesc:<input type="text" name="productDesc"/>
		<br><br>
		ProductPrice:<input type="text" name="productPrice"/>
		<br><br>
		<input type="text" value="submit"/>
		<br><br>	
	</form>	
</body>
</html>
第二个代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

</body>
</html>
第三个代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

	<a href="product-input.action">Product Input</a>
	
</body>
</html>
拦截器代码
package com.ydd.struts2.helloworld;

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;
import javax.servlet.http.HttpServletRequest;


public class FilterDispatcher implements Filter {

    public FilterDispatcher() {}

	public void destroy() {}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest req=(HttpServletRequest) request;
		//1.获取sevletPath
		String servletPath=req.getServletPath();
		System.out.println(servletPath);
		String path=null;
		
		//2.判断servletPath,若其等于"/product-input.action,则转发到
		//   /WEB-INF/pages/input.jsp
		if ("/product-input.action".equals(servletPath)) {
			path="/WEB-INF/pages/input.jsp";
		}
		//3.若其等于"/product-save.action,则转发到
		if (path!=null) {
			request.getRequestDispatcher(path).forward(request,response );
			return ;
		}
		chain.doFilter(request, response);
	}
	public void init(FilterConfig fConfig) throws ServletException {
		
	}

}

出现错误404