Wednesday 15 July 2015

java - xhtml file is not binding with bean through annotation -



java - xhtml file is not binding with bean through annotation -

i have made simple login page in jsf 2.0 using annotation. using eclipse jboss 6. login.xhtml file code is

<?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"> <?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"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>sign-in page</title> </h:head> <h:body> <h3>sign in</h3> <h:form> <!-- email id: <h:inputtext value="#{loginbean.emailid}" id="emailid" size="20"></h:inputtext> <br /> password: <h:inputsecret value="#{loginbean.password}" id="password"></h:inputsecret>--> <h:commandbutton value="signin" action="#{loginbean.dologin}" id="signin"></h:commandbutton> </h:form> </h:body> </html>

the loginbean.java code is:

bundle com; import java.io.serializable; import javax.faces.application.facesmessage; import javax.faces.context.facescontext; import javax.swing.joptionpane; import javax.faces.bean.managedbean; import javax.faces.bean.sessionscoped; @managedbean(name = "loginbean" , eager=true) @sessionscoped public class loginbean implements serializable { private static final long serialversionuid = 7765876811740798583l; private string emailid = "madiha"; private string password = "madiha"; public void dologin() { if (emailid.equals("madiha") && password.equals("madiha")) { joptionpane.showinputdialog("eggs not supposed green."); // system.out.print("you logged in"); } facesmessage msg = new facesmessage("login error!", "error msg"); msg.setseverity(facesmessage.severity_error); facescontext.getcurrentinstance().addmessage(null, msg); } public string getemailid() { homecoming emailid; } public void setemailid(string emailid) { this.emailid = emailid; } public string getpassword() { homecoming password; } public void setpassword(string password) { this.password = password; } }

when run login.xhtml, shows labels, , no button aor text field. seems bean class not binded.

1. wrong jsf tags. tag names 1) case-sensitive , 2) tags utilize belong taglib namespace prefix h: set in xmlns attribute of html element. should right f:inputtext h:inputtext, f:inputsecret h:inputsecret , f:commandbutton h:commandbutton.

2. wrong bean properties definition. @sjuan76 noted, utilize property #{bean.property}, should define public method public string getproperty(){} (case sensitive!) in managed bean.

in order avoid such problems in future you'd improve utilize ide (e.g. netbeans ide or intellij idea). ides have great autocompletion , validation facilities, javaee standard libraries.

3. mixing cdi , jsf dependency injection mechanisms. utilize cdi's @javax.inject.named @javax.enterprise.context.sessionscoped or jsf's @javax.faces.bean.managedbean @javax.faces.bean.sessionscoped.

4. wrong h:commandbutton action attribute. jsf specification (jsp tag documentation / h: / tag commandbutton) says of action attribute:

the look must evaluate public method takes no parameters, , returns object (the tostring() of called derive logical outcome) passed navigationhandler application

i.e. dologin() must not void. should homecoming e.g. string outcome, navigation rule exists in web-inf/faces-config.xml or view name should navigated to. e.g. if dologin() returns "success" user navigated success.xhtml; if dologin() returns "failure" user navigated failure.xhtml.

documentation jsf available @ jcp.org: jsf 2.2 spec.

if want show dialog instead of navigating different view, should utilize f:ajax tag h:commandbutton in login.xhtml partially render view. cannot utilize swing's joptionpane here, because client-side swing irrelevant server-side jsf.

ps recommend download jsf spec reference , get, example, "javaserver faces 2.0: finish reference" (ed burns, chris schalk). and, of course, utilize ide netbeans or intellij idea. boost experience jsf.

my login.xhtml works:

class="lang-html prettyprint-override"><?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"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/> <title>sign-in page</title> </head> <h:body> <h3>sign in</h3> <h:messages/> #{loginbean.emailid} <h:form> email id: <h:inputtext value="#{loginbean.emailid}" id="emailid" size="20"/> <br/> password: <h:inputsecret value="#{loginbean.password}" id="password"/> <h:commandbutton value="signin" action="#{loginbean.dologin}" id="signin"/> </h:form> </h:body> </html>

java eclipse jsf jboss

No comments:

Post a Comment