jax_ws_带有JAX-RS和PrimeFaces的RESTful图表_cunfen8879的博客-程序员秘密

技术标签: spring  ViewUI  python  java  web  vue  

jax_ws

jax_ws

通常,利用图表提供数据的可视表示很有用。 PrimeFaces提供制图解决方案,使您可以轻松地将数据的可视表示形式添加到Web和移动应用程序中。 如果我们将PrimeFaces图表组件的使用与RESTful Web服务数据结合在一起,我们可以创建自定义图表,以适合台式机和移动设备。

在本文中,我将更新Java EE 7动手练习MoviePlex应用程序,以提供一个仪表板,我们可以在其中集成PrimeFaces图表组件。 在本示例中,我们将创建一个图表,但是您可以利用本文来帮助您以类似的方式构建更多图表。 具体来说,我们将利用RESTful Web服务收集电影院的容量信息,并使用PrimeFaces条形图显示每个剧院的容量。

首先,下载Java EE 7动手练习应用程序解决方案归档文件(如果尚未下载)。 从那里,在NetBeans IDE中打开它。 要创建这篇文章,我正在使用NetBeans 8.0.2。 将项目导入NetBeans后,通过右键单击项目并选择“运行”,将其部署到应用程序服务器(在我的情况下为GlasFish 4.1)。 部署完成后,通过打开以下URL在浏览器中打开Theater Web服务:http:// localhost:8080 / ExploringJavaEE7 / webresources / theater /。 该Web服务应该产生一个与图1类似的清单。

图1:Theater Web Service XML

图1:Theater Web Service XML

我们将利用此Web服务中的数据来提供仪表板小部件。 首先创建后端代码,然后处理UI。 首先,通过右键单击Source Packages,然后选择“ New…”->“ Java Packages”,创建一个名为org.glassfish.movieplex7.jsf的新软件包。 接下来,通过右键单击该包并选择“ New…”->“ JSF Managed Bean”,创建一个JSF Managed Bean控制器,并将其命名为DashboardController 。 让我们将控制器注释为@SessionScoped ,然后实现java.io.Serializable 。 在此控制器中,我们将获取数据,并为仪表板构建模型。 我们将首先使用JAX-RS客户端查询Web服务,然后将利用数据填充Theater对象的列表。 因此,我们需要定义以下四个字段来开始:

Client jaxRsClient;
// Typically not hard coded...store in a properties file or database
String baseUri = "http://localhost:8080/ExploringJavaEE7/webresources/theater/";
    
private List<Theater> theaterList;
private BarChartModel theaterCapacityModel;

Client类型为javax.ws.rs.client.Client ,我们将通过调用javax.ws.rs.client.ClientBuilder来初始化类构造函数中的字段,如下所示:

public DashboardController() {
    jaxRsClient = ClientBuilder.newClient();
}

接下来,我们需要创建一种方法来加载数据,创建和配置模型。 在我们的控制器中, init()方法基本上包含将任务委派给其他方法的实现。 init()方法的实现调用两个方法: loadData()createTheaterCapacityModel()

public void init() {
    loadData();
  
    createTheaterCapacityModel();
}

编写代码是为了方便日后向我们的仪表板添加更多小部件。 loadData()方法提供了将Web服务中的数据加载到本地列表中的实现。

private void loadData() {
       
    theaterList = jaxRsClient.target(baseUri)
            .request("application/xml")
            .get(new GenericType>() {
            }
            );
      
}

如果我们有更多的小部件,则还将这些数据模型的数据加载代码也添加到此方法中。 接下来,我们需要初始化我们定义的org.primefaces.model.chart.BarChartModel ,并使用来自Web服务的数据加载它。 initTheaterCapacityModel()方法包含用于创建BarChartModel的实现,并用一个或多个ChartSeries对象填充它以构建数据。

public BarChartModel initTheaterCapacityModel() {

    BarChartModel model = new BarChartModel();

    ChartSeries theaterCapacity = new ChartSeries();
    theaterCapacity.setLabel("Capacities");


    for (Theater theater : theaterList) {

        theaterCapacity.set(theater.getId(), theater.getCapacity());

    }
    model.addSeries(theaterCapacity);

    return model;
}

如您所见,该模型由单个org.primefaces.model.chart.ChartSeries对象组成。 实际上,模型可以包含多个ChartSeries对象,并且将使用不同的彩色条在图表中显示该数据。 在这种情况下,我们只需将剧院ID和每个Theater对象的容量添加到ChartSeries对象,然后将其添加到BarChartModel 。 在我们的init()方法中调用createTheaterCapacityModel()方法,并在其中调用initTheaterCapacityModel()方法来创建org.primefaces.model.chart.BarChartModel ,然后进行相应配置。

private void createTheaterCapacityModel() {
    theaterCapacityModel = initTheaterCapacityModel();

    theaterCapacityModel.setTitle("Theater Capacity");
    theaterCapacityModel.setLegendPosition("ne");
    theaterCapacityModel.setBarPadding(3);
    theaterCapacityModel.setShadow(false);

    Axis xAxis = theaterCapacityModel.getAxis(AxisType.X);
    xAxis.setLabel("Theater");

    Axis yAxis = theaterCapacityModel.getAxis(AxisType.Y);
    yAxis.setLabel("Capacity");
    yAxis.setMin(0);
    yAxis.setMax(200);

}

如您所见,在方法内部,我们通过调用initTheaterCapacityModel()初始化模型,然后通过一系列“设置”方法对其进行配置。 具体来说,我们设置标题,位置并提供一些视觉配置。 接下来,通过调用模型的getAxis()方法并传递X和Y轴常量来设置轴。 然后,通过为Y轴设置标签和最小/最大值来配置每个轴。 请参阅本文末尾的课程的完整资源。

服务器端代码就是这样做的,现在让我们看一下用于显示图表组件的UI代码。 首先,通过右键单击并选择"New..."-> "XHTML..." ,在项目的Web Pages文件夹的根目录处生成一个新的XHTML文件,并将文件命名为dashboard.xhtmldashboard.xhtml的源应包含以下内容:

<html xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:p="http://primefaces.org/ui" xmlns="http://www.w3.org/1999/xhtml">
    <h:head>
     
     
        <title>Theater Dashboard</title>

    </h:head>
    <h:body>
        <f:event listener="#{dashboardController.initView}" type="preRenderView"/>
        <h:form id="theaterDash" prependid="false">
            <p:growl id="growl" showdetail="true"/>
            <p:layout fullpage="true">
                <p:layoutUnit position="center">
                   
                            <p:panel header="Capacity for Theaters" id="theater_capacity" style="border: 0px;">
                                <p:chart model="#{dashboardController.theaterCapacityModel}" style="border: 0px; height: 200px; width: 500px;" type="bar">
                            </p:chart></p:panel>
                         
                 
                </p:layoutUnit>
            </p:layout>

            <p:poll interval="60" listener="#{dashboardController.pollData}"/>

        </h:form>

    </h:body>
</html>

