实战spring+jsf
JSF本身提供了强大的Bean管理功能。但Spring作为一种轻量的容器,在管理Bean方面有着不可替代的优势,使用起来很方便。在Spring流行的今天,怎么能少了在JSF中整合Spring呢?下面的示例比较简单,但也能清楚的表达如何将JSF+Spring两者互相整合起来。
首先新建web项目myJSF,然后需要找到下图的jar包导入到WEN-INF/lib下:
可以到http://download.youkuaiyun.com/my去下载一个JSF+SPRING的zip包,解压后就可以用了!
第一步:创建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>
<action-listener>
org.springframework.web.jsf.DelegatingVariableResolver
</action-listener>
</application>
<navigation-rule>
<description>Navigation from the hello page</description>
<from-view-id>/ScockInput.jsp</from-view-id>
<navigation-case>
<from-outcome>stockOutputSuccess</from-outcome>
<to-view-id>/StockOut.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>stockOutputFailure</from-outcome>
<to-view-id>/StockError.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
第二步:创建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>
</beans>
第三步:创建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>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/ScockInput.jsp</welcome-file>
</welcome-file-list>
</web-app>
第四步:创建ScockInput.jsp页面
代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
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>Insert title here</title>
</head>
<body>
<h:form id="stockForm">
<h1>请输入一个字符串,如:ABC\DEF</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>
第五步:创建StockOut.jsp
代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<html>
<head>
<title>Insert title here</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>
第六步:创建Stockerror.jsp
代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<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>
第七步:创建StockValueFetcher
代码:
package com.sterning.springjsf;
import java.util.HashMap;
import java.util.Map;
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("GHJ", "10");
}
public Map<String, String> getStockSymbolsAndValues() {
return stockSymbolsAndValues;
}
public void setStockSymbolsAndValues(
Map<String, String> stockSymbolsAndValues) {
this.stockSymbolsAndValues = stockSymbolsAndValues;
}
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 "stockSymbolSuccess";
}else{
return "stockSymbolFailure";
}
}
}
写好了、自己调试下、希望大虾们多多指点!