简体   繁体   English

如何在 Struts 2 中向我的所有视图公开一个对象?

[英]How can I expose an object to all of my views in Struts 2?

I have a web application using Struts 2 with Freemarker templates, and Spring 4.我有一个使用带有 Freemarker 模板的 Struts 2 和 Spring 4 的 Web 应用程序。

I have a few configuration strings that are stored in a .properties file which I need to render on every page (for example, our CDN path, which contains a version string).我有一些配置字符串存储在.properties文件中,我需要在每个页面上呈现这些文件(例如,我们的 CDN 路径,其中包含一个版本字符串)。 Right now these properties are read by Spring and stored in an instance of org.springframework.core.env.Environment .现在这些属性被 Spring 读取并存储在org.springframework.core.env.Environment一个实例中。

Is there an easy way to make my Environment instance accessible to all of my views?有没有一种简单的方法可以让我的所有视图都可以访问我的Environment实例?

We have another application which does this through action inheritance, where a base class has a getEnvironment() method.我们有另一个应用程序通过操作继承来完成此操作,其中基类具有getEnvironment()方法。 I dislike this approach as every action must extend the base class.我不喜欢这种方法,因为每个操作都必须扩展基类。

Put your logic in a bean (eg. Configuration.java) that is then injected (through Spring DI, Java EE CDI, or whatever) in all the actions needing it, and exposed through a Getter.将您的逻辑放在一个 bean(例如 Configuration.java)中,然后在所有需要它的操作中注入(通过 Spring DI、Java EE CDI 或其他任何方式),并通过 Getter 公开。

But I would not underestimate the inheritance approach here , it's not as bad as you're describing it, 'cause you can build your inheritance tree adding specification while traversing it: one BaseAction, some Sub-BaseActions, some sub-sub... etc. Read more .但我不会低估这里的继承方法,它并不像你描述的那么糟糕,因为你可以在遍历它的同时构建你的继承树添加规范:一个 BaseAction,一些 Sub-BaseActions,一些 sub-sub...等阅读更多

If you discover later that something you've put on a BaseAction is needed by sibling BaseActions, just move it one level up (in a parent BaseAction, up to the first one, common to everyone).如果您稍后发现兄弟 BaseAction 需要您放置在 BaseAction 上的某些东西,只需将其向上移动一级(在父 BaseAction 中,向上移动到第一个,对每个人都通用)。

Write an interceptor that will intercept every action and add this interceptor to the custom interceptor stack.编写一个拦截器来拦截每一个动作并将这个拦截器添加到自定义拦截器堆栈中。 This stack you should make a default interceptor stack.这个堆栈你应该做一个默认的拦截器堆栈。

<interceptors>
  <interceptor name="myinterceptor" class="com.company.interceptor.MyInterceptor"/>
  <interceptor-stack name="customStack">
    <interceptor-ref name="myinterceptor"/>
    <interceptor-ref name="defaultStack"/>
  </interceptor-stack>
</interceptors>
<default-interceptor-ref name="customStack"/>

In the implementation of myinterceptor get bean from the application context and put it on the value stack.myinterceptor的实现中,从应用程序上下文中获取 bean 并将其放在值堆栈中。 In every JSP you can access a value stack's object using OGNL.在每个 JSP 中,您都可以使用 OGNL 访问值堆栈的对象。

More about how to write a custom interceptor you can find in docs Writing Interceptors .有关如何编写自定义拦截器的更多信息,您可以在文档编写拦截器中找到。

Well, I think the inheritance solution fits well with your problem, all your controllers need these properties, so it is the best and most logical approach.好吧,我认为继承解决方案非常适合您的问题,您的所有控制器都需要这些属性,因此这是最好和最合乎逻辑的方法。

You could try the interceptor solution like Roman just told you, but I think interceptors are a little tricky to configure and the Struts2 error logs doesn't really help when something goes bad inside interceptors and you have a big application, moreover you would loose performance processing logic at the beggining of every action instead of using it with inheritance.你可以尝试像 Roman 刚刚告诉你的拦截器解决方案,但我认为拦截器配置起来有点棘手,当拦截器内部出现问题并且你有一个很大的应用程序时,Struts2 错误日志并没有真正的帮助,而且你会失去性能在每个动作开始时处理逻辑,而不是将其与继承一起使用。

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

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