File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Equalize Java constants and struts-config.xml entries? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Equalize Java constants and struts-config.xml entries?" Watch "Equalize Java constants and struts-config.xml entries?" New topic

Equalize Java constants and struts-config.xml entries?

Chris Wimmer

Joined: Aug 18, 2005
Posts: 5

exists there any (easy) possibility to equalize the action forward patterns in struts config with static Java constants?
"Easy" means without parsing the files with any other language and to textual replace the strings.
The example below defines some mapping constants, uses them in action and in struts-config.xml.

public interface WebConstants {
public static final String SUCCESS = "success";
public static final String FAILURE = "failure";


public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
ActionForward forward = mapping.findForward(WebConstants.FAILURE);

if(form != null) {
forward = mapping.findForward(WebConstants.SUCCESS);


return forward;


<action path="/ConfigInit"
scope="session" >
<forward name="success" path="/" contextRelative="false" />
<forward name="failure" path="/" contextRelative="false" />

In the example the file contains static string constants for SUCCESS and FAILURE. In struts-config.xml the content of the definitions is used in the name attribute of the forward declaration.
In a greater software project you may have many different constants and many different labeled forward "names".

Exists there a solution to avoid misspelling in these two places?


Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'm afraid there's no easy way to do this. Struts simply reads in the configuration file at startup time and does not allow for inserting constants from classes.

If you wanted to do this, you'd have to write your own extensions to Struts and devise a symbol for indicating that the text should be replaced with a constant in a java class.

Consultant, Sima Solutions
Chris Wimmer

Joined: Aug 18, 2005
Posts: 5
Your suggestion to extend Struts and to let the ModuleConfig implementation do the replacement is a good idea. I've not thought about this till now. Thank you very much for that hint.


I agree. Here's the link:
subject: Equalize Java constants and struts-config.xml entries?
It's not a secret anymore!