|
"Later equals Never." --Ryan Cooper
|
Java /
Struts13HowToHow to create a struts application in Eclipse
<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
<%@ 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" %>
How to create a form
<html:form action="EditCompanyAction"> Comany name: <html:text property="name"></html:text><br/> Company address: <html:text property="address"/> <html:submit/> </html:form>
<form-bean name="companyForm" type="package.CompanyForm"/>
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"); } }
<action path="/EditCompanyAction" type="package.EditCompanyAction" scope="request" name="companyForm" validate="true" input="/pages/EditCompany.jsp">
<forward name="success" path="/pages/CompanyEditSuccess.jsp"/> <forward name="failure" path="/pages/Error.jsp"/> How to validate a form
@Override public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (username == null || username.equals("")) { errors.add("username", new ActionMessage("username.missing")); }
return errors;
username.missing=Manca il nome utente How to create search/results pages
<html:form action=”SearchCompanyAction”> <html:text property=”name” /> <html:submit/> </html:form>
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;
List<Company> list = companyManager.findByName(scf.getName());
request.setAttribute(“companyList”, list);
return mapping.findForward("company-list");
<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
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
<html:link action=”Edit”> <html:param name=”id” value=”${item.id}” /> </html:link> |