最佳实践?在 Struts2 中,我在哪里放置我自己的应用程序的配置参数? [英] Best Practice? Where do I put configuration parameters for my own application in Struts2?

查看:25
本文介绍了最佳实践?在 Struts2 中,我在哪里放置我自己的应用程序的配置参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java servlet 中,有 .在桌面应用中,我们通常会定义自己的配置文件.

In Java servlet, there is <context-param>. In desktop applications, we usually define our own configuration file.

我应该将 Struts2 应用程序的配置参数放在哪里?例如,我的应用程序需要为用户输入设置时间限制,或者保存和读取存储在某处的文件,或者用户可以输入错误密码的最长时间等等.我希望这些东西是可配置的.

Where should I put configuration parameters for my Struts2 application? For example, my application needs to set a time limit for user input, or save and read files stored somewhere, or the maximum time the user can type a wrong password, etc. I want those things configurable.

人们通常在 Struts2 应用程序中的做法是什么?有什么最佳做法吗?

What's the way people usually do it in Struts2 applications? Any best practice?

推荐答案

如果您熟悉您提到的 ServletContext 方法,则可以坚持使用.在您的 web.xml 中,只需添加您的 s.

If you are familiar with the ServletContext approach that you mentioned, you can stick with that. In your web.xml, just add your <context-param>s.

然后,要在您的操作中获取 ServletContext,只需实现 ServletContextAware 它将自动为您注入.

Then, to get the ServletContext in your actions, just implement ServletContextAware and it will be automatically injected for you.

这是一个简单的例子:

<context-param>
  <param-name>someSetting</param-name>
  <param-value>someValue</param-value>
</context-param>

您的操作

public class YourAction extends ActionSupport implements ServletContextAware {
  private ServletContext servletContext;

  @Override
  public String execute() throws Exception {
    String someValue = (String) servletContext.getAttribute("someSetting");

    return SUCCESS;
  }

  @Override
  public void setServletContext(final ServletContext context) {
    this.servletContext = servletContext;
  }
}

这篇关于最佳实践?在 Struts2 中,我在哪里放置我自己的应用程序的配置参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