您认为使用 servletFilter
增加页面访问次数是个好主意吗?
像这样的东西:
@WebFilter("/posts/*")
public class PostHitCounterFilter implements Filter {
@Autowired
private PostService postService; // somehow I autowire this
@Override
public void init(FilterConfig filterConfig) {//...}
@Override
public void doFilter(ServletRequest request, ServletResponse response, filterChain chain) {
String postUrl = ((HttpServletRequest) request).getRequestURI().substring(7);
postService.incrementVisits(postUrl);
chain.doFilter(request, response);
}
@Override
public void destroy() {//...}
}