1.安装jsf插件
为了在struts2应用中使用JSF组件(实际上就是MyFaces组件,因为Myfaces是jsf的一个实现,必须将myfaces的lib路径下的jar文件都复制到web应用的WEB-INF/lib下)
2.将struts2框架下的struts2-jsf-plugin-2.06.jar复制到WEB-INF/lib下
3.修改web.xml文件,增加MYFaces的支持
web.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appid="jsf"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/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
<!--JavaServerFacesServletConfiguration,notuseddirectly-->
<servlet>
<servlet-name>faces</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!--JavaServerFacesServletMapping,notcalleddirectly-->
<servlet-mapping>
<servlet-name>faces</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.xml
<?xmlversion="1.0"encoding="GBK"?>
<!--指定Spring配置文件的Schema信息-->
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
<beanid="bs"class="service.BookService"/>
</beans>
我们在spring配置文件中没有定义action的bean,所以,我们采用自动装配的方式将该业务逻辑注入到action实例中,为了让struts2使用spring框架,我们需要加入struts2-spring-plugin-2.0.6.jar到WEB-INF/lib中
并在struts.properties做如下定义
struts.i18n.encoding=gb2312
struts.objectFactory.spring.autoWire=type
struts.xml
<?xmlversion="1.0"encoding="GBK"?>
<!DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constantname="struts.custom.i18n.resources"value="messageResource"/>
<constantname="struts.i18n.encoding"value="GBK"/>
<packagename="jsf"extends="jsf-default">
<interceptors>
<interceptor-stackname="jsfFullStack">
<interceptor-refname="params"/>
<interceptor-refname="basicStack"/>
<interceptor-refname="jsfStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-refname="jsfFullStack"/>
</package>
<packagename="lee"extends="jsf">
<actionname="list"class="action.BookAction">
<resultname="success"type="jsf"/>
</action>
<actionname="edit"class="action.BookAction">
<resultname="success"type="jsf"/>
<resultname="list"type="redirect">list.action</result>
</action>
</package>
</struts>
BookSevice
packageservice;
importjava.util.HashSet;
importjava.util.Set;
importmodel.Book;
publicclassBookService
...{
privateSet<Book>bookDb;
publicBookService()
...{
bookDb=newHashSet<Book>();
bookDb.add(newBook(1,"Spring2.0宝典","全面介绍了Spring各个知识点"));
bookDb.add(newBook(2,"轻量级J2EE企业应用实战","介绍实际企业的J2EE开发过程"));
}
publicSet<Book>getAllBook()
...{
returnbookDb;
}
publicBookgetBookById(intid)
...{
for(Bookb:bookDb)
...{
if(b.getId()==id)
...{
returnb;
}
}
returnnull;
}
publicvoidaddBook(Bookb)
...{
bookDb.add(b);
}
}
Book
packagemodel;
publicclassBook
...{
privateintid;
privateStringname;
privateStringdesc;
publicBook()
...{
}
publicBook(intid,Stringname,Stringdesc)
...{
this.id=id;
this.name=name;
this.desc=desc;
}
publicvoidsetId(intid)
...{
this.id=id;
}
publicintgetId()
...{
returnthis.id;
}
publicvoidsetName(Stringname)
...{
this.name=name;
}
publicStringgetName()
...{
returnthis.name;
}
publicvoidsetDesc(Stringdesc)
...{
this.desc=desc;
}
publicStringgetDesc()
...{
returnthis.desc;
}
publicinthashCode()
...{
returnid;
}
publicbooleanequals(Objecttarget)
...{
if(targetinstanceofBook)
...{
Bookb=(Book)target;
if(b.getId()==this.id)
...{
returntrue;
}
}
returnfalse;
}
}
BookAction
packageaction;
importjava.util.ArrayList;
importjava.util.List;
importmodel.Book;
importservice.BookService;
importcom.opensymphony.xwork2.ActionSupport;
publicclassBookActionextendsActionSupport
...{
privateBookcurrentBook;
privateinteditId;
privateBookServicebs;
publicvoidsetBs(BookServicebs)
...{
this.bs=bs;
}
publicvoidsetCurrentBook(BookcurrentBook)
...{
this.currentBook=currentBook;
}
publicBookgetCurrentBook()
...{
//如果editId请求参数不为空,则currentBook也不为空
if(editId!=0)
...{
this.currentBook=bs.getBookById(editId);
}
elseif(currentBook==null)
...{
currentBook=newBook();
}
returnthis.currentBook;
}
publicvoidsetEditId(inteditId)
...{
this.editId=editId;
}
publicintgetEditId()
...{
returnthis.editId;
}
publicList<Book>getAllBook()
...{
List<Book>result=newArrayList<Book>();
for(Bookb:bs.getAllBook())
...{
result.add(b);
}
returnresult;
}
publicStringsave()
...{
bs.addBook(currentBook);
return"list";
}
}
list.jsp
<%...@pagelanguage="java"contentType="text/html;charset=GBK"%>
<%...@taglibprefix="f"uri="http://java.sun.com/jsf/core"%>
<%...@taglibprefix="h"uri="http://java.sun.com/jsf/html"%>
<html>
<head>
<title>Struts2+MyFaces+Spring整合</title>
</head>
<body>
<f:view>
<h3>Struts2+MyFaces+Spring整合</h3>
<h3>列出所有图书</h3>
<h:dataTablevalue="#{action.allBook}"var="b"style="text-align:center;width:500px"border="1">
<h:column>
<f:facetname="header">
<h:outputTextvalue="图书ID"/>
</f:facet>
<h:outputLinkvalue="edit.action">
<f:paramname="editId"value="#{b.id}"/>
<h:outputTextvalue="#{b.id}"/>
</h:outputLink>
</h:column>
<h:column>
<f:facetname="header">
<h:outputTextvalue="图书名"/>
</f:facet>
<h:outputTextvalue="#{b.name}"/>
</h:column>
<h:column>
<f:facetname="header">
<h:outputTextvalue="图书简介"/>
</f:facet>
<h:outputTextvalue="#{b.desc}"/>
</h:column>
</h:dataTable>
<p>
<h:outputLinkvalue="edit.action">
<h:outputTextvalue="新增图书"/>
</h:outputLink>
</p>
</f:view>
</body>
</html>
edit.jsp
<%...@pagelanguage="java"contentType="text/html;charset=GBK"%>
<%...@taglibprefix="f"uri="http://java.sun.com/jsf/core"%>
<%...@taglibprefix="h"uri="http://java.sun.com/jsf/html"%>
<html>
<head>
<title>Struts2+MyFaces+Spring整合</title>
</head>
<body>
<f:view>
<h3>Struts2+MyFaces+Spring整合</h3>
<h3>修改/保存图书</h3>
<h:form>
<h:inputHiddenvalue="#{action.editId}"/>
<h:panelGridcolumns="3">
<h:outputTextvalue="图书ID"/>
<h:inputTextid="id"size="5"value="#{action.currentBook.id}"required="true"/>
<h:messagefor="id"/>
<h:outputTextvalue="图书名:"/>
<h:inputTextid="name"size="30"value="#{action.currentBook.name}"required="true">
<f:validateLengthminimum="2"maximum="100"/>
</h:inputText>
<h:messagefor="name"/>
<h:outputTextvalue="图书描述:"/>
<h:inputTextid="desc"size="30"value="#{action.currentBook.desc}"required="true">
<f:validateLengthminimum="2"maximum="100"/>
</h:inputText>
<h:messagefor="desc"/>
</h:panelGrid>
<h:commandButtonvalue="保存"action="#{action.save}"/>
<br/>
</h:form>
</f:view>
</body>
</html>
如果context为test,则运行哦哪个http://localhost:8080/test/list.action进行测试,不能直接运行list.jsp
版权声明:本文为博主原创文章,未经博主允许不得转载。
分享到:
相关推荐
林信良学习笔记(内容):ajax学习笔记,算法学习笔记,计算机图形学,C++学习笔记,设计模式,Hibernate学习笔记,Java学习笔记,JSF,JSP/Servlet,JUnit,Spring,Struts等
JavaEE5学习笔记13-JSF集成Facelets使用经验总结.
JavaEE5学习笔记12-JSF集成AJAX使用经验总结。
SSH包括java,C++,算法,Window界面组建,JSF,Struts,Spring,Hibernate,JSPServlet,JUint,JavaC++从初学到高手的学习方法等经典极品不容错过!!
JavaEE5学习笔记11-JSF转换器验证器使用经验总结。
一分整理的学习笔记 里边包括了jsp,servlet,ejb,jsf,oracle,ajax,core jave struts,spring等各个方面的学习记录
常見程式演算,電腦圖學入門,設計模式,C 語言,GTK,C++,Qt3,Qt4,,Java (上),Java (下),JSP/Servlet,JSF,Ajax,JUnit,Struts,Spring,Hibernate,Acegi
是一个全方位的应用程序框架(Application framework),除此之外,对于现存的各种框架(Struts、JSF、Hibernate 等),Spring 也提供了与它们相整合的方案。 本质上讲,Spring是IOC(Inversion of Control)和面向...
Appfuse是Matt Raible 开发的一个指导性的入门级J2EE框架,它对如何集成流行的Spring、Hibernate、iBatis、Struts、xDcolet、Junit、Taperstry、JSF等基础框架给出了示范。在持久层,AppFuse采用了Hibernate O/R映射...
Appfuse是Matt Raible 开发的一个指导性的入门级J2EE框架,它对如何集成流行的Spring、Hibernate、iBatis、Struts、xDcolet、Junit、Taperstry、JSF等基础框架给出了示范。在持久层,AppFuse采用了Hibernate O/R映射...
short 16bit, -2^15~2^15-1 (2^15=32768) int 32bit, -2^31~2^31-1 (2147483648,20亿,10位有效数字) long 64bit, -2^63~2^63-1 (900亿亿,20位有效数字) float 32bit, 9位有效数字,含小数(四舍五入)(小数点算...
内附hibernate学习笔记、J2EE全实例教程、JSF中文教程、java设计模式、jsf中文教程、xml指南、struts2.0中文帮助手册 等实用文档,相信是你学习j2ee的好帮手。。
Ant使用指南.pdf cvs.pdf Eclipse入门-- Eclipse的使用简介及插件开发.PDF eclipse插件开发.doc Eclipse插件开发系列.pdf Hibernate_3.2.0_Reference_zh_CN.chm Hibernate_DEV_GUIDE.pdf javaAnnotation.doc java_...