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 {@AutowiredCarDao 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 {@Overridepublic 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/beanshttps://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttps://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context-3.1.xsd"><context:component-scan base-package="com.journaldev.jsfspring" /><!-- If you prefer XML based Dependency Injectionremove 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.

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

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

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

Download JSF Spring Integration Example Project下载JSF Spring Integration示例项目

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

jsf集成spring

jsf集成spring_Spring JSF集成相关推荐

  1. jsf集成spring_Spring和JSF集成:MVC螺母和螺栓

    jsf集成spring 过去,我曾尝试将JSF与Spring MVC集成在一起,尽管我的第一次尝试成功了,但这远非理想. 这次,我决定做出一些关键决定来帮助我集中精力: 向后兼容. 支持JSF 1.2 ...

  2. 1.5 Hello, world! 解剖 -JSF实战 -hxzon -jsf学习笔记

    为什么80%的码农都做不了架构师?>>>    1.5 Hello, world! 解剖 -JSF实战 -hxzon -jsf学习笔记 既然已经对JSF能够解决什么问题有了初步理解, ...

  3. 集成学习-Boosting集成学习算法GBDT

    GBDT算法的核心为:先构造一个(决策)树,然后不断在已有模型和实际样本输出的残差上再构造一颗树,依次迭代. 目录 Decistion Tree(决策树) Gradient Boosting(梯度提升 ...

  4. JSF范围教程– JSF / CDI会话范围

    会话作用域跨越多个HTTP请求-响应周期(理论上是无限的). 当您需要每个HTTP请求-响应周期进行一次交互时,请求作用域在任何Web应用程序中都非常有用. 但是,当您需要对属于用户会话的任何HTTP ...

  5. 【intellij idea 高级用法之:集成JIRA、UML类图插件、集成SSH、集成FTP、Database管理 】

    intellij idea 高级用法之:集成JIRA.UML类图插件.集成SSH.集成FTP.Database管理 - 菩提树下的杨过 - 博客园

  6. 企业如何数据集成?数据集成解决方案

    随着信息化时代的不断推进,企业对于数据集成解决问题的需求越发强烈,越来越多的应用场景对数据集成的诉求也越来越高.零售行业.传统销售企业.项目管理等,都离不开企业数据的共享和企业数据集成,但企业数据现状 ...

  7. 数据集成-5-批数据集成

    批数据集成 1. 批数据集成简介 1.1. 批数据集成 批数据集成 静态数据集 数据被组织成"批"地(时间窗口) 周期性的迁移到另一个系统(专门的数据端) 抽取.转换.集成为通用数 ...

  8. 系统集成方式:数据集成、控制集成

    为了完成控制集成和业务流程集成,必须首先解决数据和数据库的集成问题.在集成之前,必须首先对数据进行标识并编成目录,另外还要确定元数据模型,保证数据在数据库系统中分布和共享. 通常在以下情况下,将会使用 ...

  9. onlyoffice二次开发集成、onlyoffic集成

    onlyoffice二次开发集成.onlyoffic集成 支持功能 新增word,excel,ppt文档 在线多用户协同编辑文档 实时通讯 批注等功能 下面是demo的功能截图(架构:springbo ...

最新文章

  1. android system 分区大小,Android System分区大小异常
  2. 海贼王热血航线正在连接服务器,《航海王热血航线》无法进入原因和解决方法 进不去如何解决...
  3. 1、Expect 远程登录linux系统
  4. [测试]将TestCase整合
  5. 数据中台实战(四):商品分析(产品设计篇)
  6. Android BLE学习(三):编写自己的 BLE蓝牙读写工具(功能仿照nrf master control panel)
  7. Struts学习笔记_i18n
  8. Eclipse 答疑:Eclipse 如何设置 Java 代码自动提示和自动补全?
  9. centos树莓派安装mysql_树莓派3B+安装CentOS7
  10. 如何确定autosar的版本_什么是AUTOSAR?AUTOSAR的概要、背景以及目的-汽车电子-与非网...
  11. Discuz! X3.0/X3.1/X3.2通用 Apache伪静态规则
  12. c++程序添加资源文件及释放文件
  13. java webservice调用sap_java调用sap webservice
  14. Python if else elif
  15. python绘制emoji_使用Emoji表情拼成汉字
  16. 软件工程—实践者的研究方法
  17. 银河麒麟桌面操作系统V10 SP1 如何安装字体
  18. 毕业设计-基于机器视觉的车型识别系统
  19. 随手查_python
  20. 13700k和13700kf的区别 i7 13700k和13700kf怎么选

热门文章

  1. oracle函数listagg的使用说明(分组后连接字段)
  2. nginx做代理上网
  3. [转载] real和imag在python_Python numpy.imag() 使用实例
  4. 面向对象及软件工程——团队作业3
  5. 【LeetCode】28. Implement strStr()
  6. POJ_2456_Agressive_cows_(二分,最大化最小值)
  7. 性能强悍的开源关系数据库PostgreSQL
  8. DotNetBar 中Ribbon汉化
  9. 算法学习_简单递归算法
  10. 访问iis元数据库失败怎么解决