posts - 309, comments - 6940, trackbacks - 0, articles - 3
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Spring+JSF示例

Posted on 2007-11-13 23:14 诗特林 阅读(5368) 评论(7)  编辑  收藏 所属分类: JSF
 

Spring+JSF示例

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>

 

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>

 

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>

 

 

二、       后台处理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";
        }

    }

}


    

三、       配置文件

1WEB-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> 

 

注意我使用中文注释的地方。

 

2WEB-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>
 

 3WEB-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>

 


 

参考文献:http://www.javabeat.net/articles/2007/10/integrating-spring-and-jsf/


评论

# re: Spring+JSF示例[未登录]  回复  更多评论   

2007-11-14 08:56 by 阿蜜果
蛮好的例子,多谢了,LZ

# re: Spring+JSF示例  回复  更多评论   

2007-11-14 13:17 by BeanSoft
不错! 收藏了!

# re: Spring+JSF示例  回复  更多评论   

2007-12-13 20:27 by OpenBund
有沒有連接mysql進行查詢並顯示數據的代碼

# re: Spring+JSF示例  回复  更多评论   

2008-06-25 09:56 by ddd
我从你的代码好像找不到spring管理bean的地方

# re: Spring+JSF示例  回复  更多评论   

2008-08-29 15:28 by young_suse
在spring配置文件里没有指定Bean的有效范围request

# re: Spring+JSF示例  回复  更多评论   

2011-06-29 20:22 by zlu
写的不错

# re: Spring+JSF示例  回复  更多评论   

2014-08-29 10:50 by junjielee
faces-config.xml没有managed-bean???

只有注册用户登录后才能发表评论。


网站导航: