File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Equalize Java constants and struts-config.xml entries?

Chris Wimmer
Greenhorn

Joined: Aug 18, 2005
Posts: 5
Hello,

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.

WebConstants.java:

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

}


Action.java:

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


struts-config.xml:

<action path="/ConfigInit"
type="app.myTest.struts.action.ConfigInitAction"
scope="session" >
<forward name="success" path="/ServerConfig.do" contextRelative="false" />
<forward name="failure" path="/errorFwd.do" contextRelative="false" />
</action>



In the example the WebConstants.java 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?


Thanks


Chris
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.


Merrill
Consultant, Sima Solutions
Chris Wimmer
Greenhorn

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.

Ciao

Chris
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Equalize Java constants and struts-config.xml entries?