JSP でプロジェクトに取り組んでいますが、生成された html に問題があります。
生成された html: "1" は "Oui" (フランス語ではい) に変換され、"0" は "Non" (フランス語でいいえ) に変換されます。
<select id="privilegesRole" name="privileges" multiple="multiple">
<option value="5" selected="selected">Role 1</option>
<option value="4" selected="selected">Role 2</option>
<option value="Oui" selected="selected">Role 3</option>
<option value="6" selected="selected">Role 4</option>
<option value="2" selected="selected">Role 5</option>
<option value="Non" selected="selected">Role 6</option>
<option value="3" selected="selected">Role 7</option>
</select>
私のJSPコード:
<form:select path="privileges" multiple="true" id="privilegesRole">
<form:options itemLabel="libelle" itemValue="id" items="${role.privileges}" />
</form:select>
コントローラ: 私のコントローラの値は良好です
RoleAdministration role = findBy....();
model.addAttribute("role", role);
よろしくお願いいたします。
編集: BooleanFormatter.java がありますが、どこで使用されるかわかりません
import org.springframework.format.Formatter;
import org.springframework.stereotype.Component;
@Component
public class BooleanFormatter implements Formatter<Boolean> {
private String trueLabel = UtilMessages.getInstance().getString("common.oui");
private String falseLabel = UtilMessages.getInstance().getString("common.non");
@Override
public String print(Boolean arg0, Locale arg1) {
return arg0 ? trueLabel : falseLabel;
}
@Override
public Boolean parse(String arg0, Locale arg1) throws ParseException {
if (Boolean.TRUE.toString().equals(arg0))
return true;
else if (Boolean.FALSE.toString().equals(arg0)) {
return false;
} else if (trueLabel.equals(arg0))
return true;
else if (falseLabel.equals(arg0)) {
return false;
}
throw new ParseException(arg0, 0);
}
}
jsp コードを次のように変更しました。
<form:select id="privilegesRole" path="privileges" multiple="true" >
<c:forEach items="${role.privileges}" var="currPrivilegeSelect">
<option value="<c:out value="${currPrivilegeSelect.id}"/>"
title="<c:out value="${currPrivilegeSelect.description}"/>">
<c:out value="${currPrivilegeSelect.libelle}"/> </option>
</c:forEach>
</form:select>
それは機能しますが、その理由はわかりません。何か説明があれば、喜んでお読みいたします:)