1

我想修改我的Spring应用程序中的每个HTML页面。这背后的原因是我想在每个页面上包含一段JavaScript代码。使用servlet将Javascript注入到每个HTML页面中Filter

不幸的是,Sitemesh无法帮助我(或至少我认为它不能),因为Ajax请求也必须受此JavaScript影响,并且我的Ajax请求明确排除在装饰之外。

所以我的想法是篡改HttpServletResponse在Spring中使用过滤器,但我坚持实际访问生成的内容并修改它。

回答

2

您访问作为doFilter doFilter(ServletRequest request, ServletResponse response, FilterChain chain)一部分的ServletResponse,然后向其中添加其他内容。

示例代码示例:http://myjavaswtech.blogspot.com/2012/05/modifying-response-using-filters.html

+1

仅供参考示例代码的链接不再有效(它指向的博客上消失)。这个其他问题确实有一些示例代码:http://stackoverflow.com/questions/14736328/looking-for-an-example-for-inserting-content-into-the-response-using-a-servlet-f – kenj0418 2016-01-29 16:35:47

相关问题