最近、システムを JSF 1.2 から JSF 2.0 にアップグレードし、すべてが機能するようにしています。ただし、バリデータをデータ テーブルまたは同様のコンポーネント内で使用すると、問題が発生します。基本的に問題は、バリデーターがデータテーブルによって設定された変数を使用できないことです。
次に例を示します。
検証者:
package test;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
@FacesValidator("test.TestValidator")
public class TestValidator implements Validator {
private Integer length;
public TestValidator() {
super();
}
@Override
public void validate(FacesContext context,
UIComponent component, Object value) throws ValidatorException {
String text = (String) value;
if (text == null || text.trim().length() == 0) {
return;
}
if (length != null && text != null && length.intValue() < text.length()) {
String message = "The text is too long. It was " + text.length() +
", but only " + length + " characters are allowed.";
FacesMessage fm = new FacesMessage(
FacesMessage.SEVERITY_ERROR, message, null);
throw new ValidatorException(fm);
}
}
public Integer getLength() {
return length;
}
public void setLength(Integer length) {
this.length = length;
}
}
タグライブラリ:
<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
version="2.0">
<namespace>http://industry-supply.dk/test</namespace>
<tag>
<tag-name>testValidator</tag-name>
<validator>
<validator-id>test.TestValidator</validator-id>
</validator>
<attribute>
<name>length</name>
<required>true</required>
<type>java.lang.Integer</type>
</attribute>
</tag>
</facelet-taglib>
マネージド ビーン:
package test;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean(name = "testBean")
@RequestScoped
public class TestBean {
public TestBean() {
}
public String[] getKey() {
return new String[]{
"0",
"1",
"2",
"3"
};
}
public String[] getValue() {
return new String[]{
"This is a text and it's too long.",
"This is a text and it's too long.",
"This is a text and it's too long.",
"This is a text and it's too long."
};
}
}
JSF:
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://java.sun.com/jsf/html"
xmlns:test="http://industry-supply.dk/test">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:dataTable value="#{testBean.key}" var="k">
<h:column>
<h:message for="val"/>
<h:inputText id="val" value="#{testBean.value[k]}">
<test:testValidator length="#{testBean.key[k]}"/>
</h:inputText>
</h:column>
</h:dataTable>
<h:commandButton value="Submit"/>
</h:form>
</h:body>
</html>
プロジェクトを実行すると、4 つの入力フィールドとコマンド ボタンが表示されます。各入力フィールドには、33 文字の長さのテキストが含まれています。「送信」をクリックすると、「テキストが長すぎます。33 文字でしたが、0 文字しか使用できません。」というエラー メッセージが表示されます。行/フィールドごとに表示されます。"test:testValidator length="#{testBean.key[k]}"" は長さを 1 行目に 0、2 行目に 1、3 行目に 2、4 行目に 3 に指定しているため、これは正しくありません。そのため、最後の行のエラー メッセージには、「テキストが長すぎます。33 でしたが、3 文字しか使用できません。」と表示されているはずです。
問題は、バリデーターが JSF ファイルの dataTable コンポーネントから k 変数にアクセスできないように見えることです。これは JSF 1.2 では機能しましたが、JSF 2.0 では機能しません。現在、この問題に何日も費やしており、本当に助けが必要です. アイデアはありますか?