1

このコードは、インデックスを表示し、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;

 }
}
4

2 に答える 2

1

UIComponent私は自分のコードで参照している場合、私は何か難しい方法をしているといつも感じています。これが私がこれを行う方法です。

<f:setPropertyActionListener>and<a4j:repeat>の代わりにactionListenerandを使用してみてください<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);
}
于 2010-08-30T20:21:05.327 に答える
0

コードの変更を変更しました:

if (currentStartWithLetter != null) {
 valueExp = elFactory.createValueExpression(elContext, "unselected",Object.class);
 currentStartWithLetter.setValueExpression("styleClass", valueExp);
} 

の中へ

if (currentStartWithLetter != null) {
 valueExp = elFactory.createValueExpression(elContext, "unselected",Object.class);
 currentStartWithLetter = (UIComponent) actionEvent.getComponent().findComponent(startLetter);
 currentStartWithLetter.setValueExpression("styleClass", valueExp);
} 

そしてそれは動作します。

currentStartWithLetteractionEvent が呼び出し間で異なるため、同じコンポーネントを参照しなくなったことが問題のようです。

于 2010-08-28T17:13:56.587 に答える