gpt4 book ai didi

css - JSF : Changing component style in jSTL c:foreach array not working

转载 作者:行者123 更新时间:2023-11-28 19:05:48 32 4
gpt4 key购买 nike

此代码显示索引并使用 RichFaces 的 <a4j:commandLink> 执行操作标签。它在技术上工作正常。只有先前选择的字母的样式没有被重置(尽管执行了适当的代码部分)。有谁知道问题出在哪里以及如何解决?

JSF 页面:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:c="http://java.sun.com/jstl/core" xmlns:rich="http://richfaces.org/rich" xmlns:a4j="http://richfaces.org/a4j">
<head>
<style>
a.selected {
text-decoration: underline;
}

a.unselected {
text-decoration: none;
}
</style>
</head>
<body>
<rich:panel id="result">
hello
<h:form id="myForm">
<c:forEach var="letter" items="#{testBean.letters}" >
<a4j:commandLink id="${letter}" value="${letter}" actionListener="#{testBean.startWith}" reRender="result" styleClass="unselected"/>&#160;
</c:forEach>
</h:form>
</rich:panel>
<a4j:keepAlive beanName="testBean" />
</body>
</html>

请注意,它正在使用 <a4j:keepalive>在 Ajax 请求之间保持支持 bean 事件。

支持 bean(请求范围):

public class testBean
{
private String startLetter = null; // selects from alphabetical page
private String[] letters = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
private UIComponent currentStartWithLetter;

public void startWith(ActionEvent actionEvent)
{
FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
Application app = facesContext.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ValueExpression valueExp;
if (currentStartWithLetter != null) {
valueExp = elFactory.createValueExpression(elContext, "unselected",Object.class);
currentStartWithLetter.setValueExpression("styleClass", valueExp);
}
currentStartWithLetter = actionEvent.getComponent();
startLetter = currentStartWithLetter.getId();
valueExp = elFactory.createValueExpression(elContext, "selected",Object.class);
currentStartWithLetter.setValueExpression("styleClass", valueExp);
someAction(actionEvent);
}


public void setLetters(String[] letters) {
/* nothing to do */
}

public String[] getLetters() {
return letters;
}

public String getStartLetter() {
return startLetter;
}

public void setStartLetter(String startLetter) {
this.startLetter = startLetter;

}
}

最佳答案

我总觉得如果我引用UIComponent在我的代码中,我正在做一些困难的事情。以下是我将如何执行此操作。

尝试使用 <f:setPropertyActionListener><a4j:repeat>代替 actionListener<c:forEach> .此外,避免重新呈现整个表单,这会在某些浏览器中导致问题:

<h:panelGroup id="results">
<a4j:repeat var="letterBean" items="#{testBean.letters}" >
<a4j:commandLink id="letter" value="#{letterBean.letter}" reRender="results" styleClass="#{letterBean.selected ? 'selected' : 'unselected'}">
<f:setPropertyActionListener value="#{letterBean}" target="#{testBean.selected}"/>
</a4j:commandLink>&#160;
</a4j:repeat>
</h:panelGroup>

新 bean 类:

public class LetterBean {
private final String letter;
private boolean selected;

public LetterBean (String letter) {
this.letter = letter;
}

//getters and setters

}

替换startWith与:

public void setSelected(LetterBean selectedBean) {
for (LetterBean letter : letterBeans) {
letter.setSelected(false);
}
selectedBean.setSelected(true);
}

关于css - JSF : Changing component style in jSTL c:foreach array not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3587397/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com