Qualcuno può spiegarmi perché Foo nel mio esempio è sempre nullo quando raggiunge la classe validateDoubleRange? Il risultato finale è il valore minimo per il validatore è sempre 0. Il numero 3 viene visualizzato correttamente nella pagina quando si trova nell'elemento outputText. Valida fino se faccio il fagiolo @SessionScoped
invece di @ViewScoped
Perché f: validateDoubleRange funziona solo per @SessionScoped?
Controller:
import java.io.Serializable;
import java.math.BigDecimal;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ViewScoped
@ManagedBean(name = "fooController")
public class FooController implements Serializable {
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(FooController.class);
private static final long serialVersionUID = 1L;
private Foo foo;
private BigDecimal amount;
private Long fooId;
public Long getFooId() {
return fooId;
}
public void setFooId(Long fooId) {
this.fooId = fooId;
this.foo = new Foo();
foo.setFooId(fooId);
foo.setMinAmount(Double.valueOf(3));
foo.setMaxAmount(Double.valueOf(10));
}
public Foo getFoo() {
return foo;
}
public void sendAmount() {
log.debug("sendAmount: " + amount);
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
public static class Foo {
private Long fooId;
private Double minAmount;
private Double maxAmount;
public Foo() {
}
public void setFooId(Long fooId) {
this.fooId = fooId;
}
public void setMinAmount(Double minAmount) {
this.minAmount = minAmount;
}
public void setMaxAmount(Double maxAmount) {
this.maxAmount = maxAmount;
}
public Long getFooId() {
return fooId;
}
public Double getMaxAmount() {
return maxAmount;
}
public Double getMinAmount() {
return minAmount;
}
}
}
JSP:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
>
<f:metadata>
<f:viewParam name="fooId" value="#{fooController.fooId}" />
</f:metadata>
<h:form id="myForm">
<h:outputText value="This is correctly displayed: '#{fooController.foo.minAmount}'"/><br/>
<h:outputText value="My Input:" />
<h:inputText id="myInput"
value="#{fooController.amount}"
required="true"
>
<f:convertNumber maxFractionDigits="2"/>
<f:validateDoubleRange minimum="#{fooController.foo.minAmount}" maximum="80"/>
</h:inputText>
<h:message for="myInput"/>
<br/>
<h:commandButton id="myButton"
value="Save Amount"
action="#{fooController.sendAmount}"
>
</h:commandButton>
</h:form>
</ui:composition>
Sto usando JSF 2 su JBoss 6.1
Se sei là fuori @BalusC, questo è l'esempio più semplice che potrei inventare, ed è lo stesso problema di ieri, solo, spero, chiarito e più semplice da replicare.
- modifica, dovrei aggiungere che fooId è impostato tramite una vista param, quindi hai bisogno di? Fooid = 3 alla fine del tuo URL.
Wow, molto informativo e utile. Pensavo che avrei potuto diventare matto. Grazie mille. – Simon
Prego. – BalusC