欢迎来到Spring Jsf集成教程。JSF是一个基于组件的框架,非常关注用户界面。而Spring框架的核心原则是依赖注入. 因此,将JSF与Spring框架集成是有意义的,其中JSF将用于用户界面,Spring框架将用于后端服务器端业务逻辑。
Spring JSF
Springs可以通过与JSF集成DelegatingVariableResolver
. Let’s now see how to integrate Spring JSF frameworks with an example.
- 在中添加以下spring依赖项pom.xml文件以及标准JSF依赖项。Spring核心和web依赖性是基于MVC的web应用程序的最低要求。
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency>
- 添加授权可变分解器在里面
faces-config.xml
file as shown below. Hereel-resolver
is the delegating variable resolver.<application> <el-resolver> org.springframework.web.jsf.el.SpringBeanFacesELResolver </el-resolver> </application>
el resolver提到
SpringBeanFacesELResolver
connects the JSF前端的值Spring后端服务层。 - 在中输入以下条目
web.xml
to add listeners provided by spring framework as;<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener>
listener类将spring框架入口点绑定到servlet上下文。
让我们看看JSF项目中的java配置。
- 创建托管bean类
CarBean.java
aspackage com.journaldev.jsfspring; import java.util.List; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component @ManagedBean @SessionScoped public class CarBean { @Autowired CarDao carDao; public void setCarDao(CarDao carDao) { this.carDao = carDao; } public List<String> fetchCarDetails() { return carDao.getCarDetails(); } }
- 创建接口
CarDao.java
aspackage com.journaldev.jsfspring; import java.util.List; public interface CarDao { public List<String> getCarDetails(); }
- 创建实现类
CarImpl.java
aspackage com.journaldev.jsfspring; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Service; @Service public class CarImpl implements CarDao { @Override public List<String> getCarDetails() { List<String> cars = new ArrayList<String>(); cars.add(0, "Santro"); cars.add(1, "Zen"); cars.add(2, "Alto"); cars.add(3, "Qualis"); cars.add(4, "Innova"); for (String c : cars) { System.out.println(c); } return cars; } }
注意,Spring注解用于服务和连接是由@Autowired注解完成的。我们也可以使用Spring Bean配置文件来实现这些,我们将在后面的小节中看到。
让我们在JSF中创建视图页面。
- 创建JSF页面
car.xhtml
as<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="https://www.w3.org/1999/xhtml" xmlns:h="https://java.sun.com/jsf/html" xmlns:ui="https://java.sun.com/jsf/facelets"> <h:head> <title>JSF Spring Integration</title> </h:head> <h:body> <h2>Car Names List</h2> <ul> <ui:repeat var="cars" value="#{carBean.fetchCarDetails()}"> <li><h3>#{cars}</h3></li> </ui:repeat> </ul> </h:body> </html>
- 在中添加基本包详细信息
applicationContext.xml
to scan for service classes.<beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:context="https://www.springframework.org/schema/context" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.1.xsd https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-3.1.xsd"> <context:component-scan base-package="com.journaldev.jsfspring" /> <!-- If you prefer XML based Dependency Injection remove annotation from class and uncomment below configuration --> <!-- <bean id="carDAO" /> <bean id="carBean"> <property name="carDao" ref="carDAO"></property> </bean> --> </beans>
注意上面的注解代码,如果您更喜欢基于XML的配置,那么可以从java类中删除Spring注解并取消注解。你会得到同样的结果。
Spring Jsf示例测试
我们的应用程序已经准备好了,只需将它部署到您最喜欢的servlet容器中并运行它。你应该得到低于输出响应。
下图显示了Eclipse中的最终项目结构。