|
Java_struts 入门快速手册
----by omencathay
Struts
简介
1、 Struts基于MVC模式 Model(Bean) ,View(jsp文件), Control (Action类,ActionForm类)
2、 开发准备
struts http://jakarta.apache.org/struts
jdk1.2版本以上 http://java.sun.com/j2se
Xerces XML parser http://xml.apache.org/xerces-j
下载struts后,解压zip文件到一个临时目录;然后拷贝struts.jar, jakarta-oro.jar, jdbc2_0-stdext.jar和common*.jar文件到你所应用struts的WEB-INF/lib目录下;拷贝struts*.tld文件和*.dtd文件到WEB-INF目录下,确定该才提到的所有的jar包都在web应用服务器的 classpath环境变量上设置。
3、 配置 (两个xml文件:web.xml, struts-config.xml)
配置web.xml,第一件事情就是注册组件action servlet,<servlet> .. </servlet>标签封装了整个struts 框架的应用程序
<!-- Action Servlet Configuration -->
1 <servlet>
2 <servlet-name>action</servlet-name>
3 <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
4 <init-param>
5 <param-name>application</param-name>
6 <param-value>myApp.properties.ApplicationResources</param-value>
7 </init-param>
8 <init-param>
9 <param-name>config</param-name>
10 <param-value>/WEB-INF/struts-config.xml</param-value>
11 </init-param>
12 </servlet>
13 <!-- Action Servlet Mapping -->
14 <servlet-mapping>
15 <servlet-name>action</servlet-name>
16 <url-pattern>*.do</url-pattern>
17 </servlet-mapping>
18 <!-- Application Tag Library Descriptor -->
19 <taglib>
20 <taglib-uri>/WEB-INF/lib/app.tld</taglib-uri>
21 <taglib-location>/WEB-INF/lib/app.tld</taglib-location>
22 </taglib>
23 <!-- Struts Tag Library Descriptor -->
24 <taglib>
25 <taglib-uri>/WEB-INF/lib/struts.tld</taglib-uri>
26 <taglib-location>/WEB-INF/lib/struts.tld</taglib-location>
27 </taglib>
28 <!-- Struts Tag Library Descriptors -->
29 <taglib>
30 <taglib-uri>/WEB-INF/lib/struts-bean.tld</taglib-uri>
31 <taglib-location>/WEB-INF/lib/struts-bean.tld</taglib-location>
32 </taglib>
33 <taglib>
34 <taglib-uri>/WEB-INF/lib/struts-form.tld</taglib-uri>
35 <taglib-location>/WEB-INF/lib/struts-form.tld</taglib-location>
36 </taglib>
37 <taglib>
38 <taglib-uri>/WEB-INF/lib/struts-logic.tld</taglib-uri>
39 <taglib-location>/WEB-INF/lib/struts-logic.tld</taglib-location>
40 </taglib>
41 <taglib>
42 <taglib-uri>/WEB-INF/lib/struts-template.tld</taglib-uri>
43 <taglib-location>/WEB-INF/lib/struts-template.tld</taglib-location>
44 </taglib>
struts-config.xml文件
1 <?xml version="1.0" encoding="ISO-8859-1" ?>
2 <!DOCTYPE struts-config PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
4 "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
5 <struts-config>
6 <data-sources>
7 <data-source key = "myDatabase">
8 <set-property property="autoCommit" value="false"/>
9
10 <set-property property="description"
11 value=" Login Validation Database"/>
12 <set-property property="driverClass"
13 value="org.postgresql.Driver"/>
14 <set-property property="maxCount"
15 value="4"/>
16 <set-property property="minCount"
17 value="2"/>
18 <set-property property="user"
19 value="myusername"/>
20 <set-property property="password"
21 value="mypassword"/>
22 <set-property property="url" value="jdbc ostgresql://localhost/myAppDatabase"/>
23
24 </data-source>
25 </data-sources>
26 <!-- ========== Form Bean Definitions ========================== -->
27 <form-beans>
28 <!-- Logon form bean -->
29 <form-bean name="login"
30 type="myApp.FormBeans.LoginForm"/>
31 <!-- Order List -->
32 <form-bean name="orderlist"
33 type="myApp.FormBeans.OrderForm"/>
34 </form-beans>
35 <!-- ========== Global Forward Definitions ===================== -->
36 <global-forwards>
37 <forward name="login" path="/login.jsp"/>
38 </global-forwards>
39 <!-- ========== Action Mapping Definitions ===================== -->
40 <action-mappings>
41 <!-- Process a user logon -->
42 <action path="/login"
43 type="myApp.action.LoginExec"
44 name="login"
45 validate = "true"
46 scope="session"
47 input="/login.jsp">
48 <forward name="success" path="/secondpage.jsp"/>
49 <forward name="listorder" path="/orderlistentry.do"/>
50 <forward name="killSession" path="/SystemError.html"/>
51 </action>
52 <!-- Process a user logoff -->
53 <action path="/logout"
54 type="myApp.action.LogoutExec">
55 <forward name="success" path="/logout.jsp"/>
56 </action>
57 <!-- Process an order list -->
58 <action path="/orderlistentry"
59 type="myApp.action.OrderListExec"
60 name="orderlist"
61 scope="session"
62 input="/orderlistentry.jsp">
63 <forward name="success" path="/orderlistdisp.jsp"/>
64 <forward name="killSession" path="/SystemError.html"/>
65 <forward name="loginAgain" path="/login.jsp"/>
66 <forward name="selectSalesArea" path="/salesAreas.jsp"/>
67 </action>
68 </action-mappings>
69 </struts-config>
注:最重要的部分是action-mappings 部分
<action-mappings>
每个<action>元素定义了特定的功能与相关联的动作类, 大多数action元素至少包括以下三个部分。
Path-----应用程序映射的路径
Type-----需要的完整的包和类
Name-----action应用的<form-bean>元素的名称
;
<forward>标签告诉struts框架将要跳转到哪个jsp页面;
<data-sources>标签给出数据源属性;
4、 struts核心标签库示例
1. Html:form
属性:onsubmit
onsubmit属性得到的是:表单提交时的javaScript事件句柄
用法:
(1).
Onsubmit 和 html:submit配合使用,javaScript函数返回true或false
<html:form action=”” onsubmit=”return javaScriptFunction();”>
和
<html:submit onclick=” javaScriptFunction();”/>
配合使用
在javaScriptFunction(){
}中加入return true和return false
html:from 根据返回的true或者false决定是否提交
等同于
(2)。
使用Html:button 要在javaScript函数中加入form.submit();
<html:form action=””>
<html:button onclick=” javaScriptFunction();”/>
form.submit();决定表单的提交
2. Html:text
Html:text是输入框,可以修改数据做为property的值
如果要修改的属性在bean中有get\set方法,可以写为
<html:text name="listInfoFormBean" property="invoiceCode"/>
如果要修改的属性在某一个VO里,在VO里对这一属性有get\set方法,在bean里对这一VO有get\set方法,可以写为:
<html:text name="listInfoFormBean" property="tbInvStoresVO.invoiceCode"/>
3. Bean:write
Bean:write与html:text用法相似。只是bean:write输出的数据是readonly的
这一点可以在html:text中加入属性 readonly=”true”来实现
4. Html:select
<html:select property="invoicePurchaseCode" name=”beanname”>
<option value="0">----</option>
<option value="1">验旧供新</option>
<option value="2">交旧供新</option>
<option value="3">批量供应</option>
</html:select>
name::bean的名称。如果没有设置,将适用于这个内嵌表单相关的formbean的名字
property:表单提交是送回的请求参数的名字,也是用来确定哪个属性被选中的bean的属性的名字
value:用来表明需要被选中的选项
怎么生成一个select:
option.options = new(value, lablename)
5. logic:iterate
logic:iterate的两个用法
1. 在javaScript中初始化数组数据
//声明数组
var invoiceWordCodeArray = new Array();
//构造函数
function invoiceWordCode(invoiceCode,invoiceWordCode){
this.invoiceCode = invoiceCode;
this.invoiceWordCode = invoiceWordCode;
}
//初始化数据
<logic:iterate id="item2" name="listInfoFormBean" property="wordCodeList" indexId="i">
invoiceWordCodeArray[<bean:write name="i"/>] =
new invoiceWordCode(
'<bean:write name="item2" property="invoiceCode"/>',
'<bean:write name="item2" property="invoiceWordCode"/>');
</logic:iterate>
2.在页面上循环显示一个集合中的数据
<logic:iterate indexId="idx" id="sampleBean" name="sampleList">
<tr>
<td align="center" style="padding:1px 4px 0px 5px"></td><td width="150">
<bean:write property="id" name="sampleBean"/></td><td width="150">
<bean:write property="content" name="sampleBean"/></td><td width="150">
<bean:write property="creator" name="sampleBean"/></td>
</tr>
</logic-el:iterate>
id:页面作用域bean的名称,集合的别名
name:formbean的名字,它包含property
property:需要循环的集合的名字
logic:iterate:需要在formbean中对集合中的VO有get\set方法
indexed=”true”:每次重复完成后集合当前的索引 |
|