相当简单,JSF视图包含一个PrimeFaces布局,包括一个面板和一个图表。 在视图顶部附近,使用f:event标记来调用侦听器方法,该方法在DashboardController类中实现,该类identified by initView() 。 出于本示例的目的, p:chart标签是发生魔术的地方。 尽管可以使用其他选项(在http://www.primefaces.org/showcase中访问),但是在这种情况下,图表类型设置为“条形”。 该模型设置为#{dashboardController.theaterCapacityModel} ,我们在控制器类中定义,填充和配置了该模型。 然后,我们提供宽度和高度,以使图表很好地显示。 万一数据发生变化(我知道剧院的大小通常不会增加或减少,但是请与我一起去),我们添加了PrimeFaces轮询组件,调用pollData( )方法,该组件会定期刷新数据。 在这种情况下,数据将每60秒刷新一次。 完成后,图表应如图2所示。

图2:PrimeFaces条形图

图2:PrimeFaces条形图

图表是交互式的,如果您单击标签,则条形图将被隐藏。 如果您有多个类别(通过ChartSeries ),这将很方便。 您甚至可以在图表组件中包含p:ajax标记,并在单击图表时调用一个动作…也许会弹出一个对话框,以显示有关单击的项目的一些其他数据。 做到了……现在您可以利用PrimeFaces和RESTful Web服务创建更多图表。 我建议在MoviePlex应用程序的基础上,看看还有其他可能性。 DashboardController类的完整源代码:

package org.glassfish.movieplex7.jsf;

import java.util.List;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.GenericType;
import org.glassfish.movieplex7.entities.Theater;

import org.primefaces.model.chart.Axis;
import org.primefaces.model.chart.AxisType;
import org.primefaces.model.chart.BarChartModel;
import org.primefaces.model.chart.ChartSeries;

/**
 *
 * @author Juneau
 */
@Named(value = "dashboardController")
@SessionScoped
public class DashboardController implements java.io.Serializable {
    
    Client jaxRsClient;
    // Typically not hard coded...store in a properties file or database
    String baseUri = "http://localhost:8080/ExploringJavaEE7/webresources/theater/";
    
    private List theaterList;
  
    private BarChartModel theaterCapacityModel;


    /**
     * Creates a new instance of FamisEquipPmChartController
     */
    public DashboardController() {
        jaxRsClient = ClientBuilder.newClient();
    }
    
    public void init() {
        loadData();

        createTheaterCapacityModel();
    }
    
    /**
     * Initializes the view on page render...if we wish to grab a reference
     * to a panel, etc.
     */
    public void initView(){
        UIViewRoot viewRoot =  FacesContext.getCurrentInstance().getViewRoot();
        // Do something
    }

    public void pollData() {
        System.out.println("polling data...");
        loadData();
    }

    /**
     * JAX-RS client to poll the data
     */
    private void loadData() {
       
        theaterList = jaxRsClient.target(baseUri)
                .request("application/xml")
                .get(new GenericType>() {
                }
                );
      
    }

    

    /**
     * Initialize the Bar Chart Model for Displaying PM Estimated Hours by Month
     *
     * @return
     */
    public BarChartModel initTheaterCapacityModel() {

        BarChartModel model = new BarChartModel();

        ChartSeries theaterCapacity = new ChartSeries();
        theaterCapacity.setLabel("Capacities");


        for (Theater theater : theaterList) {

            theaterCapacity.set(theater.getId(), theater.getCapacity());

        }
        model.addSeries(theaterCapacity);

        return model;
    }

   

 
    
    private void createTheaterCapacityModel() {
        theaterCapacityModel = initTheaterCapacityModel();

        theaterCapacityModel.setTitle("Theater Capacity");
        theaterCapacityModel.setLegendPosition("ne");
        theaterCapacityModel.setBarPadding(3);
        theaterCapacityModel.setShadow(false);

        Axis xAxis = theaterCapacityModel.getAxis(AxisType.X);
        xAxis.setLabel("Theater");

        Axis yAxis = theaterCapacityModel.getAxis(AxisType.Y);
        yAxis.setLabel("Capacity");
        yAxis.setMin(0);
        yAxis.setMax(200);

    }

    /**
     * @return the theaterCapacityModel
     */
    public BarChartModel getTheaterCapacityModel() {
        return theaterCapacityModel;
    }

    /**
     * @param theaterCapacityModel the theaterCapacityModel to set
     */
   public void setTheaterCapacityModel(BarChartModel theaterCapacityModel) {
        this.theaterCapacityModel = theaterCapacityModel;
    }
    
   
}

翻译自: https://www.javacodegeeks.com/2015/02/restful-charts-with-jax-rs-and-primefaces.html

jax_ws

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/cunfen8879/article/details/112551123

智能推荐

QQ聊天室登录界面_Oxygen404的博客-程序员秘密

界面布局分上中下三个部分。顶部是一张图,最下面是三个按钮,中间就是qq号码框和密码框,在加上几个按钮。顶部JLabel组件表示的是一个标签,本身是用于显示信息的,一般情况下是不能直接更改器显示内容的。创建完的Label对象可以通过Container类中的add()方法,加入到容器中。JLabel jbtop;jbtop = new JLabel(new ImageIcon("imag...

对前端获取的文件流进行加密和解密处理_文件流加密解密_珠穆雪峰的博客-程序员秘密

最近要实现一个对数据字典中上传的文件流的加密处理,并存入缓存的处理;在读取缓存时,若不存在此数据字典的缓存,则获取其加密文件,再将文件保存到缓存的处理操作;重点讲对文件流的加密和解密,废话不多说,看红色代码:      /*** * @Description: 保存数据字典并缓存* @author * @param sysCodeAttr         * @

C++新特性探究(18.1):C++11 shared_ptr智能指针_笑鸿的学习笔记的博客-程序员秘密

  在实际的 C++ 开发中,我们经常会遇到诸如程序运行中突然崩溃、程序运行所用内存越来越多最终不得不重启等问题,这些问题往往都是内存资源管理不当造成的。比如:  有些内存资源已经被释放,但指向它的指针并没有改变指向(成为了野指针),并且后续还在使用;  有些内存资源已经被释放,后期又试图再释放一次(重复释放同一块内存会导致程序运行崩溃);  没有及时释放不再使用的内存资源,造成内存泄漏,程序占用的内存资源越来越多。   针对以上这些情况,很多程序员认为C++语言应该提供更友好的内存管理机制,这样

GetLastError_jidongdong66的博客-程序员秘密

GetLastError<br />参考资料 1<br />《Windows核心编程》(第五版) 

算法就这么回事(一)七大查找算法汇总_青何的博客-程序员秘密

查找是在一个数据结合中查找满足给定条件的记录。对于查找问题来说,没有一种算法对于任何情况下都是合适的。有的查找速度比其他算法快,但是需要较多的存储空间(例如 Hash 查找);有的算法查找速度非常快,但仅用于有序数组(例如折半查找)。在实际应用中,如何在特大型规模的数据集合上进行高效查找具有非常重要的意义。

Js-带进度条的轮播图_andanghui2862的博客-程序员秘密

带进度条的轮播图--原生JS实现实现了图片自动轮播,左右按钮实现图片左右转换,下方原点或者缩小图点击选择其中的某一张图片,然后有红条实现图片的进度。&lt;div class="content"&gt; &lt;div class="box1"&gt; &lt;ul class="box1_1"&gt; ...

随便推点

Zabbix实现微信告警_weixin_33832340的博客-程序员秘密

zabbix实现微信告警可以分为以下两个步骤:在百度告警告警平台实现微信告警将Zabbix接入百度告警平台微信告警实现微信告警只需要如下四步:个人主页关注微信升级策略配置微信告警服务管理配置服务故障管理测试微信告警1、个人主页关注微信新注册后的用户进入告警平台后,默认会保存用户在注册时填写的电话,邮件联系方式,但是微信联系方式为空,需要用户手动添加并使用微信扫描二维码。如果个...

能够在was下部署的cxf spring maven 配置_weixin_33901843的博客-程序员秘密

2019独角兽企业重金招聘Python工程师标准&gt;&gt;&gt; ...

数据库课程设计-宿舍管理系统_学生宿舍管理系统数据库_宁悦的博客-程序员秘密

最近写完了数据库的课程设计,想把整个源码的编辑过程发出来。程序很简单,需要有很多完善的地方,在这里,我想和大家分享写这个程序的心路历程。首先,在开始写程序之前。我们需要先写一些工具类,来辅助完成整个程序的构建,在这里我把连接jdbc的代码放在了一个包下面。如下图:在这里我们先来写最基本的类,jdbcDrive,这是负责和数据库进行连接,并且执行语句的类publ...

首个酒店管理系统项目感想_lyj18508204052的博客-程序员秘密

作为一名初次接触程序的程序员,哦,不!应该说是程序菜鸟,满怀希望的开始我的程序生涯,从接触现在也已经有一个月整了,在这一个月里,感觉学到的东西还是非常多的,但是东西虽多的同时,感觉就有点吃不透了,也可能是没有怎么跟上大部队的节奏,俗话说,“万事开头难”这一点,在这个月也是深有体会。   由于对于项目的分析没有到位,导致在拿到项目的时候有点束手无策,所以就先写了一些之前学过的一些基本java的代

从零自学Hadoop(18):Hive的CLI和JDBC_df51511的博客-程序员秘密

阅读目录序Hive CLI(old CLI)Beeline CLI(new CLI)JDBCDemo下载系列索引本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作。文章是哥(mephisto)写的,SourceLink序 上一篇,我们对hive的数据导出,以...

带有进度条的轮播图jQuery实现_清砾的博客-程序员秘密

1.html模块&lt;div class="banner"&gt; &lt;ul&gt; &lt;li style="background: url(img/bg1.jpg) center;"&gt; &lt;img src="img/con1.png" /&gt; &lt;div class="nav"&gt;&lt;/div&gt; &lt;div class="bar"&gt; &lt;p&gt;&lt;/p&gt; &lt;/di