Question

J'apprends Wicket par « Profiter de développement Web avec Wicket » book.It est écrit pour Wicket 1.4.7
Dans un exemple:

int weight = ((Integer) weightModel.getObject()).intValue();

est utilisé. Lorsque je clique sur le bouton d'envoi, il lance RuntimeException inattendue premières lignes:

WicketMessage: Méthode onFormSubmitted de l'interface org.apache.wicket.markup.html.form.IFormSubmitListener ciblée sur le composant [MarkupContainer [id = Composant former]] a généré une exception

Cause Racine:

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer*

Probablement weightModel.getObject () ne peut pas être converti chaîne .

Le message complet d'exception est en bas.

Mais après changé le code:

int weight=Integer.parseInt( (String) weightModel.getObject());

Il fonctionne très bien. Il est censé fonctionner correctement. Quel est le raison pour lancer l'exception?


Le code complet:

GetRequest.java

package myapp.postage;
import java.util.HashMap;
import java.util.Map;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.Model;

@SuppressWarnings("unchecked")
public class GetRequest extends WebPage {
 private Model weightModel=new Model();
 private Model patronCodeModel=new Model();
 private Map patronCodeToDiscount; 

 public GetRequest(){
  patronCodeToDiscount=new HashMap();
  patronCodeToDiscount.put("p1", new Integer(90));
  patronCodeToDiscount.put("p2", new Integer(95));

  Form form=new Form("form"){
   @Override
   protected void onSubmit(){
    int weight = ((Integer) weightModel.getObject()).intValue();
    Integer discount=(Integer)patronCodeToDiscount.get(patronCodeModel.getObject());
    int postagePerKg=10;
    int postage=weight*postagePerKg;
    if(discount!=null){
     postage=postage*discount.intValue()/100;
    }
    ShowPostage showPostage=new ShowPostage(postage);
    setResponsePage(showPostage);
   }
  };
  TextField weight=new TextField("weight",weightModel);
  form.add(weight);
  TextField patronCode=new TextField("patronCode",patronCodeModel);
  form.add(patronCode);
  add(form);
 }
}
  

Le fichier html GetRequest.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
        <form wicket:id="form">
     <table>
      <tr>
       <td>Weight</td>
       <td><input type="text" wicket:id="weight"/></td>
      </tr>
      <tr>
       <td>Patron code:</td>
       <td><input type="text" wicket:id="patronCode"/></td>
      </tr>
      <tr>
       <td></td>
       <td><input type="submit"/></td>
      </tr>
     </table>
         </form>
    </html>

Message d'exception: WicketMessage: Méthode onFormSubmitted de l'interface org.apache.wicket.markup.html.form.IFormSubmitListener ciblée sur le composant [MarkupContainer [id = Composant former]] a généré une exception

Cause Racine:

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
     at myapp.postage.GetRequest$1.onSubmit(GetRequest.java:26)
     at org.apache.wicket.markup.html.form.Form.delegateSubmit(Form.java:1538)
     at org.apache.wicket.markup.html.form.Form.process(Form.java:934)
     at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:896)
     at java.lang.reflect.Method.invoke(Method.java:597)
     at org.apache.wicket.RequestListenerInterface.invoke(RequestListenerInterface.java:182)
     at org.apache.wicket.request.target.component.listener.ListenerInterfaceRequestTarget.processEvents(ListenerInterfaceRequestTarget.java:73)
     at org.apache.wicket.request.AbstractRequestCycleProcessor.processEvents(AbstractRequestCycleProcessor.java:92)
     at org.apache.wicket.RequestCycle.processEventsAndRespond(RequestCycle.java:1250)
     at org.apache.wicket.RequestCycle.step(RequestCycle.java:1329)
     at org.apache.wicket.RequestCycle.steps(RequestCycle.java:1428)
     at org.apache.wicket.RequestCycle.request(RequestCycle.java:545)
     at org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:479)
     at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:312)
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
     at com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.invoke(HttpRequestOperationCollectionValve.java:60)
     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
     at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:379)
     at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
     at java.lang.Thread.run(Thread.java:619)

Complete stack:
org.apache.wicket.WicketRuntimeException: Method onFormSubmitted of interface org.apache.wicket.markup.html.form.IFormSubmitListener targeted at component [MarkupContainer [Component id = form]] threw an exception
     at org.apache.wicket.RequestListenerInterface.invoke(RequestListenerInterface.java:193)
     at org.apache.wicket.request.target.component.listener.ListenerInterfaceRequestTarget.processEvents(ListenerInterfaceRequestTarget.java:73)
     at org.apache.wicket.request.AbstractRequestCycleProcessor.processEvents(AbstractRequestCycleProcessor.java:92)
     at org.apache.wicket.RequestCycle.processEventsAndRespond(RequestCycle.java:1250)
     at org.apache.wicket.RequestCycle.step(RequestCycle.java:1329)
     at org.apache.wicket.RequestCycle.steps(RequestCycle.java:1428)
     at org.apache.wicket.RequestCycle.request(RequestCycle.java:545)
     at org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:479)

java.lang.reflect.InvocationTargetException
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
     at java.lang.reflect.Method.invoke(Method.java:597)
     at org.apache.wicket.RequestListenerInterface.invoke(RequestListenerInterface.java:182)
     at org.apache.wicket.request.target.component.listener.ListenerInterfaceRequestTarget.processEvents(ListenerInterfaceRequestTarget.java:73)
     at org.apache.wicket.request.AbstractRequestCycleProcessor.processEvents(AbstractRequestCycleProcessor.java:92)
     at org.apache.wicket.RequestCycle.processEventsAndRespond(RequestCycle.java:1250)
     at org.apache.wicket.RequestCycle.step(RequestCycle.java:1329)
     at org.apache.wicket.RequestCycle.steps(RequestCycle.java:1428)
     at org.apache.wicket.RequestCycle.request(RequestCycle.java:545)
     at org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:479)
Était-ce utile?

La solution

Si vous utilisez Wicket 1.4 et au-dessus, vous devez utiliser les génériques et les utiliser pour dire quel type que vous wicket attendez. Wicket fera la conversion pour vous alors.

Je suggère les modifications suivantes (pour le poids, l'autre champ est laissé comme un exercice;)):

Ajouter un champ à vous la page qui contiendra l'entrée de l'utilisateur:

private Integer weight;

Ajouter getter et setter pour ce champ:

public Integer getWeight() {return weight;}
public void SetWeight(Integer weight) {this.weight = weight;}

Ensuite, remplacez le code pour ajouter le champ de texte pour le poids avec:

form.add(new TextField<Integer>("weight"
     , new PropertyModel<Integer>(this, "weight"));

Avec cela, Wicket convertit le userinput en un nombre entier et le stocker dans le poids sur le terrain. Le PropertyModel utilise la page elle-même pour accéder au champ.

L'espoir qui aide.

Astuce: dans le cas où l'utilisateur entre quelque chose qui ne peut pas être converti, Wicket ajoutera une erreur au champ de texte. Vous devez ajouter un Feedbackpanel à votre page pour voir.

Profitez

Autres conseils

Quel entier fait la fonction qui fonctionne le rendement? Et quelle exception jette?

Si son zéro, peut-être getObject () ne retourne pas un entier du tout.

  

Probablement weightModel.getObject () ne peut pas être converti en chaîne.

non. l'objet retourné est une chaîne pas un entier que vous voulez avec le casting entier.

une solution serait d'analyser la chaîne retournée Integer.parseInt (str) (mais je pense que peut le faire wicket pour vous ...)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top