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

在学习尚硅谷的Struts2教程时,遇到一个与教程不同且百度未找到解决方案的错误:使用拦截器尝试跳转到JSP页面时返回404状态。这个问题困扰了博主,对Struts2的拦截器功能产生了疑问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天我看尚硅谷的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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值