您的位置:首页 > 编程语言 > Java开发

JSF2.0与Spring3集成

2011-11-20 15:26 531 查看
同以前的JSF1.2与Spring2.5集成类似,只是有一些类名和默认配置的变化。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext*.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

<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>*.jsf</url-pattern>
</servlet-mapping>
</web-app>

Faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude" 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_2_0.xsd"> 
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
JSF1.2和1.2以前是加入

<variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>


Spring
的配置文件就正常配置就可以了

ApplicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 
<!-- 自动扫描组件 -->
<context:component-scan base-package="org.gui.jsf20" />
</beans>


服务层组件:

package org.gui.jsf20.service.impl;

import org.gui.jsf20.service.HelloService;
import org.springframework.stereotype.Service;

@Service("helloService")
public class HelloServiceImpl implements HelloService {

public HelloServiceImpl() {
System.out.println("Init helloService bean...");
}

public void sayHello(String words) {
System.out.println("You Say: " + words);
}

}


Backing Bean:

package org.gui.jsf20.form;

import javax.annotation.Resource;

import org.gui.jsf20.service.HelloService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Controller("helloBean") @Scope("session")
public class HelloBean {
private String words;
private String result;

@Resource(name="helloService")
private HelloService helloService;

// Actions
public void doSayWords() {
helloService.sayHello(words);
result = "Are you Say \"" + words + "\"";
}

// Setters & Getters
public String getWords() {
return words;
}

public void setWords(String words) {
this.words = words;
}

public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}


页面:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core" xml:lang="en" lang="en">
<head>
<title>My Facelets Page</title>
<meta http-equiv="keywords" content="enter,your,keywords,here" />
<meta http-equiv="description"
content="A short description of this page." />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

<!--<link rel="stylesheet" type="text/css" href="styles.css">-->
</head>
<body>
<h:form>
Param: <h:outputText value="#{param['id']}"></h:outputText><br/>
Worlds:<h:inputText value="#{helloBean.words}"></h:inputText><br/>
Result:<h:outputText value="#{helloBean.result}"></h:outputText><br/>
<h:commandButton value="Say Words" actionListener="#{helloBean.doSayWords}"></h:commandButton>
</h:form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: