5

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?

Steven Benitez
  • 10,936
  • 3
  • 39
  • 50
Haozhun
  • 6,331
  • 3
  • 29
  • 50

3 Answers3

5

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;
  }
}
Steven Benitez
  • 10,936
  • 3
  • 39
  • 50
0

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.

Russell Shingleton
  • 3,176
  • 1
  • 21
  • 29
  • Did you read my question? I'm not asking about file handling! – Haozhun Sep 03 '11 at 03:09
  • "For example, my application needs to save and read files stored somewhere" - This leads me to believe you are talking about file handling and where to configure these settings. Regardless, settings like this are typically handled in the struts.properties file or in the struts.xml file, beyond that you configure the application like any other using web.xml or programatically. – Russell Shingleton Sep 03 '11 at 03:16
  • I'm sorry. That would be my fault. Can you answer my question again if you understand it now? Thank you. – Haozhun Sep 03 '11 at 03:19
  • There are many struts2 settings that can be overridden or setup in the struts.properties. I generally use this: http://struts.apache.org/2.0.11/docs/strutsproperties.html as a basis for my projects and comment out what I don't need. What's not provided through the standard API, I typically write my own stuff to handle the situation. – Russell Shingleton Sep 03 '11 at 03:23
  • I do not mean to override struts2 settings. I mean where should I save my own settings for my own action's use. What's the way people usually do it? – Haozhun Sep 03 '11 at 03:31
  • It application and programmer dependent. You can define a custom resource bundles on a class by class basis or as a global properties file which can be set in the struts.properties such as "struts.custom.i18n.resources=global-messages" (http://struts.apache.org/2.0.14/docs/how-do-i-set-a-global-resource-bundle.html). I generally build more dynamic applications through defining settings in a database table. I'm not sure there's any hard set best practice for this. Am I getting closer to what you're looking for? – Russell Shingleton Sep 03 '11 at 03:32
  • Yes, this is what I'm looking for! But isn't i18n resources for i18n uses? Database is ok, but kind of overkill. – Haozhun Sep 03 '11 at 03:40
  • You could just use straight java to load the properties file you know, such as in this post: http://stackoverflow.com/questions/333363/loading-properties-file-from-java-package – Russell Shingleton Sep 03 '11 at 03:49
0

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;

}
bittersweetryan
  • 3,383
  • 5
  • 28
  • 42