如何在 Struts 2.2.1 中获取 Servlet Request 属性?
我正在阅读一些教程,其中在调用任何操作之前,有一个过滤器将 ServletRequest 中的属性设置为 Connection。
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
Connection connection = new ConnectionFactory().getConnection();
request.setAttribute("connection", connection);
chain.doFilter(request, response);
connection.close();
}
但是我仍然没有找到在我的操作中获取该属性的方法。我怎样才能得到它?
I'm reading some tutorial where before invocating any action there is a filter that sets an attribute in the ServletRequest as Connection.
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
Connection connection = new ConnectionFactory().getConnection();
request.setAttribute("connection", connection);
chain.doFilter(request, response);
connection.close();
}
However I still didn't find a way to get the attribute in my Action. How can I get it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
另一种选择是您的操作类实现
ServletRequestAware
。在该方法的实现中,您只需将请求分配给实例字段即可。Another option is that your action class implements
ServletRequestAware
. In the implementation of the method you simply assign the request to an instance field.