简体   繁体   English

Struts Action class 中的不可序列化属性

[英]Non-serializable attributes in Struts Action class

I am working on refactoring our code according to Sonar and Kiuwan analyzers recommendations.我正在根据 Sonar 和 Kiuwan 分析器的建议重构我们的代码。 Our architecture is standard MVC Java Web App based on Struts 2.x framework deployed on Apache Tomcat 9.x. Our architecture is standard MVC Java Web App based on Struts 2.x framework deployed on Apache Tomcat 9.x. App itself is basically an information system that allows users to browse pdf documents and perform actions on them. App 本身基本上是一个信息系统,允许用户浏览 pdf 文档并对其执行操作。

I have run into a design flaw which I am not sure how to solve or if it even is an problem all togther.我遇到了一个设计缺陷,我不知道如何解决,或者它甚至是一个问题。

During http request processing we set set up Hibernate's Entity Manager instance in Interceptor class before Struts Actions are called.在 http 请求处理期间,我们在调用 Struts 操作之前在拦截器 class 中设置了 Hibernate 的实体管理器实例。 We are using this EM instance for the whole lifecycle of the Struts action.我们将这个 EM 实例用于 Struts 操作的整个生命周期。 When the action finishes the interceptor closes the EM instance.当动作完成时,拦截器关闭 EM 实例。 The EM instance is saved into instance of action class as an class attribute. EM 实例作为 class 属性保存到动作实例 class 中。 Snippet below.片段如下。

class BaseAction extends ActionSupport implements EntityManagerAware { //EntityManagerAware my interface that allows interceptor to inject em instance into underlying Action class
    private EntityManager em;
    private String someParam;

    @Action(value = "welcome", results = {@Result(name = SUCCESS, type = "tiles", location = "welcome")})
    public String input() {
        em.doStuffWithEntities();
        log.trace("Some param": + someParam);
        return SUCCESS;
    }

...
}

I have always thought this is solution is OK, but now we introduced Sonar.我一直认为这个解决方案是可以的,但现在我们介绍了声纳。 Sonar marks em attribute as - Make "em" transient or serializable. Sonar 将 em 属性标记为 - 使“em”瞬态或可序列化。 I understand that Sonar thinks it is an attribute that might be passed during HttpRequests, line someParam attribute in the snippet above.我了解 Sonar 认为它是可能在 HttpRequests 期间传递的属性,在上面的代码片段中行 someParam 属性。

My question is do Struts action attributes need to be Serializable (even the ones like "em")?我的问题是 Struts 动作属性是否需要可序列化(即使是像“em”这样的属性)? Can I somehow solve this with perhaps Struts annotation or simply with transient flag?我可以通过 Struts 注释或仅使用瞬态标志以某种方式解决这个问题吗? When does serialization of Action class instance occur? Action class 实例的序列化何时发生? Can it occur?它会发生吗?

Basically can somebody explain this in greater detail (I can always just mark this Sonar issue as false positive, but I really do not know how this works)?基本上有人可以更详细地解释这一点(我总是可以将此声纳问题标记为误报,但我真的不知道这是如何工作的)? I have googled and googled but came up empty handed.我用谷歌搜索和谷歌搜索,但空手而归。

Thank you very much,非常感谢,

Jiri吉里

EntityManager should be a persistent dependency , for example: EntityManager 应该是一个持久依赖,例如:

 @PersistenceUnit(unitName = "QueryPersistenceUnit") private EntityManagerFactory entityManagerFactory; public EntityManager getEntityManager() { return entityManagerFactory.createEntityManager(); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM