JSF Logic: Dymanically display images and change text style

本文介绍如何在Web应用中根据业务逻辑动态改变表格数据显示样式,包括字体加粗、背景色变化及图片显示,并提供实现代码示例。
Presenting data in different styles according to its logic is always required in web application. A typical example will be the online book store, if the quantity of one type of book is below 100, then it should be displayed in bold red to alarm people, otherwise normal style will be used. Sometimes, it is also necessary to display images dynamically based on business logic, for example, the online live soccer match should reflect excactlly  what is happening in the field, like a yellow card should be displayed next to a player's name if he gets one. Here is a sample:



For this sample, the key issue is to use binding attribute of dataTable, and rendered, style attributes of graphicImage and outputText respectively. Binding means binding the instance of current dataTable to a UIData.

OK, let's take a look at the JSP source code:

<hx:dataTableEx id="tableEx1" value="#{pc_TestPortletView.orders.data}" var="vardata" ..  binding="#{pc_TestPortletView.orderStyle.listData}">

<hx:columnEx id="columnEx1">
<f:facet name="header">
<h:outputText styleClass="outputText" value="Order_details" id="text1"></h:outputText>
</
f:facet>   
                

<h:outputText id="textOrder_details1" value="#{vardata.order_details}"  styleClass="outputText"style="#{pc_TestPortletView.orderStyle.boldStyle}">
</h:outputText>

<hx:graphicImageEx id="imageEx1" styleClass="graphicImageEx" value="theme/yellow.gif"rendered="#{pc_TestPortletView.orderStyle.displayYellow}">
</
hx:graphicImageEx>
</
hx:columnEx>

We use the binding attribute to bind the current dataTable instance to a UIData which is listData in this case, and use style and rendered attributes to control the appearance. They are all integrated into the OrderStyle javabean:

public class OrderStyle {

     UIData listData = new UIData();

     boolean displayYellow;

               String boldStyle;

        public String getBoldStyle() {

            int order_id =((Order)(listData.getRowData())).getOrder_id();

            if(order_id==0||order_id==2||order_id==3)

                  boldStyle ="font-weight: bold";

                  //boldStyle="boldSytle";

            else

                  boldStyle = "font-weight: 400";

                  //boldStyle="normalStyle";

            return boldStyle;

      }

      public void setBoldStyle(String boldStyle) {

            this.boldStyle = boldStyle;

      }

public boolean isDisplayYellow() {

            int order_id =((Order)(listData.getRowData())).getOrder_id();

            if(order_id==0||order_id==1||order_id==4)

                  displayYellow= true;

            else

                  displayYellow= false;

            return displayYellow;

      }

      public void setDisplayYellow(boolean displayYellow) {

            this.displayYellow = displayYellow;

      }

public UIData getListData() {

            return listData;

      }

      public void setListData(UIData listData) {

            this.listData = listData;

      }

    ...

}

That's all! (Notice: In this way, the style is applied only to the column, not to the row.)

Now you may ask, could we change the row background* based on logic? The answer is yes. Please keep tracking which is coming soon...

* After test, the way we are using to change the row background according to logic is only suitable to the data table without paging. Here is the code we use, we bind the rowClasses attribute of dataTable to the variable rowColorStyle:

<hx:dataTableEx id="tableEx1"
value="#{pc_TestPortletView.orders.data}"var="vardata"
                        styleClass="dataTableEx" headerClass="headerClass"
                        rowClasses="#{pc_TestPortletView.rowColorStyle}"
                        border="0" cellpadding="2"
                        cellspacing="0"
                        rows="15">

to get the row style, getRowStyle function in OrdersRowStyle is invoked:
public String getRowColorStyle() {
OrdersRowStyle rowStyle = new OrdersRowStyle();
            rowColorStyle = rowStyle.getRowStyle(getOrders().getData());
            returnrowColorStyle;
      }
Here is the OrdersRowStyle class:
 
 
publicclass OrdersRowStyle {
      public String getRowStyle(Order[] order)
      {
            String rowStyle = ""; 
            for(int i=0; i<order.length;i++)
            {
                if(order[i].getOrder_id()==0||order[i].getOrder_id()==3)
                        rowStyle +="darkBrownColor,";
                  else
                        rowStyle +="brownColor,";
            }
            System.out.println("rowStyle: "+rowStyle);
            return rowStyle;
      }
}
The problem is with paging, the row style is repeated on the next pages, like:
 Go to the next page, you will see the same row style is used:
 
 
【电力系统】单机无穷大电力系统短路故障暂态稳定Simulink仿真(带说明文档)内容概要:本文档围绕“单机无穷大电力系统短路故障暂态稳定Simulink仿真”展开,提供了完整的仿真模型与说明文档,重点研究电力系统在发生短路故障后的暂态稳定性问题。通过Simulink搭建单机无穷大系统模型,模拟不同类型的短路故障(如三相短路),分析系统在故障期间及切除后的动态响应,包括发电机转子角度、转速、电压和功率等关键参数的变化,进而评估系统的暂态稳定能力。该仿真有助于理解电力系统稳定性机理,掌握暂态过程分析方法。; 适合人群:电气工程及相关专业的本科生、研究生,以及从事电力系统分析、运行与控制工作的科研人员和工程师。; 使用场景及目标:①学习电力系统暂态稳定的基本概念与分析方法;②掌握利用Simulink进行电力系统建模与仿真的技能;③研究短路故障对系统稳定性的影响及提高稳定性的措施(如故障清除时间优化);④辅助课程设计、毕业设计或科研项目中的系统仿真验证。; 阅读建议:建议结合电力系统稳定性理论知识进行学习,先理解仿真模型各模块的功能与参数设置,再运行仿真并仔细分析输出结果,尝试改变故障类型或系统参数以观察其对稳定性的影响,从而深化对暂态稳定问题的理解。
本研究聚焦于运用MATLAB平台,将支持向量机(SVM)应用于数据预测任务,并引入粒子群优化(PSO)算法对模型的关键参数进行自动调优。该研究属于机器学习领域的典型实践,其核心在于利用SVM构建分类模型,同时借助PSO的全局搜索能力,高效确定SVM的最优超参数配置,从而显著增强模型的整体预测效能。 支持向量机作为一种经典的监督学习方法,其基本原理是通过在高维特征空间中构造一个具有最大间隔的决策边界,以实现对样本数据的分类或回归分析。该算法擅长处理小规模样本集、非线性关系以及高维度特征识别问题,其有效性源于通过核函数将原始数据映射至更高维的空间,使得原本复杂的分类问题变得线性可分。 粒子群优化算法是一种模拟鸟群社会行为的群体智能优化技术。在该算法框架下,每个潜在解被视作一个“粒子”,粒子群在解空间中协同搜索,通过不断迭代更新自身速度与位置,并参考个体历史最优解和群体全局最优解的信息,逐步逼近问题的最优解。在本应用中,PSO被专门用于搜寻SVM中影响模型性能的两个关键参数——正则化参数C与核函数参数γ的最优组合。 项目所提供的实现代码涵盖了从数据加载、预处理(如标准化处理)、基础SVM模型构建到PSO优化流程的完整步骤。优化过程会针对不同的核函数(例如线性核、多项式核及径向基函数核等)进行参数寻优,并系统评估优化前后模型性能的差异。性能对比通常基于准确率、精确率、召回率及F1分数等多项分类指标展开,从而定量验证PSO算法在提升SVM模型分类能力方面的实际效果。 本研究通过一个具体的MATLAB实现案例,旨在演示如何将全局优化算法与机器学习模型相结合,以解决模型参数选择这一关键问题。通过此实践,研究者不仅能够深入理解SVM的工作原理,还能掌握利用智能优化技术提升模型泛化性能的有效方法,这对于机器学习在实际问题中的应用具有重要的参考价值。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
<think>好的,我现在需要解决用户的问题:如何在JSF中动态设置p:inputText组件为只读状态。用户希望看到示例,所以我得先回忆一下JSF和PrimeFaces的相关知识。首先,用户提到的是PrimeFaces的组件p:inputText。动态设置只读属性通常可以通过几种方式实现。常见的做法是使用EL表达式,结合后端bean的属性来控制readonly属性。例如,使用rendered或者直接设置readonly属性,但readonly更符合需求。接下来,我需要确认EL表达式的正确写法。readonly属性应该绑定到一个布尔值,所以后端bean里需要有对应的boolean属性。比如,在bean里有一个isReadOnly字段,然后通过#{bean.readOnly}来引用。 然后,可能需要考虑动态条件的情况,比如根据某些业务逻辑判断是否只读。这时候可以在EL表达式中使用条件判断,比如使用三元运算符或者调用方法返回布尔值。 另外,用户可能需要通过JavaScript在前端动态改变只读状态。这时候需要获取组件客户端ID,并用PrimeFaces的widgetVar来操作。例如,通过PF('widgetVar').jq.prop('readonly', true)来设置。还要注意,JSF中动态属性的处理方式,确保表达式正确,并且组件在渲染时能正确应用属性。有时可能需要使用AJAX来更新组件状态,比如在某个事件触发后重新渲染输入框。最后,需要提供示例代码,包括后端bean的代码和前端xhtml的代码,以及可能的JavaScript操作。确保示例简洁明了,覆盖不同场景,比如EL表达式绑定、条件判断和前端JS操作。 可能还需要提醒用户注意组件ID和widgetVar的命名,避免冲突,以及在AJAX更新时的注意事项。比如,如果组件被AJAX更新后,可能需要重新获取widgetVar或者使用正确的选择器。总结下来,回答的结构应该是先介绍EL表达式绑定,给出后端和前端的示例,然后讲解条件判断的情况,最后补充JavaScript动态修改的方法。同时,确保代码正确,语法无误,符合用户的需求。</think>在JSF中动态设置`<p:inputText>`的只读状态可通过以下三种方式实现: ### 方式1:EL表达式绑定 在后端bean中定义boolean属性: ```java @Named @ViewScoped public class FormBean { private boolean readOnly = true; // Getter/setter } ``` 前端绑定: ```xhtml <p:inputText value="#{formBean.value}" readonly="#{formBean.readOnly}"/> ``` ### 方式2:条件表达式 直接根据业务逻辑设置: ```xhtml <p:inputText value="#{formBean.value}" readonly="#{formBean.userType eq 'ADMIN'}"/> ``` ### 方式3:JavaScript动态修改 通过widgetVar控制: ```xhtml <p:inputText id="dynamicField" widgetVar="inputWidget"/> <p:commandButton value="锁定" onclick="PF('inputWidget').jq.prop('readonly', true)"/> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值