启天JAVA培训社区's Archiver

junsan 发表于 2008-9-7 12:45

Struts 2整合MyFaces

要在Struts框架中整合MyFaces实现,只需要将相关的pluglin插件和相关类库复制到Web应用的lib目录下,增加一些配置内容就可以完整支持MyFaces。

[size=3][font=宋体]下面介绍[/font][font=Times New Roman]Struts 2[/font][font=宋体]框架整合[/font][font=Times New Roman]MyFaces[/font][font=宋体]的详细步骤。[/font][/size]
[size=3][font=宋体]([/font][font=Times New Roman]1[/font][font=宋体])复制[/font][font=Times New Roman]MyFaces[/font][font=宋体]类库。将下载后的[/font][font=Times New Roman]MyFaces[/font][font=宋体]压缩包解压,复制[/font][font=Times New Roman]lib[/font][font=宋体]目录下的所有[/font][font=Times New Roman]jar[/font][font=宋体]文件到[/font][font=Times New Roman]Web[/font][font=宋体]应用的[/font][font=Times New Roman]WEB-INF/lib[/font][font=宋体]目录下。[/font][/size]
[size=3][font=宋体]([/font][font=Times New Roman]2[/font][font=宋体])复制[/font][font=Times New Roman]MyFaces[/font][font=宋体]插件。[/font][font=Times New Roman]Struts 2[/font][font=宋体]框架下载包的[/font][font=Times New Roman]lib[/font][font=宋体]目录下已经包含了[/font][font=Times New Roman]JSF[/font][font=宋体]的插件,文件名称为[/font][font=Times New Roman]struts2-jsf-plugin-x.x.x.jar[/font][font=宋体],[/font][font=Times New Roman]x[/font][font=宋体]为相应的版本号。需要将该文件复制到[/font][font=Times New Roman]Web[/font][font=宋体]应用的[/font][font=Times New Roman]WEB-INF/lib[/font][font=宋体]目录下。[/font][/size]
[size=3][font=宋体]([/font][font=Times New Roman]3[/font][font=宋体])增加[/font][font=Times New Roman]web.xml[/font][font=宋体]内容。为了整合[/font][font=Times New Roman]Myfaces[/font][font=宋体],需要在[/font][font=Times New Roman]web.xml[/font][font=宋体]文件中增加如下内容:[/font][/size]
[size=2][font=Courier New]<!--[/font][font=宋体]配置[/font][font=Courier New]Myfaces-->[/font][/size]
[size=2][font=Courier New]
<listener>[/font][/size]
[size=2][font=Courier New]
<listener-class>[/font][/size]
[size=2][font=Courier New]
org.apache.myfaces.webapp.StartupServletContextListener[/font][/size]
[size=2][font=Courier New]
</listener-class>[/font][/size]
[size=2][font=Courier New]
</listener>[/font][/size]
[size=2][font=Courier New]
<!-- JavaServer Faces Servlet [/font][font=宋体]配置[/font][font=Courier New]-->[/font][/size]
[size=2][font=Courier New]
<servlet>[/font][/size]
[size=2][font=Courier New]
<servlet-name>faces</servlet-name>[/font][/size]
[size=2][font=Courier New]
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>[/font][/size]
[size=2][font=Courier New]
<load-on-startup>1</load-on-startup>[/font][/size]
[size=2][font=Courier New]
</servlet>[/font][/size]
[size=2][font=Courier New]
<!-- JavaServer Faces Servlet Mapping[/font][font=宋体]配置[/font][font=Courier New] -->[/font][/size]
[size=2][font=Courier New]
<servlet-mapping>[/font][/size]
[size=2][font=Courier New]
<servlet-name>faces</servlet-name>[/font][/size]
[size=2][font=Courier New]
<url-pattern>*.action</url-pattern>[/font][/size]
[size=2][font=Courier New]
</servlet-mapping>[/font][/size]
[size=3][font=宋体]([/font][font=Times New Roman]4[/font][font=宋体])导入[/font][font=Times New Roman]MyFaces[/font][font=宋体]标签库。为了在[/font][font=Times New Roman]JSP[/font][font=宋体]视图中使用[/font][font=Times New Roman]MyFaces[/font][font=宋体]标签,需要在[/font][font=Times New Roman]JSP[/font][font=宋体]文件中使用如下代码来导入[/font][font=Times New Roman]MyFaces[/font][font=宋体]标签库:[/font][/size]
[size=2][font=Courier New]<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>[/font][/size]
[size=2][font=Courier New]<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>[/font][/size]
[size=3][font=宋体]([/font][font=Times New Roman]5[/font][font=宋体])引用[/font][font=Times New Roman]JSF[/font][font=宋体]拦截器。还需要在[/font][font=Times New Roman]Struts 2[/font][font=宋体]框架的[/font][font=Times New Roman]Action[/font][font=宋体]配置文件中引用[/font][font=Times New Roman]JSF[/font][font=宋体]拦截器,该拦截器默认名称为[/font][font=Times New Roman]jsfStack[/font][font=宋体],一个典型的引用拦截器的包配置如下:[/font][/size]
[size=2][font=Courier New]<!--[/font][font=宋体]定义一个[/font][font=Courier New]jsf[/font][font=宋体]包,该包继承[/font][font=Courier New]jsf-default[/font][font=宋体],[/font][font=Courier New]jsf-default[/font][font=宋体]在[/font][font=Courier New]struts-plugin.xml[/font][font=宋体]中定义[/font][font=Courier New]-->[/font][/size]
[size=2][font=Courier New]
<package name="jsf" extends="jsf-default">[/font][/size]
[size=2][font=Courier New]
<interceptors>[/font][/size]
[size=2][font=Courier New]
<!--[/font][font=宋体]自定义拦截器栈[/font][font=Courier New]-->[/font][/size]
[size=2][font=Courier New]
<interceptor-stack name="jsfFullStack">[/font][/size]
[size=2][font=Courier New]
<interceptor-ref name="params" />[/font][/size]
[size=2][font=Courier New]
<interceptor-ref name="basicStack" />[/font][/size]
[size=2][font=Courier New]
<!--jsf[/font][font=宋体]拦截器引用,见[/font][font=Courier New]struts-plugin.xml[/font][font=宋体]中定义[/font][font=Courier New]-->[/font][/size]
[size=2][font=Courier New]
<interceptor-ref name="jsfStack" />[/font][/size]
[size=2][font=Courier New]
</interceptor-stack>[/font][/size]
[size=2][font=Courier New]
</interceptors>[/font][/size]
[size=2][font=Courier New]
<!--[/font][font=宋体]设置为默认拦截器栈[/font][font=Courier New]-->[/font][/size]
[size=2][font=Courier New]
<default-interceptor-ref name="jsfFullStack" />[/font][/size]
[size=2][font=Courier New]
</package>[/font][/size]
[size=2][font=Courier New]
<!--[/font][font=宋体]继承[/font][font=Courier New]jsf[/font][font=宋体]包,同时继承其默认拦截器,不需要再引用该拦截器[/font][font=Courier New]-->[/font][/size]
[size=2][font=Courier New]
<package name="ch14" extends="jsf" namespace="/ch14">[/font][/size]
[size=2][font=Courier New]
<action name="list" class="ch14.UserAction">[/font][/size]
[size=2][font=Courier New]
… …[/font][/size]
[size=2][font=Courier New]
</action>[/font][/size]
[size=2][font=Courier New]
… …[/font][/size]
[size=2][font=Courier New]
</package>[/font][/size]

