Site hosted by Angelfire.com: Build your free website today!

Using STRUTS in your Java application

Use the ANT buildtool to create a .WAR file with the following structure.
The .WAR file can then be deployed to Apache or any other Servlet engine.

index.jsp main.jsp search.jsp login.jsp style.css WEB-INF/ WEB-INF/classes/ WEB-INF/classes/com/ WEB-INF/classes/com/struts/ WEB-INF/classes/com/struts/ApplicationResources.properties WEB-INF/classes/com/struts/SearchAction.class WEB-INF/classes/com/struts/LoginAction.class WEB-INF/classes/com/struts/MainAction.class WEB-INF/lib/ WEB-INF/lib/struts.jar WEB-INF/struts-bean.tld WEB-INF/struts-config.xml WEB-INF/struts-form.tld WEB-INF/struts-html.tld WEB-INF/struts-logic.tld WEB-INF/struts-template.tld WEB-INF/struts.tld WEB-INF/web.xml
The following 4 steps shows how Struts is used to control the navigation and validation of a simple JSP page using the Struts1.1 Controller Framework. 1. Create Action mapping (Struts-config.xml) <action> path="/attemptLogin" type="com.generic.action.LoginAction" name="dynaLoginForm" scope="request" validate="false" input="/login.jsp" <forward name="success" path="/main.jsp"> <action> 2. Create DynaActionForm (Struts-config.xml) <form-beans> <form-bean name="dynaLoginForm" type="org.apache.struts.validator.DynaValidatorForm"> <form-property name="user" type="java.lang.String"/> <form-property name="userID" type="java.lang.String"/> <form-property name="password" type="java.lang.String"/> </form-bean> </form-beans> 3. Create login.jsp Page (revised to show fields) User: <html-el:text property="user" size="4" maxlength="4"/> User ID: <html-el:text property="userID" size="4" maxlength="2"/> Password: <html-el:password property="password" size="20" maxlength="20"/> 4. Create validation form (Validation.xml) <formset> <form name="dynaLogonForm"> <field property="user" depends="required,mask"> <arg0 key="User name" resource="false"/> </field> <field property="userID" depends="required,mask"> <arg0 key="User ID" resource="false"/> <var> <var-name>mask</var-name> <var-value>${numbersOnly}</var-value> </var> </field> <field property="password" depends="required"> <arg0 key="Password" resource="false"/> </field> </form> </formset> Note that all of the fields have the EXACT same name - The Struts Dyna Forms require this in order to correctly process the data from the HTML form to the dynamically created Struts DynaActionForm. The data from the page can then be retrieved from the Form inside the LoginAction class, and the data can be processed. If there is an error on the page <wrong password>, the user name and user ID can be automatically retrieved from the DynaActionForm and placed on the JSP page again so the user only has to retype their password.
This can also be accompished manually by writing your own "Bean" that contains the exact Java variable names as the JSP form names <user, password, etc>, and you can use the BeanUtils.copyProperties<bean, form> in the LoginAction class to accomplish the retrieving of fields.
WEB.XML Struts ActionServlet Mapping <servlet-mapping> <servlet-name> action <servlet-name> <url-pattern> *.do <url-pattern> <servlet-mapping>
Non-Struts Servlet Mapping <servlet> <servlet-name> Login <servlet-name> <servlet-class> com.generic.servlet.LoginServlet <servlet-class> <servlet> <servlet-mapping> <servlet-name> Login <servlet-name> <url-pattern> /Login <url-pattern> <servlet-mapping> HREF tag: Calling a Struts Action class from HTML <a href="/fwrk/Login.do"> HREF tag: Calling a Servlet from HTML <a href="/fwrk/Login"> Form tag: Calling a Struts Action class using HTML-EL <html-el:form action="/Login" OR html-el:form action="/Login.do"> Form tag: Calling a Servlet using standard FORM tag <form action="/fwrk/Login"> Form tag: Calling a Struts Action class using standard FORM tag <form action="/fwrk/Login.do">