Recent Changes - Search:

HomePage

"Later equals Never."

--Ryan Cooper

Blog

Maps & Geo

Java

Scala

Continuous Integration

Personal

Admin

  • Private area

edit SideBar

Struts13HowTo

How to create a struts application in Eclipse

  • Create a new Dynamic Web project in Eclipse
  • Unzip struts-blank.war
  • copy the content of src/java (MessageResources.properties) in src
  • copy all jsp pages to WebContent
  • copy META-INF and WEB-INF directories to WebContent
  • Edit web.xml and replace the DTD definition with an xmlns definition to enable expression language support.
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>

How to add a web page

  • Create the page.jsp file or copy the sample /pages/welcome.jsp
  • If needed, add the <%@ taglib declaration for the bean/html/logic tags
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
  • Optionally, add a global forward that gives the page an alias

How to create a form

  • Define a jsp page containing the form: EditCompany.jsp
<html:form action="EditCompanyAction">
        Comany name: <html:text property="name"></html:text><br/>
        Company address: <html:text property="address"/>
        <html:submit/>
        </html:form>
  • Create a form bean that extends ActionForm
public class CompanyForm extends ActionForm
{
        private String name;
        private String address;

        //set and get methods
}
  • Optionally, define the validate method (see below)
  • Define the form bean in struts-config.xml
<form-bean
            name="companyForm"
            type="package.CompanyForm"/>
  • Define an Action that receives the form,
public class EditCompanyAction extends Action
{
        @Override
        public ActionForward execute(ActionMapping mapping,
                        ActionForm form,
                        HttpServletRequest request,
                        HttpServletResponse response)
                        throws Exception
        {
                //cast the form object
                CompanyForm companyForm = (CompanyForm) form;
                //copy the companyForm data to a Company object




                //insert data in the db using a separate object
                try{
                        companyManager.insert(company);
                        return mapping.findForward("success");
        catch(Exception e)
        {
                        return mapping.findForward("failure");
        }
}
  • Define the Action in struts-config.xml
<action
            path="/EditCompanyAction"
            type="package.EditCompanyAction"
            scope="request"
            name="companyForm"
            validate="true"
            input="/pages/EditCompany.jsp">
  • Define the forwards to the various pages that follow the Action
<forward
                name="success"
                path="/pages/CompanyEditSuccess.jsp"/>

            <forward
                name="failure"
                path="/pages/Error.jsp"/>

How to validate a form

  • Write a method in ActionForm that overrides validate()
@Override
        public ActionErrors validate(ActionMapping mapping,
                        HttpServletRequest request)
        {
 
  • Create an ActionErrors object
ActionErrors errors = new ActionErrors();
  • Add ActionMessage objects to the error list
if (username == null || username.equals(""))
{
errors.add("username", new ActionMessage("username.missing"));
}
  • return the error list
return errors;
  • create the corresponding message keys in the resource.properties file
username.missing=Manca il nome utente

How to create search/results pages

  • Create a page with a form containing one or more search fields: CompanySearch.jsp
<html:form action=”SearchCompanyAction”>
        <html:text property=”name” />
        <html:submit/>
</html:form>
  • Optionally define a form bean with variables corresponding to the search fields
public class CompanySearchForm extends ActionForm
{
        private String name;

        //set and get
}
  • Define the form in struts-config.xml and call it companySearchForm
  • Create a Search action where the execute method reads the search parameter either from the form bean or directly from the request
public class SearchCompanyAction extends Action
{
        @Override
        public ActionForward execute(ActionMapping mapping,
                        ActionForm form,
                        HttpServletRequest request,HttpServletResponse response)
        {
                //cast the form object
                SearchCompanyForm scf = (SearchCompanyForm) form;
  • Call a findBy() method in the model class
List<Company> list = companyManager.findByName(scf.getName());
  • Put the list of items in the request scope (request.setAttribute(“listName”, listOfResults);
request.setAttribute(“companyList”, list);
  • Forward the view to the result page
return mapping.findForward("company-list");
  • Write a result page (e.g. UserList.jsp) that retrieves the elements from the list using the expression language or the
<logic:iterator> tag
        <logic:iterate id=”company” name=”companyList” type=”Company” >
                <bean write name=”company” property=”name”/> <br/>
                <bean write name=”company” property=”address”/> <br/>
        </logic:iterate>

How to create a list page

  • Use the same steps as the search/result page but create an action (ListAllCompaniesAction) without parameters that returns the list of all items.
  • Typically, if the search action receives no parameters it returns all items.

Remember: the list page cannot be called directly! You must call ListAllCompaniesAction instead

<html:link action=”ListAllCompaniesAction” />

How to add edit/delete actions to a list

  • Define an edit/delete action which accepts a parameter such as the object id or unique name
  • Dynamically generate for each element of the list a tag
<html:link action=”Edit”>
    <html:param name=”id” value=”${item.id}/>
</html:link>
Edit - History - Print - Recent Changes - Search
Page last modified on November 15, 2007, at 02:41 PM