junsan 发表于 2008-9-7 12:46

[size=3][font=Times New Roman]Struts 2[/font][font=宋体]框架整合[/font][font=Times New Roman]JSF[/font][font=宋体]的关键在于[/font][font=Times New Roman]Struts 2[/font][font=宋体]的[/font][font=Times New Roman]plugin[/font][font=宋体]插件[/font][font=Times New Roman]struts2-jsf-plugin-2.0.11.jar[/font][font=宋体],该插件压缩包的结构如图[/font][font=Times New Roman]14.4[/font][font=宋体]所示。[/font][/size]
[size=3]
[img]http://p.blog.csdn.net/images/p_blog_csdn_net/Struts2BBS/14-10.jpg[/img][/size]
[size=3][/size]
[font=宋体]图[/font][font=Times New Roman]14.4
struts2-jsf-plugin-2.0.11.jar[/font][font=宋体]插件目录[/font]
[size=3][font=宋体]该目录下的[/font][font=Times New Roman]struts-plugin.xml[/font][font=宋体]文件就是插件的配置文件,该文件内容如代码[/font][font=Times New Roman]14.1[/font][font=宋体]所示。[/font][/size]
[size=3][font=黑体]代码[/font]14.1
struts-plugin.xml[font=黑体]配置文件[/font][/size]

[size=2][font=Courier New] <!DOCTYPE struts PUBLIC[/font][/size]
[size=2][font=Courier New]
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"[/font][/size]
[size=2][font=Courier New]
"http://struts.apache.org/dtds/struts-2.0.dtd">[/font][/size]
[size=2][font=Courier New]
<struts>[/font][/size]
[size=2][font=Courier New]<!--
[/font][font=宋体]定义[/font][font=Courier New]jsf-default [/font][font=宋体]包[/font][font=Courier New] -->[/font][/size]
[size=2][font=Courier New]
<package name="jsf-default" extends="struts-default">[/font][/size]
[size=2][font=Courier New]
<!—[/font][font=宋体]定义结果类型[/font][font=Courier New]-->[/font][/size]
[size=2][font=Courier New]
<result-types>[/font][/size]
[size=2][font=Courier New]
<result-type name="jsf" class="org.apache.struts2.jsf.FacesResult" />[/font][/size]
[size=2][font=Courier New]
</result-types>[/font][/size]
[size=2][font=Courier New]
<interceptors>[/font][/size]
[size=2][font=Courier New]<interceptor class="org.apache.struts2.jsf.FacesSetupInterceptor"[/font][/size]
[size=2][font=Courier New]

name="jsfSetup" />[/font][/size]
[size=2][font=Courier New]<interceptor class="org.apache.struts2.jsf.RestoreViewInterceptor"[/font][/size]
[size=2][font=Courier New]

name="jsfRestore" />[/font][/size]
[size=2][font=Courier New]<interceptor class="org.apache.struts2.jsf.ApplyRequestValuesInterceptor"[/font][/size]
[size=2][font=Courier New]

name="jsfApply" />[/font][/size]
[size=2][font=Courier New]<interceptor class="org.apache.struts2.jsf.ProcessValidationsInterceptor"[/font][/size]
[size=2][font=Courier New]

name="jsfValidate" />[/font][/size]
[size=2][font=Courier New]<interceptor class="org.apache.struts2.jsf.UpdateModelValuesInterceptor"[/font][/size]
[size=2][font=Courier New]

name="jsfUpdate" />[/font][/size]
[size=2][font=Courier New]<interceptor class="org.apache.struts2.jsf.InvokeApplicationInterceptor"[/font][/size]
[size=2][font=Courier New]

name="jsfInvoke" />[/font][/size]
[size=2][font=Courier New]
<interceptor-stack name="jsfStack">[/font][/size]
[size=2][font=Courier New]
<interceptor-ref name="jsfSetup">[/font][/size]
[size=2][font=Courier New]


<param name="variableResolver">[/font][/size]
[size=2][font=Courier New]


org.apache.struts2.jsf.StrutsVariableResolver</param>[/font][/size]
[size=2][font=Courier New]


<param name="navigationHandler">[/font][/size]
[size=2][font=Courier New]


org.apache.struts2.jsf.StrutsNavigationHandler</param>[/font][/size]
[size=2][font=Courier New]
</interceptor-ref>[/font][/size]
[size=2][font=Courier New]
<interceptor-ref name="jsfRestore" />[/font][/size]
[size=2][font=Courier New]

