开发者

Best Practice? Where do I put configuration parameters for my own application in Struts2?

开发者 https://www.devze.com 2023-04-01 21:06 出处:网络
In Java servlet, there is <context-param>. In desktop applications, we usually define our o开发者_JAVA百科wn configuration file.

In Java servlet, there is <context-param>. In desktop applications, we usually define our o开发者_JAVA百科wn 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?


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


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.


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;

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号