jsf集成spring_Spring JSF集成

jsf集成spring

Welcome to Spring JSF integration tutorial. JSF is a component based framework with great focus on user interfaces. Whereas Spring framework core principle is Dependency Injection. So it makes sense to integrate JSF with Spring framework where JSF will be used for user interfaces and Spring framework will be used for backend server side business logic.

欢迎使用Spring JSF集成教程。 JSF是一个基于组件的框架,非常注重用户界面。 Spring框架的核心原理是依赖注入 。 因此,将JSF与Spring框架集成是有意义的,其中JSF将用于用户界面,而Spring框架将用于后端服务器端业务逻辑。

SpringJSF (Spring JSF)

Springs can be integrated with JSF through DelegatingVariableResolver. Let’s now see how to integrate Spring JSF frameworks with an example.

可以通过DelegatingVariableResolver将Spring与JSF集成。 现在让我们看一下如何将Spring JSF框架与示例集成在一起。

  1. Add the following spring dependencies in pom.xml along with the standard JSF dependencies. Spring core and web dependencies are a minimum requirement for MVC based web application.
    <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>

    在pom.xml中添加以下spring依赖项以及标准JSF依赖项。 Spring核心和Web依赖关系是基于MVC的Web应用程序的最低要求。
  2. Add the DelegatingVariableResolver in faces-config.xml file as shown below. Here el-resolver is the delegating variable resolver.
    <application>
        <el-resolver>
            org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
    </application>

    The el-resolver mentioning SpringBeanFacesELResolver connects the JSF front end values to the Spring backend service layer.

    faces-config.xml文件中添加DelegatingVariableResolver 。 这里el-resolver是委托变量解析​​器。

    el-resolver提到了SpringBeanFacesELResolverJSF前端值连接到Spring后端服务层。

  3. Make the following entry in 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>

    The listener class ties the spring framework entry points to the servlet context.

    web.xml进行以下输入,以将spring框架提供的侦听器添加为:

    侦听器类将spring框架入口指向servlet上下文。

Our configuration files are ready, let’s look at the java classes for our Spring JSF example project.

我们的配置文件已经准备好,让我们来看一下Spring JSF示例项目的java类。

  1. Create the managed bean class CarBean.java as
    package 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();
    	}
    
    }

    创建托管Bean类CarBean.java
  2. Create the interface CarDao.java as
    package com.journaldev.jsfspring;
    
    import java.util.List;
    
    public interface CarDao {
    
    	public List<String> getCarDetails();
    
    }

    创建接口CarDao.java
  3. Create the implementation class CarImpl.java as
    package 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;
    
    	}
    
    }

    Notice the use of Spring annotations for service and wiring is done by @Autowired annotation. We can do these using Spring Bean configuration file too, we will see that in later sections.

    CarImpl.java作为

    注意,使用Spring注释进行服务和接线是通过@Autowired注释完成的。 我们也可以使用Spring Bean配置文件来完成这些操作,我们将在后面的部分中看到。

Let’s create the view page in JSF.

让我们在JSF中创建视图页面。

  1. Create the JSF page 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>

    创建JSF页面car.xhtml
  2. Add the base package details in 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 class="com.journaldev.jsfspring.CarImpl" id="carDAO" />
    	
    	<bean class="com.journaldev.jsfspring.CarBean" id="carBean">
    		<property name="carDao" ref="carDAO"></property>
    	</bean>
    	 -->
    	 
    </beans>

    Notice the commented code above, if you prefer XML based configuration then you can remove Spring annotations from java classes and uncomment these. You will get the same results.

    applicationContext.xml添加基本​​软件包详细信息以扫描服务类。

    请注意上面的注释代码,如果您更喜欢基于XML的配置,则可以从Java类中删除Spring注释并取消注释。 您将获得相同的结果。

Spring JSF示例测试 (Spring JSF Example Test)

Our application is ready, just deploy it in your favorite servlet container and run it. You should get below output response.

我们的应用程序已准备就绪,只需将其部署在您喜欢的servlet容器中并运行即可。 您应该获得以下输出响应。

Below image shows the final project structure in Eclipse.

Spring JSF Integration

下图显示了Eclipse中的最终项目结构。

You can download the project from below link and play around with it to learn more.

您可以从下面的链接下载该项目并进行试用以了解更多信息。

翻译自: https://www.journaldev.com/7112/spring-jsf-integration

jsf集成spring

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值