FACELETS: Não consigo fazer o exemplo da documentação funcionar
3 respostas
E
evtlucas
Prezados
Tentei desenvolver o projeto de exemplo do site https://facelets.dev.java.net/nonav/docs/dev/docbook.html sem sucesso. Cheguei ao ponto de não acusar erro na inicialização do tomcat(versão 6.0), mas a página guess.xhtml aparece da seguinte forma no firefox:
I’m thinking of a number from #{NumberBean.min} to #{NumberBean.max}. Can you guess it?
packagetutorial;importjava.io.Serializable;importjava.util.Random;importjavax.faces.application.FacesMessage;importjavax.faces.component.UIComponent;importjavax.faces.context.FacesContext;importjavax.faces.validator.ValidatorException;@SuppressWarnings("serial")publicclassNumberBeanimplementsSerializable{protectedfinalstaticRandomrand=newRandom();protectedintmin;protectedintmax;protectedintguess;protectedintactual;// Default ConstructorpublicNumberBean(){this.min=1;this.max=10;}// called by JSF to validate user inputpublicvoidvalidate(FacesContextcontext,UIComponentcomponent,Objectvalue)throwsValidatorException{// coerce the value to an inttry{intparam=Integer.parseInt(value.toString());// validate paramif(param>this.max||param<this.min){FacesMessagemsg=newFacesMessage("Guess must be between "+this.min+" and "+this.max);thrownewValidatorException(msg);}}catch(NumberFormatExceptione){FacesMessagemsg=newFacesMessage("Must be a number");thrownewValidatorException(msg);}}// lazy generate our actual valuepublicsynchronizedintgetActual(){if(this.actual==0){this.actual=rand.nextInt(this.max-this.min);this.actual+=this.min;}returnthis.actual;}// our message for the responsepublicStringgetMessage(){if(this.guess==this.getActual()){return"Sweet, you got it right!";}elseif(this.guess<this.getActual()){return"Sorry, try something higher";}else{return"Too bad, go lower";}}// other bean propertiespublicintgetMin(){returnthis.min;}publicintgetMax(){returnthis.max;}publicintgetGuess(){returnthis.guess;}publicvoidsetMin(intmin){this.min=min;}publicvoidsetMax(intmax){this.max=max;}publicvoidsetGuess(intguess){this.guess=guess;}}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"xmlns:ui="http://java.sun.com/jsf/facelets"><head><metahttp-equiv="Content-Type"content="text/html; charset=iso-8859-1"/><title>Facelets: Number Guess Tutorial</title><styletype="text/css"><!--body{font-family:Verdana,Arial,Helvetica,sans-serif;font-size:small;}--></style></head><body><h1><ui:insertname="title">Default Title</ui:insert></h1><p><ui:insertname="body">Default Body</ui:insert></p></body></html>
guess.xhtml
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"xmlns:ui="http://java.sun.com/jsf/facelets"xmlns:h="http://java.sun.com/jsf/html"><body><!-- This text above will not be displayed. --><ui:compositiontemplate="/template.xhtml"><!-- This text above will not be displayed. --><ui:definename="title">
I'm thinking of a number from #{NumberBean.min} to #{NumberBean.max}. Can you guess it?
</ui:define><!-- This text above will not be displayed. --><ui:definename="body"><h:formid="helloForm"><h:inputTexttype="text"id="userNo"value="#{NumberBean.guess}"validator="#{NumberBean.validate}"/><br/><h:commandButtontype="submit"id="submit"action="success"value="Submit"/><br/><h:messageshowSummary="true"showDetail="false"style="color: red; font-weight: bold;"id="errors1"for="userNo"/></h:form></ui:define><!-- This text above will not be displayed. --></ui:composition><!-- This text above will not be displayed. --></body></html>
response.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"xmlns:ui="http://java.sun.com/jsf/facelets"xmlns:h="http://java.sun.com/jsf/html"><body><ui:compositiontemplate="/template.xhtml"><ui:definename="title">
#{NumberBean.message}
</ui:define><ui:definename="body"><formjsfc="h:form"><inputjsfc="h:commandButton"type="submit"id="back"value="Back"action="success"/></form></ui:define></ui:composition></body></html>
Jars instalados na imagem em anexo
E
evtlucas
Prezados
Desculpem o erro de principiante: Estava chamando a página guess.xhtml dessa forma. Ao chamar como guess.jsf, passou a funcionar.