gpt4 book ai didi

java - 为什么使用 ServletRequestAware 而不是 ServletActionContext?

转载 作者:行者123 更新时间:2023-11-29 03:15:52 27 4
gpt4 key购买 nike

要在 Struts 2 中获取 servlet 请求,我们可以使用 ServletRequestAwareServletActionContext。但是,在特定的互联网中 resource ,它被告知应该使用 ServletRequestAware 而不是 ServletActionContext

这是否与 ServletActionContext 将成为多线程环境中的共享资源这一事实有关,或者这背后是否有任何其他原因?

最佳答案

ServletRequestAware 是一种更好的方法,因为它将操作方法​​与静态访问器分离。

为了使用ServletActionContext 测试某些东西,您需要模拟静态方法 模拟请求。要测试 ServletRequestAware 方法,您只需模拟请求即可。

虽然存在使模拟静态方法更容易的工具,但实现接口(interface)和传递模拟请求更容易。这就是 ServletRequestAware 存在的原因。

关于java - 为什么使用 ServletRequestAware 而不是 ServletActionContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26699019/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com