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
<?
xml version="1.0" encoding="UTF-8"
?>
< web-app id ="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/j2ee http://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 >
<!-- JavaServer Faces Servlet Configuration, not used directly -->
< servlet >
< servlet-name > faces </ servlet-name >
< servlet-class > javax.faces.webapp.FacesServlet </ servlet-class >
< load-on-startup > 1 </ load-on-startup >
</ servlet >
<!-- JavaServer Faces Servlet Mapping, not called directly -->
< servlet-mapping >
< servlet-name > faces </ servlet-name >
< url-pattern > *.action </ url-pattern >
</ servlet-mapping >
</ web-app >
< web-app id ="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/j2ee http://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 >
<!-- JavaServer Faces Servlet Configuration, not used directly -->
< servlet >
< servlet-name > faces </ servlet-name >
< servlet-class > javax.faces.webapp.FacesServlet </ servlet-class >
< load-on-startup > 1 </ load-on-startup >
</ servlet >
<!-- JavaServer Faces Servlet Mapping, not called directly -->
< servlet-mapping >
< servlet-name > faces </ servlet-name >
< url-pattern > *.action </ url-pattern >
</ servlet-mapping >
</ web-app >
applicationContext.xml
<?
xml version="1.0" encoding="GBK"
?>
<!-- 指定Spring配置文件的Schema信息 -->
< beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >
< bean id ="bs" class ="service.BookService" />
</ beans >
<!-- 指定Spring配置文件的Schema信息 -->
< beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >
< bean id ="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
<?
xml version="1.0" encoding="GBK"
?>
<! DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< constant name ="struts.custom.i18n.resources" value ="messageResource" />
< constant name ="struts.i18n.encoding" value ="GBK" />
< package name ="jsf" extends ="jsf-default" >
< interceptors >
< interceptor-stack name ="jsfFullStack" >
< interceptor-ref name ="params" />
< interceptor-ref name ="basicStack" />
< interceptor-ref name ="jsfStack" />
</ interceptor-stack >
</ interceptors >
< default-interceptor-ref name ="jsfFullStack" />
</ package >
< package name ="lee" extends ="jsf" >
< action name ="list" class ="action.BookAction" >
< result name ="success" type ="jsf" />
</ action >
< action name ="edit" class ="action.BookAction" >
< result name ="success" type ="jsf" />
< result name ="list" type ="redirect" > list.action </ result >
</ action >
</ package >
</ struts >
<! DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< constant name ="struts.custom.i18n.resources" value ="messageResource" />
< constant name ="struts.i18n.encoding" value ="GBK" />
< package name ="jsf" extends ="jsf-default" >
< interceptors >
< interceptor-stack name ="jsfFullStack" >
< interceptor-ref name ="params" />
< interceptor-ref name ="basicStack" />
< interceptor-ref name ="jsfStack" />
</ interceptor-stack >
</ interceptors >
< default-interceptor-ref name ="jsfFullStack" />
</ package >
< package name ="lee" extends ="jsf" >
< action name ="list" class ="action.BookAction" >
< result name ="success" type ="jsf" />
</ action >
< action name ="edit" class ="action.BookAction" >
< result name ="success" type ="jsf" />
< result name ="list" type ="redirect" > list.action </ result >
</ action >
</ package >
</ struts >
BookSevice
package
service;
import java.util.HashSet;
import java.util.Set;
import model.Book;
public class BookService
... {
private Set<Book> bookDb;
public BookService()
...{
bookDb = new HashSet<Book>();
bookDb.add(new Book(1 , "Spring2.0宝典" , "全面介绍了Spring各个知识点"));
bookDb.add(new Book(2 , "轻量级J2EE企业应用实战" , "介绍实际企业的J2EE开发过程"));
}
public Set<Book> getAllBook()
...{
return bookDb;
}
public Book getBookById(int id)
...{
for (Book b : bookDb)
...{
if (b.getId() == id)
...{
return b;
}
}
return null;
}
public void addBook(Book b)
...{
bookDb.add(b);
}
}
import java.util.HashSet;
import java.util.Set;
import model.Book;
public class BookService
... {
private Set<Book> bookDb;
public BookService()
...{
bookDb = new HashSet<Book>();
bookDb.add(new Book(1 , "Spring2.0宝典" , "全面介绍了Spring各个知识点"));
bookDb.add(new Book(2 , "轻量级J2EE企业应用实战" , "介绍实际企业的J2EE开发过程"));
}
public Set<Book> getAllBook()
...{
return bookDb;
}
public Book getBookById(int id)
...{
for (Book b : bookDb)
...{
if (b.getId() == id)
...{
return b;
}
}
return null;
}
public void addBook(Book b)
...{
bookDb.add(b);
}
}
Book
package
model;
public class Book
... {
private int id;
private String name;
private String desc;
public Book()
...{
}
public Book(int id , String name ,String desc)
...{
this.id = id;
this.name = name;
this.desc = desc;
}
public void setId(int id)
...{
this.id = id;
}
public int getId()
...{
return this.id;
}
public void setName(String name)
...{
this.name = name;
}
public String getName()
...{
return this.name;
}
public void setDesc(String desc)
...{
this.desc = desc;
}
public String getDesc()
...{
return this.desc;
}
public int hashCode()
...{
return id;
}
public boolean equals(Object target)
...{
if (target instanceof Book)
...{
Book b = (Book)target;
if (b.getId() == this.id)
...{
return true;
}
}
return false;
}
}
public class Book
... {
private int id;
private String name;
private String desc;
public Book()
...{
}
public Book(int id , String name ,String desc)
...{
this.id = id;
this.name = name;
this.desc = desc;
}
public void setId(int id)
...{
this.id = id;
}
public int getId()
...{
return this.id;
}
public void setName(String name)
...{
this.name = name;
}
public String getName()
...{
return this.name;
}
public void setDesc(String desc)
...{
this.desc = desc;
}
public String getDesc()
...{
return this.desc;
}
public int hashCode()
...{
return id;
}
public boolean equals(Object target)
...{
if (target instanceof Book)
...{
Book b = (Book)target;
if (b.getId() == this.id)
...{
return true;
}
}
return false;
}
}
BookAction
package
action;
import java.util.ArrayList;
import java.util.List;
import model.Book;
import service.BookService;
import com.opensymphony.xwork2.ActionSupport;
public class BookAction extends ActionSupport
... {
private Book currentBook;
private int editId;
private BookService bs;
public void setBs(BookService bs)
...{
this.bs = bs;
}
public void setCurrentBook(Book currentBook)
...{
this.currentBook = currentBook;
}
public Book getCurrentBook()
...{
//如果editId请求参数不为空,则currentBook也不为空
if (editId != 0)
...{
this.currentBook = bs.getBookById(editId);
}
else if (currentBook == null)
...{
currentBook = new Book();
}
return this.currentBook;
}
public void setEditId(int editId)
...{
this.editId = editId;
}
public int getEditId()
...{
return this.editId;
}
public List<Book> getAllBook()
...{
List<Book> result = new ArrayList<Book>();
for (Book b : bs.getAllBook())
...{
result.add(b);
}
return result;
}
public String save()
...{
bs.addBook(currentBook);
return "list";
}
}
import java.util.ArrayList;
import java.util.List;
import model.Book;
import service.BookService;
import com.opensymphony.xwork2.ActionSupport;
public class BookAction extends ActionSupport
... {
private Book currentBook;
private int editId;
private BookService bs;
public void setBs(BookService bs)
...{
this.bs = bs;
}
public void setCurrentBook(Book currentBook)
...{
this.currentBook = currentBook;
}
public Book getCurrentBook()
...{
//如果editId请求参数不为空,则currentBook也不为空
if (editId != 0)
...{
this.currentBook = bs.getBookById(editId);
}
else if (currentBook == null)
...{
currentBook = new Book();
}
return this.currentBook;
}
public void setEditId(int editId)
...{
this.editId = editId;
}
public int getEditId()
...{
return this.editId;
}
public List<Book> getAllBook()
...{
List<Book> result = new ArrayList<Book>();
for (Book b : bs.getAllBook())
...{
result.add(b);
}
return result;
}
public String save()
...{
bs.addBook(currentBook);
return "list";
}
}
list.jsp
<%
...
@ page language="java" contentType="text/html; charset=GBK"
%>
<% ... @ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<% ... @ taglib prefix="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:dataTable value ="#{action.allBook}" var ="b" style ="text-align:center;width:500px" border ="1" >
< h:column >
< f:facet name ="header" >
< h:outputText value ="图书ID" />
</ f:facet >
< h:outputLink value ="edit.action" >
< f:param name ="editId" value ="#{b.id}" />
< h:outputText value ="#{b.id}" />
</ h:outputLink >
</ h:column >
< h:column >
< f:facet name ="header" >
< h:outputText value ="图书名" />
</ f:facet >
< h:outputText value ="#{b.name}" />
</ h:column >
< h:column >
< f:facet name ="header" >
< h:outputText value ="图书简介" />
</ f:facet >
< h:outputText value ="#{b.desc}" />
</ h:column >
</ h:dataTable >
< p >
< h:outputLink value ="edit.action" >
< h:outputText value ="新增图书" />
</ h:outputLink >
</ p >
</ f:view >
</ body >
</ html >
<% ... @ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<% ... @ taglib prefix="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:dataTable value ="#{action.allBook}" var ="b" style ="text-align:center;width:500px" border ="1" >
< h:column >
< f:facet name ="header" >
< h:outputText value ="图书ID" />
</ f:facet >
< h:outputLink value ="edit.action" >
< f:param name ="editId" value ="#{b.id}" />
< h:outputText value ="#{b.id}" />
</ h:outputLink >
</ h:column >
< h:column >
< f:facet name ="header" >
< h:outputText value ="图书名" />
</ f:facet >
< h:outputText value ="#{b.name}" />
</ h:column >
< h:column >
< f:facet name ="header" >
< h:outputText value ="图书简介" />
</ f:facet >
< h:outputText value ="#{b.desc}" />
</ h:column >
</ h:dataTable >
< p >
< h:outputLink value ="edit.action" >
< h:outputText value ="新增图书" />
</ h:outputLink >
</ p >
</ f:view >
</ body >
</ html >
edit.jsp
<%
...
@ page language="java" contentType="text/html; charset=GBK"
%>
<% ... @ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<% ... @ taglib prefix="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:inputHidden value ="#{action.editId}" />
< h:panelGrid columns ="3" >
< h:outputText value ="图书ID" />
< h:inputText id ="id" size ="5" value ="#{action.currentBook.id}" required ="true" />
< h:message for ="id" />
< h:outputText value ="图书名:" />
< h:inputText id ="name" size ="30" value ="#{action.currentBook.name}" required ="true" >
< f:validateLength minimum ="2" maximum ="100" />
</ h:inputText >
< h:message for ="name" />
< h:outputText value ="图书描述:" />
< h:inputText id ="desc" size ="30" value ="#{action.currentBook.desc}" required ="true" >
< f:validateLength minimum ="2" maximum ="100" />
</ h:inputText >
< h:message for ="desc" />
</ h:panelGrid >
< h:commandButton value ="保存" action ="#{action.save}" />
< br />
</ h:form >
</ f:view >
</ body >
</ html >
<% ... @ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<% ... @ taglib prefix="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:inputHidden value ="#{action.editId}" />
< h:panelGrid columns ="3" >
< h:outputText value ="图书ID" />
< h:inputText id ="id" size ="5" value ="#{action.currentBook.id}" required ="true" />
< h:message for ="id" />
< h:outputText value ="图书名:" />
< h:inputText id ="name" size ="30" value ="#{action.currentBook.name}" required ="true" >
< f:validateLength minimum ="2" maximum ="100" />
</ h:inputText >
< h:message for ="name" />
< h:outputText value ="图书描述:" />
< h:inputText id ="desc" size ="30" value ="#{action.currentBook.desc}" required ="true" >
< f:validateLength minimum ="2" maximum ="100" />
</ h:inputText >
< h:message for ="desc" />
</ h:panelGrid >
< h:commandButton value ="保存" action ="#{action.save}" />
< br />
</ h:form >
</ f:view >
</ body >
</ html >
如果context为test,则运行哦哪个http://localhost:8080/test/list.action进行测试,不能直接运行list.jsp