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?
Some of the best practices while developing Struts2 application are: 1. Always try to extend struts-default package while creating your package to avoid code redundancy in configuring interceptors. 2. For common tasks across the application, such as logging request params, try to use interceptors.
The struts application contains two main configuration files struts. xml file and struts. properties file.
Q 10 - Which of the following is true in the life cycle of a request in Struct2 application? A - Selected action is executed to perform the requested operation.
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:
<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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With