Struts
Struts Example
struts example tutorials
Our first struts application
Our first small application will be a page with an HTML form containing all the well known controls.
This is the form below with struts application. The example code is shown below
freewebschools register page
When we submit this page it should redirect itself without loosing the data that we have entered.
Starting a new struts applicationthe web.xml file is where servlets and
other stuff are defined to the servlet container |
web.xml
<web app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/*.do</URL-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/web-ing/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/web-ing/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/web-ing/struts-logic.tld</taglib-location>
</taglib></web-app> |
The file contains 3 sections
A the definition of the struts servlet named "actionservlet"
the url mapping for the calls to this servlet
The definitions of the struts tag libraries
You will see that the servlet will be called if our browser requests a file when we submit the form in our one page application we will decide
to use the action name "sumit.do"
The mapping of the request to a specific action and action form class is done in the struts-config.file.
I have edited the file form “struts-blank” to suit our one page application
|
struts-config.xml
<struts-config>
<form-beans>
<form bean name=”submitform” type=”Freewebschools.playground.Submitform” />
</form-beans>
<action-mappings>
<action path=”/submit” type=”Freewebschools.playgroung.SubmitAaction” name=”submitform” input=”/submit.jsp” scope=”request”.
<forward name=”success” path=”/submit.jsp”/>
<forward name=”failure” path=”/submit.jsp”/>
,/action>
</action-mappings>
</struts-config>
|
As you see the file contains two section: the form-beans section, that lists the actionForm beans and the action-mappings.
Building the jsp page
Submit.jsp |
In order for the ActionForm bean to work, you must use struts own tags for creating the HTML form and the controls in it.
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-logic.tld" prefix="logic" %>
<html>
<head><title>freewebschools students</title></head>
<body>
<h3> freewebschools students</h3>
<html:errors/>
<html:form action=”submit.do”>
student name:<html:text property=”name”/><br>
address :<html:text property=”address”/><br>
sex <html:radio property=”sex” value=”m”/>male
<html:radio property=”sex” value=”f”/>female<br>
</html:form>
</body></html> |
Coding the actionform class
This class must extend the struts actionForm, and must have setters and getters for all the form controls in the jsp-page.
|
actionform class
Package Freewebschools.playground;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.Action;
public final class submitform extends ActionForm
{
/*student name*/
private String name=”frewebschools”;
public String getLastName(){
return(this.lastName);
}
public void setLastNmae(String name)
{
this.name=name;
}
/*address*/
private String address=null;
public String getAaddress
()
{
return(this.address);
}
public void setAddress(String address)
{
this.address=address;
}
/*sex*/
private String sex=null;
public String getSex()
{
return(this.sex);
}
public void setsex(String sex)
{
this.sex=sex;
}
} |
This class is placed in the WEB-INF/classes/Freewebschools/playgroung directory
Coding the action class
The action class is seen form the application programmers prospective the heart of the application. This is where you must decide how you will separate your own application code. most often the action class should be kept as thin as possible, placing business logic in other beans or even EJBS’S on other servers.
The implementation of the action class must contain a “perform” method, which receives the request and response objects, the instance and the action mapping information from the configuration file. A very simple ActionForm class, which simply lets the request pass unaltered, is given as follows
|
action class
Packaged Freewebschools.palyground
Import javax.servlet.http.*;
Import org.apache.struts.action.*;
Public final c;las SubmitAction extends Action
{
public ActionForwared perform(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
{
SubmitForm f=(Submit Form) form;
String name=f.getLastName();
Request.setAttribute(“name”,name.toUpperCase());
Return(mapping.findForwd(“success”));
}
} |
This class is also placed in WEB-INF/classes/Freewebschools/playground directory
An important thing to remember is that struts only creates a single instance of the action class, shared amongst all users of the application.
Testing your application
Restart your servlet container
To test your example
Enter this url in the browser
http://locslhost:8080/myproject/submit.jsp.
If you application works, you will see that last name is filled with “Freewebschools”(which means that struts has already created an instance of the actionForm bean, and extracted the data form it)
Now enter all the data in the controls and press submit. Note that url changes to
Submit.do and that all the data stays unchanged in the form
|