Академический Документы
Профессиональный Документы
Культура Документы
In this session, we will try to build a very simple Struts application step by step.
12/30/2006
Sang Shin
sang.shin@sun.com www.javapassion.com/j2ee Java Technology Evangelist Sun Microsystems, Inc.
2
12/30/2006
12/30/2006
Sample App
? ?
Creating ActionForm object Creating Action object Forwarding at either success or failure through configuration set in struts-config.xml file Input validation Internationalizaition
The sample application we are going to use and show is Keld Hansen's submit application. The source files and ant build.xml script can be found in the handson/homework material you can download from class website. This is a simple application but it uses most of Struts framework.
12/30/2006
Steps to follow
12/30/2006
Steps
1.Create development directory structure 2.Write web.xml 3.Write struts-config.xml 4.Write ActionForm classes 5.Write Action classes 6.Create ApplicationResource.properties 7.Write JSP pages 8.Write ant build script 9.Build, deploy, and test the application
6
So this is the list of steps. Of course, you don't exactly follow these steps in sequence. In fact, it is expected that some of these steps will be reiterated.
12/30/2006
12/30/2006
Same development directory structure for any typical Web application Ant build script should be written accordingly If you are using NetBeans, it is taken care of by the IDE
The source directory structure is the same directory structure we used for other Web application development under Java WSDP. Of course, the build.xml script should be written accordingly.
12/30/2006
12/30/2006
web.xml
?
ActionServlet is like any other servlet Servlet definition and mapping of ActionServlet
There are several Struts specific <init-param> elements Struts tag libraries could be defined
10
Because Struts application is a genuine Web application, it has to follow the same rules that any Web application has to follow. And one of them is the presence of web.xml deployment descriptor file. The web.xml file should define ActionServlet, which is the controller piece that is provided by the Struts framework, and its mapping with URI. As you will see in the example web.xml file in the following slide, there are several Struts specific initialization parameters. Also the Struts tag libraries also need to be declared.
10
12/30/2006
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app 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"> 3 <servlet> 4 <servlet-name>action</servlet-name> 5 <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> 6 <init-param> 7 <param-name>config</param-name> 8 <param-value>/WEB-INF/struts-config.xml</param-value> 9 </init-param> 10 ... 11 </servlet> 12 <servlet-mapping> 13 <servlet-name>action</servlet-name> 14 <url-pattern>*.do</url-pattern> 15 </servlet-mapping>
11
Example: web.xml
This is the continuation of the web.xml file. Here you see the declarations of Struts tag libraries.
11
12/30/2006
12
12
12/30/2006
struts-config.xml
? ?
Identify required input forms and then define them as <form-bean> elements Identify required Action's and then define them as <action> elements within <action-mappings> element
make sure same value of name attribute of <formbean> is used as the value of name attribute of <action> element define if you want input validation
Decide view selection logic and specify them as <forward> element within <action> element
13
(read theslide)
13
12/30/2006
struts-config.xml: <form-beans>
1 2 3 4 5 6 7 8 9 10 11 12 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config> <form-beans> <form-bean </form-beans>
name="submitForm" type="submit.SubmitForm"/>
14
This is <form-beans> section of the struts-config.xml file. Here we define one <form-bean> element. The value of the name attribute of the <form-bean> element is the same as the value of name attribute of <action> element in the following slide.
14
12/30/2006
struts-config.xml: <action-mappings>
1
2 <!-- ==== Action Mapping Definitions ===============--> 3 <action-mappings> 4 5 <action path="/submit" 6 type="submit.SubmitAction" 7 name="submitForm" 8 input="/submit.jsp" 9 scope="request" 10 validate="true"> 11 <forward name="success" path="/submit.jsp"/> 12 <forward name="failure" path="/submit.jsp"/> 13 </action> 14 15 </action-mappings>
15
15
12/30/2006
16
16
12/30/2006
ActionForm Class
? ?
Extend org.apache.struts.action.ActionForm class Decide set of properties that reflect the input form Write getter and setter methods for each property Write validate() method if input validation is desired
17
17
12/30/2006
18
18
12/30/2006
19 a When this LogonForm is associated with controller, it will be passed to the controller whenever its service is requested by the user. JSP pages acting as the view for this LogonForm are automatically updated by Struts with the current values of the UserName and Password properties. If the user changes the properties via the JSP page, the LogonForm will automatically be updated by Struts But what if the user screws up and enters invalid data? ActionForms provide validation Before an ActionForm object is passed to a controller for processing, a validate method can be implemented on the form which allows the form to belay processing until the user fixes invalid input as we will see on the next slide...
19
12/30/2006
20
20
12/30/2006
Action Classes
? ?
Decide what kind of server-side Model objects (EJB, JDO, etc.) can be invoked
21
21
12/30/2006
22
22
12/30/2006
23
23
12/30/2006
Resource file
? ?
Create resource file for default locale Create resource files for other locales
24
24
12/30/2006
Example: ApplicationResource.properties
1 2 3 4 5 6 7 errors.header=<h4>Validation Error(s)</h4><ul> errors.footer=</ul><hr> error.lastName=<li>Enter your last name error.address=<li>Enter your address error.sex=<li>Enter your sex error.age=<li>Enter your age
25
25
12/30/2006
26
26
12/30/2006
JSP Pages
? ?
Write one JSP page for each view Use Struts tags for
27
27
12/30/2006
Example: submit.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <html> <head><title>Submit example</title></head> <body> <h3>Example Submit Page</h3> <html:errors/> <html:form action="submit.do"> Last Name: <html:text property="lastName"/><br> Address: <html:textarea property="address"/><br> Sex: <html:radio property="sex" value="M"/>Male <html:radio property="sex" value="F"/>Female<br> Married: <html:checkbox property="married"/><br> Age: <html:select property="age"> <html:option value="a">0-19</html:option> <html:option value="b">20-49</html:option> <html:option value="c">50-</html:option> </html:select><br>
28
28
12/30/2006
Example: submit.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 <logic:present name="lastName" scope="request"> Hello <logic:equal name="submitForm" property="age" value="a"> young </logic:equal> <logic:equal name="submitForm" property="age" value="c"> old </logic:equal> <bean:write name="lastName" scope="request"/> </logic:present> </body> </html>
29
29
12/30/2006
30
30
12/30/2006
For building and running the application If you use NetBeans, this step is not necessary
31
31
12/30/2006
32
32
12/30/2006
33
33
12/30/2006
34
34
12/30/2006
35
35
12/30/2006
36