最佳实践?在 Struts2 中我应该把我自己的应用程序的配置参数放在哪里?

发布于 2024-12-02 11:14:57 字数 245 浏览 0 评论 0原文

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

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

人们通常在 Struts2 应用程序中这样做的方式是什么?有什么最佳实践吗?

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒废 2024-12-09 11:14:57

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

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

这是一个简短的示例:

web.xml

<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;
  }
}

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.

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

Here's a brief example:

web.xml

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

Your Action

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;
  }
}
爱你不解释 2024-12-09 11:14:57

请参阅此处:Apache Struts 2 文档 - 处理文件上传
或:Apache Struts 2 文档 - 文件上传

可以通过将 struts.properties 文件放入 WEB-INF/classes 来设置属性。属性文件中找到的任何属性都将覆盖默认值。

  • struts.multipart.parser - 该属性应设置为扩展 MultiPartRequest 的类。目前,该框架附带了 Jakarta FileUpload 实现。
  • struts.multipart.saveDir - 将放置上传文件的目录。如果未设置此属性,则默认为 javax.servlet.context.tempdir。
  • struts.multipart.maxSize - 允许上传的最大文件大小(以字节为单位)。这有助于防止有人上传大量大文件而滥用系统。默认值为 2 MB,最高可设置为 2 GB(如果您想编辑 Pell 多部分源,则可以设置更高,但如果您需要上传大于 2 GB 的文件,则确实需要重新考虑!) maxSize 适用于表单上的一个文件的总大小,而不是单个文件的大小。

如果您对默认值感到满意,则无需将任何属性放入 struts.prop

我通常将所有这些设置放入默认包中的 struts.properties 文件中。如果您使用此类配置,也可以在 struts.xml 文件中设置它们。

Google 搜索会显示大量使用“Struts2 文件上传”作为搜索参数的 struts 2 文件处理示例。

See here: Apache Struts 2 Documentation - Handling File Uploads
or : Apache Struts 2 Documentation - File Upload

Properties can be set by putting a struts.properties file in WEB-INF/classes. Any property found in the properties file will override the default value.

  • struts.multipart.parser - This property should be set to a class that extends MultiPartRequest. Currently, the framework ships with the Jakarta FileUpload implementation.
  • struts.multipart.saveDir - The directory where the uploaded files will be placed. If this property is not set it defaults to javax.servlet.context.tempdir.
  • struts.multipart.maxSize - The maximum file size in bytes to allow for upload. This helps prevent system abuse by someone uploading lots of large files. The default value is 2 Megabytes and can be set as high as 2 Gigabytes (higher if you want to edit the Pell multipart source but you really need to rethink things if you need to upload files larger then 2 Gigabytes!) If you are uploading more than one file on a form the maxSize applies to the combined total, not the individual file sizes.

If you're happy with the defaults, there is no need to put any of the properties in struts.prop

I typically put all these settings in my struts.properties file located in the default package. They can also be set in the struts.xml file if you use this type of configuration.

A Google search turns up a plethora of file handling examples for struts 2 using "Struts2 file upload" as your search parameters.

靖瑶 2024-12-09 11:14:57

我使用在实现 javax.servlet.ServletContextListener 类的类中加载的配置 xml 文档。从那里我将属性设置为 servletContext:

public void contextInitialized(ServletContextEvent contextEvent) {
    try{

        Document xmlDocument = readConfigFile(contextEvent.getServletContext().getRealPath("") + fileSeperator + AppConfigConstants.XML_CONFIG_LOCATION);

        contextEvent.getServletContext().setAttribute(AppConfigConstants.RECORDS_PAGE_NODE_NAME,this.getValueFromConfig(AppConfigConstants.RECORDS_PAGE_NODE_NAME,xmlDocument));

...

}

然后在我的 struts 基本操作类中,我有从 servlet 上下文获取属性的方法。

protected Integer getRecordsPage(){
    Integer recordsPage = Integer.valueOf("0");

    if(this.getServlet().getServletContext().getAttribute(AppConfigConstants.RECORDS_PAGE_NODE_NAME)!= null){
        recordsPage = Integer.valueOf(this.getServlet().getServletContext().getAttribute(AppConfigConstants.RECORDS_PAGE_NODE_NAME).toString());
    }

    return recordsPage;

}

I use a config xml document that I load in a class that implements javax.servlet.ServletContextListener class. From there I set attributes to the servletContext:

public void contextInitialized(ServletContextEvent contextEvent) {
    try{

        Document xmlDocument = readConfigFile(contextEvent.getServletContext().getRealPath("") + fileSeperator + AppConfigConstants.XML_CONFIG_LOCATION);

        contextEvent.getServletContext().setAttribute(AppConfigConstants.RECORDS_PAGE_NODE_NAME,this.getValueFromConfig(AppConfigConstants.RECORDS_PAGE_NODE_NAME,xmlDocument));

...

}

Then in my struts base action class I have methods that get the properties from the servlet context.

protected Integer getRecordsPage(){
    Integer recordsPage = Integer.valueOf("0");

    if(this.getServlet().getServletContext().getAttribute(AppConfigConstants.RECORDS_PAGE_NODE_NAME)!= null){
        recordsPage = Integer.valueOf(this.getServlet().getServletContext().getAttribute(AppConfigConstants.RECORDS_PAGE_NODE_NAME).toString());
    }

    return recordsPage;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文