Wednesday, December 14, 2011

Struts

This is a short how to get a Struts application up and running.
The following run time configuration was used
jdk1.5.0_09
Jboss4.0.5
Ubuntu Edgy Eft
The following development environment was used
Eclipse3.2.2
Web Tools plugin 1.5.1
Struts version used is 1.3.8.
If you need a more thorough explanation on the MVC2 pattern browse the net.
You need some things to get it to work. First off the struts-config.xml file.
This file should be in the WEB-INF directory. Remember, WEB-INF makes the file inaccessible to the outside world.




















Well, well what does all that mean.
First off a form-bean. And now, what does a form bean do? It basically means that
if you post data Struts make sure that your parameters are sent to the server and handled there.
For instance if you post a user name that name will be sent to the server, and the instance of the form class will use getters/setters and use that.
All form beans must extend the org.apache.struts.action.ActionForm; Please override the validate method since this makes it possible for you
to handle erroneous data in a good way. I have implemented the reset method as well.
my form bean
public class StartForm extends ActionForm {

/**
*
*/
private static final long serialVersionUID = -3219629289363196387L;

private String name;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

/**
* Reset all properties to their default values.
*
* @param mapping The mapping used to select this instance
* @param request The servlet request we are processing
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.name=null;
}

/**
* Reset all properties to their default values.
*
* @param mapping The mapping used to select this instance
* @param request The servlet request we are processing
* @return errors
*/
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"));
}


return errors;
}
Ok, now we are done with the Form. This is the Model aprt of the pattern. The model can be seen as a stupid data carrier.
We'll take the action now.

public class StartAction extends Action {
package com.aja.lib.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

ActionErrors errors = form.validate(mapping, request);

if (errors.size() > 0){
return mapping.findForward("fail");
}


return mapping.findForward("success");
}

}
As you can see, extend the org.apache.struts.action.Action class and override the execute method. You have two different outcomes. In case of a success go to
"success" and in case of error go to "fail". These two paths must be mapped in the struts-config.xml file. Remember this code.


The action in our case is where you usually put your calls to the tiers behind the presentation tier, i.e business tier. This is the C in MVC, the control part, the traffic handler.
What about the View? Well, look above in the code "success.jsp" and fail.jsp that's our views. They must exist.
First off the start point that we defined in the struts-config.xml, start.jap
<%@ page language="java" pageEncoding="ISO-8859-1"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>







Wddfsd


























Please Enter the Following Details

Name



Save

Cancel




Now here we see some magic. Note the html:text property="name", this will be mapped towards our form beans setName method.
I made it simple here, success just forwards to a stupid empty page, and fail back to start.jsp. below is my success.jsp
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>





Sucess











Deploy it and write http://locahost:8080/LibWeb/start.jsp. Good luck now.
One final word, if you develop something for real, you will most likely for instance not have a .jsp instead you can add in the configuration a lot of default handling stuff.

0 comments:

Post a Comment