JSF 本身提供了强大的 Bean 管理功能。但 Spring 作为一种轻量的容器,在管理 Bean方面有着不可替代的优势,使用起来很方便 。在 Spring 流行的今天,怎么能少了在 JSF 中整合 Spring 呢?下面的示例比较简单,但也能清楚的表达如何将 JSF+Spring 两者互相整合起来。
整个示例,通过输入页面输入特定的字符串,在后面查找此字符串对应的数值,然后展现给客户,如果没有找到则提示错误。
一、??????
页面
?
1.输入页面:
stockInput.jsp
<%
@page?contentType
=
"
text/html
"
%>
<% @page?pageEncoding = " UTF-8 " %>
<% @taglib?prefix = " f " ?uri = " http://java.sun.com/jsf/core " %>
<% @taglib?prefix = " h " ?uri = " http://java.sun.com/jsf/html " %>
< f:view >
???? < html >
???????? < head >
???????????? < title > 字符输入页面 </ title >
???????? </ head >
???????? < body >
???????????? < h:form?id = " stockForm " >
???????????????? < h1 >
????????????????????请输入一个字符串,如ABC或DEF或GHI或JKL
???????????????? </ h1 >
???????????????? < p >
???????????????????? < h:inputText?id = " stockSymbolInput " ?value = " #{stockBean.symbolName} "
????????????????????????required = " true " >
???????????????????? </ h:inputText >
???????????????? </ p >
???????????????? < h:commandButton?id = " stockSubmit " ?type = " submit " ?value = " 提交 "
????????????????????action = " #{stockBean.findStockValue} " >
???????????????? </ h:commandButton >
???????????? </ h:form >
???????? </ body >
???? </ html >
</ f:view >
<% @page?pageEncoding = " UTF-8 " %>
<% @taglib?prefix = " f " ?uri = " http://java.sun.com/jsf/core " %>
<% @taglib?prefix = " h " ?uri = " http://java.sun.com/jsf/html " %>
< f:view >
???? < html >
???????? < head >
???????????? < title > 字符输入页面 </ title >
???????? </ head >
???????? < body >
???????????? < h:form?id = " stockForm " >
???????????????? < h1 >
????????????????????请输入一个字符串,如ABC或DEF或GHI或JKL
???????????????? </ h1 >
???????????????? < p >
???????????????????? < h:inputText?id = " stockSymbolInput " ?value = " #{stockBean.symbolName} "
????????????????????????required = " true " >
???????????????????? </ h:inputText >
???????????????? </ p >
???????????????? < h:commandButton?id = " stockSubmit " ?type = " submit " ?value = " 提交 "
????????????????????action = " #{stockBean.findStockValue} " >
???????????????? </ h:commandButton >
???????????? </ h:form >
???????? </ body >
???? </ html >
</ f:view >
?
2.输出页面:stockOutputFailure.jsp
<%
@page?contentType
=
"
text/html
"
%>
<% @page?pageEncoding = " UTF-8 " %>
<% @taglib?prefix = " f " ?uri = " http://java.sun.com/jsf/core " %>
<% @taglib?prefix = " h " ?uri = " http://java.sun.com/jsf/html " %>
<! DOCTYPE?HTML?PUBLIC? " -//W3C//DTD?HTML?4.01?Transitional//EN "
" http://www.w3.org/TR/html4/loose.dtd " >
< html >
???? < head >
???????? < meta?http - equiv = " Content-Type " ?content = " text/html;?charset=UTF-8 " >
???????? < title > 取得字符串所对应的值 </ title >
???? </ head >
???? < body >
???????? < f:view >
???????????? < h1 >
????????????????所输入的字符串
???????????????? < h:outputText?value = " #{stockBean.symbolName} " >
???????????????? </ h:outputText >
????????????????对应的值是
???????????????? < h:outputText?value = " #{stockBean.symbolValue} " >
???????????????? </ h:outputText >
???????????? </ h1 >
???????? </ f:view >
???? </ body >
</ html >
<% @page?pageEncoding = " UTF-8 " %>
<% @taglib?prefix = " f " ?uri = " http://java.sun.com/jsf/core " %>
<% @taglib?prefix = " h " ?uri = " http://java.sun.com/jsf/html " %>
<! DOCTYPE?HTML?PUBLIC? " -//W3C//DTD?HTML?4.01?Transitional//EN "
" http://www.w3.org/TR/html4/loose.dtd " >
< html >
???? < head >
???????? < meta?http - equiv = " Content-Type " ?content = " text/html;?charset=UTF-8 " >
???????? < title > 取得字符串所对应的值 </ title >
???? </ head >
???? < body >
???????? < f:view >
???????????? < h1 >
????????????????所输入的字符串
???????????????? < h:outputText?value = " #{stockBean.symbolName} " >
???????????????? </ h:outputText >
????????????????对应的值是
???????????????? < h:outputText?value = " #{stockBean.symbolValue} " >
???????????????? </ h:outputText >
???????????? </ h1 >
???????? </ f:view >
???? </ body >
</ html >
?
3 .错误处理页面: stockOutputFailure.jsp
<%
@page?contentType
=
"
text/html
"
%>
<% @page?pageEncoding = " UTF-8 " %>
<% @taglib?prefix = " f " ?uri = " http://java.sun.com/jsf/core " %>
<% @taglib?prefix = " h " ?uri = " http://java.sun.com/jsf/html " %>
<! DOCTYPE?HTML?PUBLIC? " -//W3C//DTD?HTML?4.01?Transitional//EN "
" http://www.w3.org/TR/html4/loose.dtd " >
< html >
???? < head >
???????? < meta?http - equiv = " Content-Type " ?content = " text/html;?charset=UTF-8 " >
???????? < title > 错误页面 </ title >
???? </ head >
???? < body >
???????? < f:view >
???????????? < h1 >
????????????????所查找的字符串
???????????????? < h:outputText?value = " #{stockBean.symbolName} " >
???????????????? </ h:outputText >
????????????????不存在,请再次查找 !
???????????? </ h1 >
???????? </ f:view >
???? </ body >
</ html >
<% @page?pageEncoding = " UTF-8 " %>
<% @taglib?prefix = " f " ?uri = " http://java.sun.com/jsf/core " %>
<% @taglib?prefix = " h " ?uri = " http://java.sun.com/jsf/html " %>
<! DOCTYPE?HTML?PUBLIC? " -//W3C//DTD?HTML?4.01?Transitional//EN "
" http://www.w3.org/TR/html4/loose.dtd " >
< html >
???? < head >
???????? < meta?http - equiv = " Content-Type " ?content = " text/html;?charset=UTF-8 " >
???????? < title > 错误页面 </ title >
???? </ head >
???? < body >
???????? < f:view >
???????????? < h1 >
????????????????所查找的字符串
???????????????? < h:outputText?value = " #{stockBean.symbolName} " >
???????????????? </ h:outputText >
????????????????不存在,请再次查找 !
???????????? </ h1 >
???????? </ f:view >
???? </ body >
</ html >
?
?二、?????? 后台处理 Bean
? StockValueFetcher.java
package
?com.sterning.springjsf;
import ?java.util. * ;
public ? class ?StockValueFetcher? {
???? private ?Map < String,?String > ?stockSymbolsAndValues;
???? private ?String?symbolName;
???? private ?String?symbolValue;
???? public ?StockValueFetcher()? {
????????stockSymbolsAndValues? = ? new ?HashMap < String,?String > ();
????????stockSymbolsAndValues.put( " ABC " ,? " 10 " );
????????stockSymbolsAndValues.put( " DEF " ,? " 20 " );
????????stockSymbolsAndValues.put( " GHI " ,? " 30 " );
????????stockSymbolsAndValues.put( " JKL " ,? " 40 " );
????}
???? public ?String?getSymbolName()? {
???????? return ?symbolName;
????}
???? public ? void ?setSymbolName(String?symbolName)? {
???????? this .symbolName? = ?symbolName;
????}
???? public ?String?getSymbolValue()? {
???????? return ?symbolValue;
????}
???? public ? void ?setSymbolValue(String?symbolValue)? {
???????? this .symbolValue? = ?symbolValue;
????}
???? public ?String?findStockValue() {
???????? boolean ?symbolFound? = ?stockSymbolsAndValues.containsKey(symbolName);
???????? if ?(symbolFound) {
????????????symbolValue? = ?stockSymbolsAndValues.get(symbolName);
???????????? return ? " stockOutputSuccess " ;
????????} else {
???????????? return ? " stockOutputFailure " ;
????????}
????}
}
import ?java.util. * ;
public ? class ?StockValueFetcher? {
???? private ?Map < String,?String > ?stockSymbolsAndValues;
???? private ?String?symbolName;
???? private ?String?symbolValue;
???? public ?StockValueFetcher()? {
????????stockSymbolsAndValues? = ? new ?HashMap < String,?String > ();
????????stockSymbolsAndValues.put( " ABC " ,? " 10 " );
????????stockSymbolsAndValues.put( " DEF " ,? " 20 " );
????????stockSymbolsAndValues.put( " GHI " ,? " 30 " );
????????stockSymbolsAndValues.put( " JKL " ,? " 40 " );
????}
???? public ?String?getSymbolName()? {
???????? return ?symbolName;
????}
???? public ? void ?setSymbolName(String?symbolName)? {
???????? this .symbolName? = ?symbolName;
????}
???? public ?String?getSymbolValue()? {
???????? return ?symbolValue;
????}
???? public ? void ?setSymbolValue(String?symbolValue)? {
???????? this .symbolValue? = ?symbolValue;
????}
???? public ?String?findStockValue() {
???????? boolean ?symbolFound? = ?stockSymbolsAndValues.containsKey(symbolName);
???????? if ?(symbolFound) {
????????????symbolValue? = ?stockSymbolsAndValues.get(symbolName);
???????????? return ? " stockOutputSuccess " ;
????????} else {
???????????? return ? " stockOutputFailure " ;
????????}
????}
}
三、??????
配置文件
????
1 . WEB-INF/faces-config.xml
<?
xml?version='1.0'?encoding='UTF-8'
?>
< faces-config? version ="1.2" ?
xmlns ="http://java.sun.com/xml/ns/javaee" ?
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" ?
xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee?
http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd" >
< application >
???? < variable-resolver >
????????org.springframework.web.jsf.DelegatingVariableResolver
???? </ variable-resolver >
</ application >
<!-- ??下面的代码在没有使用Spring时起一样的作用
<managed-bean>
????<managed-bean-name>stockBean</managed-bean-name>
????????<managed-bean-class>
????????????com.sterning.springjsf.StockValueFetcher
????????</managed-bean-class>
????<managed-bean-scope>request</managed-bean-scope>????
</managed-bean>
-->
< navigation-rule >
???? < description > Navigation?from?the?hello?page. </ description >
???? < from-view-id > /stockInput.jsp </ from-view-id >
???? < navigation-case >
???????? < from-outcome > stockOutputSuccess </ from-outcome >
???????? < to-view-id > /stockOutputSuccess.jsp </ to-view-id >
???? </ navigation-case >
???? < navigation-case >
???????? < from-outcome > stockOutputFailure </ from-outcome >
???????? < to-view-id > /stockOutputFailure.jsp </ to-view-id >
???? </ navigation-case > ????
</ navigation-rule >
</ faces-config > ?
< faces-config? version ="1.2" ?
xmlns ="http://java.sun.com/xml/ns/javaee" ?
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" ?
xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee?
http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd" >
< application >
???? < variable-resolver >
????????org.springframework.web.jsf.DelegatingVariableResolver
???? </ variable-resolver >
</ application >
<!-- ??下面的代码在没有使用Spring时起一样的作用
<managed-bean>
????<managed-bean-name>stockBean</managed-bean-name>
????????<managed-bean-class>
????????????com.sterning.springjsf.StockValueFetcher
????????</managed-bean-class>
????<managed-bean-scope>request</managed-bean-scope>????
</managed-bean>
-->
< navigation-rule >
???? < description > Navigation?from?the?hello?page. </ description >
???? < from-view-id > /stockInput.jsp </ from-view-id >
???? < navigation-case >
???????? < from-outcome > stockOutputSuccess </ from-outcome >
???????? < to-view-id > /stockOutputSuccess.jsp </ to-view-id >
???? </ navigation-case >
???? < navigation-case >
???????? < from-outcome > stockOutputFailure </ from-outcome >
???????? < to-view-id > /stockOutputFailure.jsp </ to-view-id >
???? </ navigation-case > ????
</ navigation-rule >
</ faces-config > ?
?
?注意我使用中文注释的地方。
?
2 . WEB-INF/applicationContext.xml
<?
xml?version="1.0"?encoding="UTF-8"
?>
<! DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean? id ="stockBean"
class ="com.sterning.springjsf.StockValueFetcher" >
</ bean >
?
<! DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean? id ="stockBean"
class ="com.sterning.springjsf.StockValueFetcher" >
</ bean >
? 3 . WEB-INF/web.xml
<?
xml?version="1.0"?encoding="UTF-8"
?>
< web-app? version ="2.5" ?xmlns ="http://java.sun.com/xml/ns/javaee"
????xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
????xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee?
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
???? < listener >
???????? < listener-class >
????????????org.springframework.web.context.ContextLoaderListener
???????? </ listener-class >
???? </ listener >
???? < context-param >
???????? < param-name > contextConfigLocation </ param-name >
???????? < param-value > /WEB-INF/applicationContext.xml </ param-value >
???? </ context-param >
???? < servlet >
???????? < servlet-name > Faces?Servlet </ servlet-name >
???????? < servlet-class > javax.faces.webapp.FacesServlet </ servlet-class >
???????? < load-on-startup > 1 </ load-on-startup >
???? </ servlet >
???? < servlet-mapping >
???????? < servlet-name > Faces?Servlet </ servlet-name >
???????? < url-pattern > /faces/* </ url-pattern >
???? </ servlet-mapping >
???? < session-config >
???????? < session-timeout > 30 </ session-timeout >
???? </ session-config >
???? < welcome-file-list >
???????? < welcome-file > faces/stockInput.jsp </ welcome-file >
???? </ welcome-file-list >
</ web-app >
< web-app? version ="2.5" ?xmlns ="http://java.sun.com/xml/ns/javaee"
????xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
????xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee?
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
???? < listener >
???????? < listener-class >
????????????org.springframework.web.context.ContextLoaderListener
???????? </ listener-class >
???? </ listener >
???? < context-param >
???????? < param-name > contextConfigLocation </ param-name >
???????? < param-value > /WEB-INF/applicationContext.xml </ param-value >
???? </ context-param >
???? < servlet >
???????? < servlet-name > Faces?Servlet </ servlet-name >
???????? < servlet-class > javax.faces.webapp.FacesServlet </ servlet-class >
???????? < load-on-startup > 1 </ load-on-startup >
???? </ servlet >
???? < servlet-mapping >
???????? < servlet-name > Faces?Servlet </ servlet-name >
???????? < url-pattern > /faces/* </ url-pattern >
???? </ servlet-mapping >
???? < session-config >
???????? < session-timeout > 30 </ session-timeout >
???? </ session-config >
???? < welcome-file-list >
???????? < welcome-file > faces/stockInput.jsp </ welcome-file >
???? </ welcome-file-list >
</ web-app >
?
?
参考文献: http://www.javabeat.net/articles/2007/10/integrating-spring-and-jsf/