java 拦截器 判断url_Spring 的拦截器如何实现url转发

2022-09-27 15:04:41 浏览数 (1)

如何实现Spring 的过滤器代码中改写请求的URL,并转发出去,注意,是转发,不是重定向

package com.blog.inteceptor;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;

import org.springframework.web.servlet.ModelAndView;

import com.blog.entity.User;

public class HandlerInteceptor implements HandlerInterceptor{

public void afterCompletion(HttpServletRequest arg0,

HttpServletResponse arg1, Object arg2, Exception arg3)

throws Exception {

System.out.println(“执行后”);

}

public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,

Object arg2, ModelAndView arg3) throws Exception {

System.out.println(“…..”);

}

public boolean preHandle(HttpServletRequest request, HttpServletResponse res,

Object arg2) throws Exception {

System.out.println(“拦截用户”);

String url = request.getScheme() ”://” request.getServerName()

“:” request.getServerPort()

request.getServletPath();

System.out.println(url);

//在这里使用forward实现转发,而不是使用redirect实现重定向

return true;

}

} 我需要在上述代码中的方法中将请求的URL换成另外一个url,然后转发出去。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/183756.html原文链接:https://javaforall.cn

0 人点赞