Reality is a perception. Perceptions are not always based on facts, and are strongly influenced by illusions. Inquisitiveness is hence indispensable

Thursday, January 1, 2009

Web development framework - Struts 1 - part2

In this post, we will see some nuances involved in struts-config.xml, as described earlier, struts-config is set of guidelines for actionservlet that help request delegation.

Struts config example


The following xml file illustrates some of the common features.
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="SimpleFormBean" type="org.ui.beans.SimpleFormBean"/>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
<forward name="welcome" path="/Welcome.do"/>
<forward name="success" path="/success.jsp"/>
<forward name="showLogin" path="/csaLogin.jsp"/>
</global-forwards>
<action-mappings>
<action input="/csaLogin.jsp" name="SimpleFormBean" parameter="simpleShow" path="/show" scope="session" type="org.ui.actions.SimpleStrutsAction"/>
<action path="/Welcome" forward="/welcomeStruts.jsp"/>
</action-mappings>
<message-resources parameter="com/myapp/struts/ApplicationResource"/>
<message-resources key="newBundle" parameter="com/myapp/struts/NewResource"/>

<!-- ========================= Validator plugin ================================= -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>




The config file is made up of three major aspects: formbeans, action mappings, message-resources. ActionServlet processes the action mappings and identifies the action to be used for the current request. The 'path' attribute influences the decisions. The action element in xml also specifies the target action class in 'type' attribute and form bean to be used using 'name' attribute. The form bean listed in 'name' also listed under form beans element in the config xml file. The class used for bean representation is mentioned here. An action element can also directly map to a html or jsp file.

The action class contract dictates the use of execute() method. (however, using dispatch action forms with parameters, we can call any method of our choice). The form-bean as discussed earlier, is a good candidate for validation actions.

An example of how validations would look.

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (getName() == null || getName().length() < 1) {
errors.add("name", new ActionMessage("error.name.required"));
}
if(getNumber() <= 0){
errors.add("number",new ActionMessage("error.posnumber.needed",getNumber()));
}
{
int x = (int)getNumber();
double y = x;
int res = ((Double)y).compareTo(getNumber());
if(res != 0){
errors.add("number",new ActionMessage("error.integer.needed",getNumber()));
}
}
return errors;
}


The jsp file meant to use the errors looks like this:

  <div id="page1">
<html:errors bundle="newBundle"/>
<html:form action="show.do" method="post">
<div class="fields">
<label for="user">USER</label> <html:text property="name" ></html:text>
<html:errors bundle="newBundle" property="name"></html:errors>
<label for="user-id">USER-ID</label><html:text property="number" ></html:text>
<html:errors property="number"></html:errors>
</div>
<span class="buttons">
<html:submit property="submitType" value="submit1"/>
<html:submit property="submitType" value="submit2"/>
</span>
</html:form>

</div>


The attribute bundle="newBundle" is another aspect, the message properties can be customised (one of the starting steps for internationalisation, no we won't be looking at it in this post). We can have as many message/resource bundles as we like. Have a look at struts-config.xml. The bundle attribute in error tags should map to the key attribute given in struts config.

That summarised our discussion on basic struts.

No comments:

Popular Posts

Labels

About Me

Well for a start, I dont' want to!. Yes I am reclusive, no I am not secretive; Candid? Yes; Aspergers? No :). My friends call me an enthusiast, my boss calls me purist, I call myself an explorer, to summarise; just an inquisitive child who didnt'learn to take things for granted. For the sake of living, I work as a S/W engineer. If you dont' know what it means, turn back right now.