您的位置:首页 > 编程语言 > Java开发

Struts配置文件

2005-12-06 18:15 519 查看
这是将struts组件结合在一起的东东:struts-config.xml。默认值/WEB-INF/struts-config.xml。配置文件可以定义:l 全局转发l ActionMapping类l ActionForm beanl JDBC数据源配置全局转发


全局转发用来在JSP页之间创建逻辑名称映射。转发都可以通过对调用操作映射的实例来获得,例如: actionMappingInstace.findForward(“ligicalName”);全局转发的例子:(所有的例子我没有进行解释,一是结合表可以理解,二是例子大部分来自系列四的示例,你应该在作完实验后,再来看一便)<global-forwards>

<forward name="bookCreated" path="/BookView.jsp"/>

</global-forwards>

属性


描述


Name全局转发的名字
Path与目标URL的相对路径
配置ActionMapping


ActionMapping对象帮助进行框架内部的流程控制,它们可将请求URI映射到Action类,并且将Action类与ActionForm bean相关联。ActionServlet在内部使用这些映射,并将控制转移到特定Action类的实例。所有Action类使用perform()方法实现特定应用程序代码,返回一个ActionForward对象,其中包括响应转发的目标资源名称。例如:<action-mappings>

<action path="/createBook"

type="BookAction"

name="bookForm"

scope="request"

input="/CreateBook.jsp">

</action>

<forward name=”failure” path=”/CreateBook.jsp”/>

<forward name=”cancel” path=”/index.jsp”/>

</action-mappings>

属性


描述


path

Action类的相对路径

name

与本操作关联的Action bean的名称

type

连接到本映射的Action类的全称(可有包名)

scope

ActionForm bean的作用域(请求或会话)

prefix

用来匹配请求参数与bean属性的前缀

suffix

用来匹配请求参数与bean属性的后缀

attribute

作用域名称。

className

ActionMapping对象的类的完全限定名默认的类是org.apache.struts.action.ActionMapping

input

输入表单的路径,指向bean发生输入错误必须返回的控制

unknown

设为true,操作将被作为所有没有定义的ActionMapping的URI的默认操作

validate

设置为true,则在调用Action对象上的perform()方法前,ActionServlet将调用ActionForm bean的validate()方法来进行输入检查

通过<forward>元素,可以定义资源的逻辑名称,该资源是Action类的响应要转发的目标。

属性


描述


idID
classNameActionForward类的完全限定名,默认是org.apache.struts.action.ActionForward
name操作类访问ActionForward时所用的逻辑名
path响应转发的目标资源的路径
redirect若设置为true,则ActionServlet使用sendRedirect()方法来转发资源
配置ActionForm Bean


ActionServlet使用ActionForm来保存请求的参数,这些bean的属性名称与HTTP请求参数中的名称相对应,控制器将请求参数传递到ActionForm bean的实例,然后将这个实例传送到Action类。例子:<form-beans>

<form-bean name="bookForm" type="BookForm"/>

</form-beans>

属性

描述

id

ID

className

ActionForm bean的完全限定名,默认值是org.apache.struts.action.ActionFormBean

name

表单bean在相关作用域的名称,这个属性用来将bean与ActionMapping进行关联

type

类的完全限定名

配置JDBC数据源


用<data-sources>元素可以定义多个数据源。
属性


描述


idID
keyAction类使用这个名称来寻找连接
type实现JDBC接口的类的名称
下面属性需要<set-property>元素定义,在框架1.1版本中已不在使用,但你可用<data-source>元素。例子:<data-sources>

<data-source id=”DS1”

key=”conPool”

type=”org.apache.struts.util.GenericDataSource”

<set-property id=”SP1”

autoCommit="true"

description="Example Data Source Configuration"

driverClass="org.test.mm.mysql.Driver"

maxCount="4"

minCount="2"

url="jdbc:mysql://localhost/test"

user="struts"

password="wrox" />

<data-source/>

</data-sources>

属性


描述


