struts配置参数详解
来源:互联网 发布:淘宝店铺营销亮点 编辑:程序博客网 时间:2024/06/08 01:05
Struts-config.xml配置文件讲解
1.<struts-config>元素:struts配置文件的根元素。(和它对应的配置类为org.apache.struts.config.MouleConfig类)
<data-sources>, <form-beans>, <global-exceptions>, <global-forwards>, <action-mappings>, <controller / >, <message-resources / >, <plug-in />。
2.<data-sources>元素:用来配置应用所需要的数据源。
<span style="font-family:Comic Sans MS;font-size:14px;"> <data-sources> <data-source type=”org.apache.commons.dbcp.BasicDataSource”> <set-property property=”autoCommit” value=”true”/> <set-property property=”description” value=”MYSQL Data Source”/> <set-property property=”driverClass” value=”com.mysql.jdbc.Driver”/> <set-property property=”maxCount” value=”10”/> <set-property property=”minCount” value=”2”/> <set-property property=”user” value=”root”/> <set-property property=”password” value=””/> <set-property property=”url” value=”jdbc:mysql://localhost:3306/数据库名称”/> </data-source> </data-sources></span>
配置了数据源后,就可在Action类中访问数据源了
Javax.sql.DataSource dataSource = getDataSource(request);
Javax.sql.Connection myConnection = dataSource.getConnection();
配置多个数据源:
<data-source key=”A” type=”org.apache.commons.dbcp.BasicDataSource”> …property as before… </data-source><data-source key=”B” type=”org.apache.commons.dbcp.BasicDataSource”> …property as before… </data-source>
在Action中取得数据源:
dataSourceA = getDataSource(request,”A”); dataSourceB = getDataSource(request,”B”);
3.<form-beans>元素:用来配置多个ActionForm Bean。
Example2(动态ActionForm):
<!-- sample form bean descriptor for a DynaActionForm <form-bean name="logonForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="username" type="java.lang.String"/> <form-property name="password" type="java.lang.String"/> </form-bean>
4.<global-exceptions>元素:用于配置异常处理类。
<global-exceptions> <exception key="expired.password" type="app.ExpiredPasswordException" path="/changePassword.jsp"/> </global-exceptions>
5.<global-forwards>元素:用来声明全局的转发关系。
<global-forwards> <!-- Default forward to "Welcome" action --> <!-- Demonstrates using index.jsp to forward --> <forward name="welcome" path="/Welcome.do"/> </global-forwards>
6.<action-mappings>元素:包含<action>元素,描述了从特定的请求路径到相应的Action类的映射。
在<action>元素中可以包含多个<exception>和<forward>元素,它们分别配置局部的异常
处理及请求转发仅被当前的Action所访问。
属性:
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)
(13)
<Action>元素的forward,includes,type属性相互排斥(即只能设置其中一项)。
Example:
<Action path=”/search” type=”addressbook.actions.SearchAction” name=”searchForm” scope=”request” validate=”true” input=”/search.jsp”> <forward name=”success” path=”/display.jsp”/> </action>
注意:局部<forward>元素和<exception>元素的优先级大于全局的。
7.<controller>元素:用于配置ActionServlet。
(1)
(2)
(3)
(4)
(5)
(6)
(7)
Example:
<controller contentType=”text/html;charset=UTF-8” locale=”true” processorClass=”CustomRequestProcessor”/>
8.<message-resources>元素:用来配置resource bundle,resource bundle用于存放本地化消息文本。
(1)
(2)
(3)
(4)
(5)
Example:
9.<plug-in>元素:用于配置struts插件。
<span style="font-family:Comic Sans MS;font-size:14px;"><plug-in className="org.apache.struts.tiles.TilesPlugin" ></span><span style="font-family: 'Comic Sans MS';font-size:14px; "> </span><wbr style="font-family: 'Comic Sans MS';font-size:14px; "><span style="font-family: 'Comic Sans MS';font-size:14px; "> </span><wbr style="font-family: 'Comic Sans MS';font-size:14px; "><span style="font-family: 'Comic Sans MS';font-size:14px; "> </span><wbr style="font-family: 'Comic Sans MS';font-size:14px; "><span style="font-family: 'Comic Sans MS';font-size:14px; "> </span><wbr style="font-family: 'Comic Sans MS';font-size:14px; "><span style="font-family: 'Comic Sans MS';font-size:14px; "> </span><!-- Path to XML definition file --> <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" /> <!-- Set Module-awareness to true --> <set-property property="moduleAware" value="true" /> </plug-in></wbr></wbr></wbr></wbr>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/> </plug-in>
struts-config.xml
- <action path="/login" type="com.jialin.LoginAction" name="userForm"
- scope="request" validate="true" attribute="uf">
- <forward name="success" path="/LoginSuccess.jsp" />
- <forward name="fail" path="/LoginFail.jsp" />
- </action>
其中 validate和attribut有什么用?
validate,是否验证表单数据。
我们在ActionForm中,重写父类的validate方法,当从表单收集完数据放到ActionForm后,如果配置文件中validate=true,将调用该方法,这里我们可以做一些数据验证。
- @Override
- public ActionErrors validate(ActionMapping mapping,
- HttpServletRequest request) {
- System.out.println("-------ActionForm.validate()从表单收集完数据放到ActionForm后做验证--------");
- return super.validate(mapping, request);
- }
当然,如果系统安全性各方面要求不高,我们一般的验证都是放在javascript里的。
attribute
如果我们配置了attribute属性,我们在jsp中要获取actionform中的值得时候,form名就变为attribute设置的值。而不再是form-bean节点下 name属性的值。
例如
- <struts-config>
- <!-- set ActionForm info-->
- <form-beans>
- <form-bean name="userForm" type="com.jialin.UserActionForm" />
- </form-beans>
- <action-mappings>
- <!-- Set path,action,actionform,scope,forward info -->
- <action path="/login" type="com.jialin.LoginAction" name="userForm"
- scope="request" validate="true" attribute="uf">
- <forward name="success" path="/LoginSuccess.jsp" />
- <forward name="fail" path="/LoginFail.jsp" />
- </action>
- </action-mappings>
- </struts-config>
如果我们要用el表达式取表单的值,不能使用${userForm.name},而是${uf.name}
ActionForm中还可以重写父类的reset的方法,可以在把收集表单数据往ActionForm中放之前做一些处理
- @Override
- public void reset(ActionMapping mapping, HttpServletRequest request) {
- System.out.println("------------ActionForm.reset()<span style="font-family:Calibri;">可以</span><span lang="zh-CN" style="font-family:宋体;">在把收集表单数据往</span><span lang="en-US" style="font-family:Calibri;">ActionForm</span><span lang="zh-CN" style="font-family:宋体;">中放之前做一些处理</span>-----------");
- }
- struts配置参数详解
- struts.xml参数配置
- Struts框架配置详解
- struts.xml配置详解
- struts.xml配置详解
- struts.xml配置详解
- struts 2 配置详解
- struts配置与详解
- Struts.xml配置详解
- struts.xml配置详解
- struts.properties配置详解
- struts.xml配置详解
- struts.xml配置详解
- struts.xml配置详解
- struts.xml配置详解
- struts.xml配置详解
- struts.properties配置详解
- struts.xml配置详解
- 漫漫编程路上学到的宝贵经验
- Linux Top 命令解析 比较详细
- 同为程序员 为什么我的工资最低
- UVA - 11014 Make a Crystal
- xampp 80 443端口冲突和MySQL Service detected with wrong path解决办法
- struts配置参数详解
- Linux学习笔记:虚拟机设置中的各种网络连接方式详解
- 【基于WinForm+Access局域网共享数据库的项目总结】之篇二:WinForm开发扇形图统计和Excel数据导出
- 看操作系统虚拟化原理总结篇——虚拟化概述原理
- Maven查找使用的包
- iphone手势识别(双击、捏、旋转、拖动、划动、长按)UITapGestureRecognizer
- opencv 图片基本处理(灰度化,反色,二值化,膨胀腐蚀以及ROI的copy等)
- C ++ 抽象工厂模型
- UVa989 - Su Doku(数独游戏)