2011-01-31 1 views
8

ho fatto alcune ricerche sul web e in StackOverflow per vedere come gestire il seguente messaggio che ricevo su uno dei miei schermi:utilizzo primavera di messaggi typeMismatch

Impossibile convertire il valore della proprietà di tipo [java.lang.String] su richiesto tipo [java.lang.Long] per proprietà 'qtyToShip'; l'eccezione annidata è java.lang.IllegalArgumentException: Impossibile analizzare il numero: di analizzarlo numero: "a"

Dalla ricerca e dal guardando il web ho pensato che aggiungendo quanto segue al mio file errors.properties avrebbe produrre i risultati desiderati:

typeMismatch.shippingCommand.qtyToShip=1. Invalid value for Qty To Ship, accepts only numbers. 
typeMismatch.qtyToShip=2. Invalid value for Qty To Ship, accepts only numbers. 
shippingCommand.qtyToShip=3. Invalid value for Qty To Ship, accepts only numbers. 
qtyToShip=4. Invalid value for Qty To Ship, accepts only numbers. 


typeMismatch.java.lang.NumberFormatException=5. Invalid value for {0}, accepts only numbers. 
typeMismatch.java.lang.Integer=Must specify an integer value. 
typeMismatch.java.lang.Long=Must specify an integer value. 
typeMismatch.java.lang.Float=Must specify a decimal value. 
typeMismatch.java.lang.Double=Must specify a decimal value. 
typeMismatch.int=Invalid number entered 
typeMismatch=Invalid type entered 

Aggiungo valori interi al messaggio per determinare quale verrebbe visualizzato.

Ora in cima alla mia JSP Ho il seguente:

<center> 
    <spring:hasBindErrors name="shippingCommand"> 
     <c:if test="${errors.errorCount > 0 }"> 
     <h4>Following errors need to be corrected:</h4> 
     <font color="red"> 
      <c:forEach items="${errors.allErrors}" var="error"> 
      <spring:message code="${error.code}" arguments="${err.arguments}" text="${error.defaultMessage}"/><br /> 
      </c:forEach> 
     </font> 
     </c:if> 
    </spring:hasBindErrors> 
    </center> 

Quanto sopra è fuori dalla mia forma, dentro il mio modulo ho il seguente (testare ideali)

Quindi il risultato è che quando eseguo il mio codice per attivare l'errore, vedo che all'interno del mio modulo viene visualizzato il seguente messaggio:

1. Invalid value for Qty To Ship, accepts only numbers. 

che viene da questo: typeMismatch.shippingCommand.qtyToShip

l'esterno della displayes forma:

Invalid type entered 

Quello che non capisco è perché sono in grado di visualizzare il messaggio corretto dentro la mia forma, ma non fuori?

Nel controllore ho aggiunto il seguente:

@Override 
protected void initBinder(HttpServletRequest pRequest, ServletRequestDataBinder pBinder) throws Exception 
{ 
    NumberFormat numberFormat = NumberFormat.getInstance(); 
    pBinder.registerCustomEditor(Long.class, "qtyToShip", new CustomNumberEditor(Long.class, numberFormat, false)); 
} 

Grazie

risposta

6

Forse error.code non garantisce di restituire il codice di messaggio più specifico. Prova a passare il messaggio di errore nel suo insieme:

<spring:message message = "${error}" /> 
+2

Figlio di una pistola - che ha fatto il trucco. Grazie – boyd4715