desciption数据源的描述
autoCommit数据源创建的连接所使用的默认自动更新数据库模式
driverClass数据源所使用的类,用来显示JDBC驱动程序接口
loginTimeout数据库登陆时间的限制,以秒为单位
maxCount最多能建立的连接数目
minCount要创建的最少连接数目
password数据库访问的密码
readOnly创建只读的连接
user访问数据库的用户名
urlJDBC的URL
通过指定关键字名称,Action类可以访问数据源,比如: javax.sql.DataSource ds = servlet.findDataSource(“conPool”); javax.sql.Connection con = ds.getConnection();

ActionServlet


框架中的控制器组件是有org.apache.struts.action.ActionServlet类实现的,这个类是javax.servlet.http.HttpServlet类的扩展。Struts controller基本功能是:
1. 截获用户的Http请求
2. 把这个请求映射到相应的Action类,如果这是此类收到的第一
个请求,将初始化实例并缓寸。
3. 创建或发现一个ActionForm bean实例(看配置文件是否定
义),然后将请求过程移植到bean.
4. 调用Action实例的perform()方法并将ActioForm bean,
Action Mapping对象,request和response对象传给它。
如:public ActionForword perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
5.perform返回一个ActionForword对象,此对象连接到相应的jsp
页面.

ActionServlet配置


我们需要在web.xml中声明ActionServlet,并且将它配置成启动时进行加载。以下为可以配置的初始化参数:

参数


默认值


描述


application

null

应用程序的资源集合的类

bufferSize

4096

文件上传的缓冲区大小

config

/WEB-INF/struts-config.xml

配置文件的位置和名称

content

Text/html

默认的内容类型

debug

0

程序调试的级别

detail

0

程序调试细节的级别

factory

null

消息资源工厂,用于国际化中解释消息资源

formBean

org.apache.struts.action.ActionFormBean

封装ActionForm bean信息的类的名称

forward

org.apache.struts.action.ActionForward

封装ActionForward对象信息的类的名称

locale

true

为true,将在用户会话中存储一个本地对象

mapping

org.apache.struts.action.ActionForward

封装ActionMapping信息的类的名称

maxFileSize

250M

上传文件的最大尺寸

multipartClass

org.apache.struts.action.ActionForward

处理多部分请求的类的名称

noCache

False

HTTP标头是否要设置为禁止缓寸

null

True

设置为true,对于无效的信息关键字将返回null

tempDir

作为一个servlet参数提供给程序的工作目录

处理下载文件是使用的临时工作目录

validate

True

是否使用新格式的配置文件

vallidating

True

是否对配置文件进行有效性分析

大多数情况下,标准的servlet就能够满足用户需要。

第一次收到特定请求的URI时,ActionServlet将适当的Action类进行实例化,然后ActionServlet在Action类实例中以servlet为变量名存储一个引用。当被实例化后,Action类会被暂存以备再用。

ActionServlet也提供一些方法,由Action类用来访问数据源和转发目标之类的资源。

ActionServlet方法


ActinServlet提供了一组能够被Action对象使用的方法。

Struts API的全部信息在struts-documentation.war中可以找到。

动态的添加或删除,这些方法只影响应用程序当前的实例:

public void addFormBean(ActionFormBean formBean)

public void removeFormBean(ActionFormBean formBean)

public void addForward(ActionForward actionForward)

public void removeForward(ActionForward actionForward)

public void addMapping(ActionMapping actionMapping)

public void removeMapping(ActionMapping actionMapping)

根据名称查找对象:

public ActionFormBean findFormBean(String name)

public ActionForward findForward(String name)

public ActionMapping findMapping(String name)

用来处理数据源:

public void addDataSource(String key , DataSource ds)

public DataSource findDataSource(String key)

我们还可以:

l 使用destroy()方法结束ActionServlet

l 使用reload()方法从struts配置文件将信息重新加载。

ActionMapping


将特定请求映射到特定Action的相关信息存储在ActionMapping中,ActionServelt将ActionMapping传送到Action类的perform()方法,Action将使用ActionMapping的findForward()方法,此方法返回一个指定名称的ActionForward,这样Action就完成了本地转发。若没有找到具体的ActionForward,就返回一个null.

public ActionForward findForward(String name)

可在映射中动态添加ActionForward:

public void addForward(ActionForward forward)

可返回与映射关联的表单bean:

public String getName()

可返回映射的属性域(会话或请求)

public String getScope()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: