I have confusion about these files in struts2.
Normally struts.xml file the core which has configuration in struts1. So I also thought of using struts.xml file.
But in my project already they put struts2-config-browser-plugin.jar, it has struts-plugin.xml. Just searched in Google and found that struts-plugin.xml is enough to run struts application.
Now I'm adding the struts.xml file, the application is not working.
Can I use both XML file in application?
If I remove the struts2-config-browser-plugin.jar , what are the changes need to add in struts.xml file ?
There are three configuration files which are by default loaded by the framework (if they exist):
struts-default.xml - included in struts2-core.jar, contains all basic configuration of the framework
struts-plugin.xml - if plugin want to override some defaults or define its own settings (results, actions, etc)
struts.xml - contains user defined configuration, mostly actions, results and custom stacks of interceptors
You should just use struts.xml and put all the configurations there.
Some notes about struts-plugin.xml - http://struts.apache.org/development/2.x/docs/plugins.html
Related
My environment : JBoss EAP 7 with a "log4j2.xml" in classpath (historic behavior).
I would like to introduce a way to have a (non mandatory) custom log4j2 configuration file (per EAR application) but still use (fall back) to (existing) "log4j2.xml" if the custom configuration file is missing.
To me, the only way to accomplish this was to use composite configuration by using "log4j2.configurationFile" property (within log4j2.component.properties) and set both the "log4j2.xml" and the custom configuration filename (separated by a comma).
But if the custom file is missing, even the generic "log4j2.xml" is ignored.
When looking at log4j2 (v2.12.1) code (https://github.com/apache/logging-log4j2/blob/log4j-2.12.1/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java#L380) I can see that indeed if one config file is missing, none config file (of the list) is used (-> "return null")
Is there a way to accomplish the behavior I want?
Thanks
I currently have a package on my class path called MyResources with multiple property files that struts uses. Works great:
<constant name="struts.custom.i18n.resources" value="com.company.MyResources"/>
I am trying to move the properties files to a file location, so they can be updated without having to rebuild the package. Is it possible in Struts 2 to refer to this file location?
For example, my new file location with the properties files is:
/g01/properties/
And I would like Struts to use that location for the resource.
Yes, by providing an implementation of ResourceBundleTextProvider and initializing it in your struts.xml configuration file.
The default implementation, com.opensymphony.xwork2.TextProviderSupport defers the text lookup to com.opensymphony.xwork2.util.LocalizedTextUtil.
There are a number of ways to go about this, but if you don't need any of the default S2 behavior, here's the place to start:
<bean type="com.opensymphony.xwork2.TextProvider" name="struts"
class="com.opensymphony.xwork2.TextProviderSupport" scope="default" />
Provide your own ResourceBundleTextProvider implementation that uses whatever configuration management you want, for example, we implemented a DB-backed version (with caching, of course) that allowed translations to live in, and be managed by, a normal DB and I18N front end.
I'll see if I can dig up my original work this weekend and provide a link to a stripped-down solution.
The location of the file cannot be off the class path when you run your app. You should determine which classloader is used to load the resource. Then you should find a way to configure this classloader to be able to use the location as resource. So, it's possible. A short answer.
Im running my sruts 2 (2.1.8.1) applications in a jboss AS (5.1.0-GA). Im placing a.war and b.war in the same /server/default/deploy path and im placing the struts 2 libs in the /server/default/lib path.
There is no error message in the app or the server, but when i type http://localhost:8080/b/ for some reason im getting in the b.jsp page, the messages in the message resources of the application a.
So, my question is:
placing the struts jars in the /lib of the server creates only one instance of the value stack that all the applications have to use the same? If so, what can i do to have different instances for every aplication but keeping the jars in the server paht?
Should i take back the struts jars into the war?
Thanks
im using the struts tag to get the system title
<s:property value="%{getText('system.title')}"/>
this is my configuration for a.war:
/WEB-INF/classes/a-message-resources.properties
system.title=Namefor system A
in the struts.xml i have
<constant name="struts.custom.i18n.resources" value="a-message-resources" />
This is the configuration for b.war
/WEB-INF/classes/b-message-resources.properties
system.title=Namefor system B
in the struts.xml i have
<constant name="struts.custom.i18n.resources" value="b-message-resources" />
I think I might know what is going on here.
The struts.custom.i18n.resources value is tokenized and each token is added to the LocalizedTextUtil.DEFAULT_RESOURCE_BUNDLES. This is a static final list of strings. Before adding an item to the list (such as system.title), list.remove() is called on the same token, removing any prior entries for system.title.
Because this field is static, and because it allows only one entry for each message, whichever of your system.title properties is loaded first is then being overwritten by the next one.
As well, it appears that your JBoss instance is loading this class in a manner that is causing the static variables to be shared. You may be able to configure your JBoss to load this class separately for each app. This might be a good place start.
I am using JSR 303 Bean validation in my JSF 2.0 web application and it works fine with annotations. Now I would like to ignore annotations and configure validation rules using the validation.xml file, so this is what I did (I am using an eclipse dynamic web project) :
Added validation.xml under WebContent/META-INF/validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<validation-config
xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd"
>
<constraint-mapping>META-INF/validation/constraint-mapping.xml</constraint-mapping>
</validation-config>
Then created the file constraint-mapping.xml under WebContent/META-INF/validation/constraint-mapping.xml
<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd"
xmlns="http://jboss.org/xml/ns/javax/validation/mapping">
<bean class="my.full.path.ValidationMB" ignore-annotations="true">
</bean>
</constraint-mappings>
Having these configurations in place, I suppose the annotations in my bean class ValidationMB shall be ignored, BUT this is not happening!, which makes me assume that the validation.xml file is not being loaded.
any ideas? thanks.
Environment:
Apache Tomcat 7.0.23
javax.faces-2.1.4.jar
hibernate-validator-4.2.0.Final.jar
hibernate-validator-annotation-processor-4.2.0.Final.jar
validation-api-1.0.0.GA.jar
slf4j-api-1.6.1.jar
From the spec: section 4.4.6. XML Configuration: META-INF/validation.xml
Unless explicitly ignored by calling
Configuration.ignoreXMLConfiguration(), a Configuration takes into
account the configuration available in META-INF/validation.xml. This
configuration file is optional but can be used by applications to
refine some of the Bean Validation behavior. If more than one
META-INF/validation.xml file is found in the classpath, a
ValidationException is raised.
To solve my problem I had to create a META-INF folder under the project src folder, which ends in the WEB-INF/classes/META-INF.
The structure of the web application is:
ROOT
|_META-INF -- don't put validation.xml here
|_WEB-INF
|__ classes
|_META-INF
|__validation.xml
But I think that if I pack my web application in a jar file and reuse it in another project It may not work, I will let you know later once I do it.
Try to put your validation.xml directly into the WEB-INF/ directory.
I stumbled across this while looking for something else but wanted to clarify to the OP what is happening. You do in fact need the file to exist at META-INF/validation.xml; however, that is relative to the classpath which is why it worked when you put it under WEB-INF/classes/META-INF/validation.xml.
The cleaner approach is to let the file be put there for you. Your Eclipse project should already be outputting whatever is in your source directory to WEB-INF/classes somehow for you or nothing would be running. But sometimes there are filters on what it outputs so it might excluding something. You might want to check your src dirs and make sure they don't have exclusions.
Just as an example, if you had a Maven war project, all of your java sources would go in src/main/java and the generated classes would end up in the WEB-INF/classes directory. The equivalent happens for src/main/resources which contains non-source files. When I want *.xml, *.properties, etc. to end up in WEB-INF/classes I put them in src/main/resources. For your example I would have a src/main/resources/META-INF/validation.xml file.
Hope this helps anyone else who comes across this and is confused.
With Struts2 we have to have struts.xml in the class path, so it no longer works to have it under WEB-INF. So the way I got my project to deploy was to stick it under WEB-INF/classes and have it include ../struts2.xml
2 Problems:
Eclipse cleans out the classes folder when I do a rebuild, so it
deletes struts.xml
Eclipse doesn't show the classes folder in my project browser, so
its a poor place to stick config files in the first place.
How are you Struts2 Eclipse developers doing this?
You can either just put the struts.xml at the root of your source directory or set up an additional resources source directory and put it there. Eclipse quite happily copies it to WEB-INF/classes for you when it does a compilation.
I am late to the party, we can configure the struts.xml in any directory in the classpath of the web application, but provide the location using the "config" init parameter of the filter configuration in web.xml as below, if my struts.xml file is in "/com/resources/" directory.
<filter>
<filter-name>action</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,/com/resources/struts.xml</param-value>
</init-param>
</filter>
If we don't provide a config init parameter struts2 by default takes 3 values "struts-default.xml,struts-plugin.xml,struts.xml", you can see the struts2 Dispatcher class code below which will configure these 3 files to the configuration manager.
String configPaths = (String)this.initParams.get("config");
if (configPaths == null) {
configPaths = "struts-default.xml,struts-plugin.xml,struts.xml";
}
String[] files = configPaths.split("\\s*[,]\\s*");
for (String file : files)
if (file.endsWith(".xml")) {
if ("xwork.xml".equals(file))
this.configurationManager.addContainerProvider(createXmlConfigurationProvider(file, false));
else
this.configurationManager.addContainerProvider(createStrutsXmlConfigurationProvider(file, false, this.servletContext));
}
With Struts 1.2, it was required to put the struts-config.xml in the classpath (under WEB-INF folder) but with Struts 2.0, it is required to be in src/main/resources folder.
See the documentation Struts 2 Documentation here
I pasted struts.xml in this directory and the project executed fine.
I am not using Eclipse so this answer is not specific to your requirements but, I use Maven so we have all the "resources" that are needed by the application in a seperate folder called "resources" and When the application is built these files are copied into the appropriate places automatically. In Netbeans the files in the folder are available and I know that there are persons using eclipse with a similar setup.
I should point out that our project started from appfuse so most of these configurations were pre made. You can look at how it was done there.
In struts 2 projects, struts.xml file is added in src(Java Resources) folder along with the packages and libraries.
Please refer the image given below.
If u want to know more about struts 2 project structure please visit this link
Note: In eclipse, you are not allowed to paste a file directly in src folder. So you need to first paste it in any other place in the project( for example, in 'WebContent' folder), then use move functionality to put it in right place( That is 'src' folder).
You can place struts.xml file in src(Java Resources) packages.
When the compilation process struts.xml file will generate inside the ROOT/WEB-INF/classes directory.
if you get the same error again and again better check the struts actions.
check the deployed path of the application and you can find out what you want.
(struts.xml file)