<interceptor-ref name="jsfApply" />[/font][/size]
[size=2][font=Courier New]
<interceptor-ref name="jsfValidate" />[/font][/size]
[size=2][font=Courier New]
<interceptor-ref name="jsfUpdate" />[/font][/size]
[size=2][font=Courier New]
<interceptor-ref name="jsfInvoke" />[/font][/size]
[size=2][font=Courier New]
</interceptor-stack>[/font][/size]
[size=2][font=Courier New]
</interceptors>[/font][/size]
[size=2][font=Courier New]
//[/font][font=宋体]定义默认拦截器栈[/font][/size]
[size=2][font=Courier New]
<default-interceptor-ref name="jsfStack"/>[/font][/size]
[size=2][font=Courier New]
</package>[/font][/size]
[size=2][font=Courier New]</struts>[/font][/size]
[size=3][font=宋体]该配置文件定义定义了包[/font][font=Times New Roman]jsf-default[/font][font=宋体],该包中定义了一个名为[/font][font=Times New Roman]jsf[/font][font=宋体]的结果类型,同时定义了一系列相关的拦截器,并将这些拦截器组成一个拦截器栈[/font][font=Times New Roman]jsfStack[/font][font=宋体],最后设置[/font][font=Times New Roman]jsfStack[/font][font=宋体]作为包的默认拦截器。[/font][/size]
[size=3][/size][font=宋体][size=11pt]★[/size][/font][size=11pt][font=Times New Roman] [/font][/size][font=黑体][size=11pt]注意[/size][/font][size=11pt][font=Times New Roman] [/font][/size][font=宋体][size=11pt]★[/size][/font][size=11pt][/size]
[size=3][font=Times New Roman][font=方正楷体简体]开发者在配置[/font]Struts 2[font=方正楷体简体]框架的[/font]Action[font=方正楷体简体]时,需要继承该[/font]jsf-default[font=方正楷体简体]包,就可以同时继承[/font]jsf[font=方正楷体简体]结果类型和默认的拦截器栈。[/font][/font][/size]
[size=3][font=Times New Roman]MyFaces[/font][font=宋体]的标签库文件在类库[/font][font=Times New Roman]myfaces-impl-x.x.x.jar[/font][font=宋体]文件中,读者可以打开该压缩包,在[/font][font=Times New Roman]META-INF[/font][font=宋体]目录下有两个标签库文件:[/font][font=Times New Roman]myfaces_core.tld[/font][font=宋体]和[/font][font=Times New Roman]myfaces_html.tld[/font][font=宋体],开发者要在[/font][font=Times New Roman]JSP[/font][font=宋体]页面中使用这两个标签库,则必须首先在页面中导入,导入代码如下所示:[/font][/size]
[size=2][font=Courier New]<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>[/font][/size]
[size=2][font=Courier New]<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>[/font][/size]
[size=3][font=宋体]代码中的[/font][font=Times New Roman]uri[/font][font=宋体]为标签库中的指定值,例如在[/font][font=Times New Roman]myfaces_core.tld[/font][font=宋体]文件中,有如下内容:[/font][/size]
[size=2][font=Courier New]… …[/font][/size]
[size=2][font=Courier New]<taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor">[/font][/size]
[size=2][font=Courier New]
<tlib-version>1.0</tlib-version>[/font][/size]
[size=2][font=Courier New]
<jsp-version>1.2</jsp-version>[/font][/size]
[size=2][font=Courier New]
<short-name>f</short-name>[/font][/size]
[size=2][font=Courier New]
<uri>[url]http://java.sun.com/jsf/core&lt[/url];/uri>[/font][/size]
[size=2][font=Courier New]
<display-name>JSF core tag library.</display-name>[/font][/size]
[size=2][font=Courier New]
<description>[/font][/size]
[size=2][font=Courier New]
This tag library implements the standard JSF core tags.[/font][/size]
[size=2][font=Courier New]</description>[/font][/size]
[size=2][font=Courier New]… …[/font][/size]
[size=3][font=宋体]读者可以看到,该标签库文件中的[/font][font=Times New Roman]uri[/font][font=宋体]定义同[/font][font=Times New Roman]JSP[/font][font=宋体]导入相同。[/font][/size]
[size=3][/size][font=宋体][size=11pt]★[/size][/font][size=11pt][font=Times New Roman] [/font][/size][font=黑体][size=11pt]说明[/size][/font][size=11pt][font=Times New Roman] [/font][/size][font=宋体][size=11pt]★[/size][/font][size=11pt][/size]
[size=3][font=Times New Roman][font=方正楷体简体]要熟练使用[/font]MyFaces[font=方正楷体简体]的标签库,可以参考相关文档。[/font][/font][/size]

页: [1]

Powered by Discuz! Archiver 6.1.0F  © 2001-2007 Comsenz Inc.