《struts2权威指南》学习笔记之struts2整合jsf

本文介绍如何整合Struts2、MyFaces和Spring框架,包括配置步骤、核心文件修改及示例代码展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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-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/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信息 -->
< beans xmlns ="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" >

< 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

<? xmlversion="1.0"encoding="GBK" ?>
<! DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.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
... {
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

package model;


public class Book
... {
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

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
... {
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: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

<% ... @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: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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值