再一次打扰我了,有人知道如何解决此错误/index.xhtml @ 17,95 value =“#{user.firstname}”:无法到达目标,标识符'user'解析为空
我有geeter和seeter方法以及@ManagedBean和@SessionScoped,那么为什么netnet起作用? tnx的答案。所以我有2个xhtml文件和一个java文件...问题可能在java文件中,但是我不知道为什么。
<h:head>
<title>JSf ragistration form</title>
</h:head>
<h:body>
<h:form id="form">
<h2>JSf registration app</h2>
<h4>registration form</h4>
<table>
<tr>
<td><h:outputLabel for="Fname">first name</h:outputLabel></td>
<td><h:inputText id="first-id" value="#{user.firstname}" required="true"/></td>
</tr>
<tr>
<td> <h:outputLabel for="Lname">last name</h:outputLabel></td>
<td>
<h:inputText id="Last-id" value="#{user.lastname}" required="true"/>
</td>
</tr>
<tr>
<td> <h:outputLabel for="sex">Sex</h:outputLabel> </td>
<td> <h:selectOneRadio id="sex-id" value="#{user.sex}" required="true">
<f:selectItem itemLabel="male" itemValue="Male"/>
<f:selectItem itemLabel="female" itemValue="Female" />
</h:selectOneRadio>
</td>
</tr>
<tr>
<td> <h:outputLabel for="date">Date of birth</h:outputLabel></td>
<td><h:inputText id="date-id" value="#{user.dat}" required="true">
<f:convertDateTime pattern="MM-dd-yy"/>
</h:inputText>
<br/>
</td>
</tr>
<tr>
<td> <h:outputLabel id="email">Email</h:outputLabel></td>
<td>
<h:inputText id="email-id" value="#{user.email}" required="true" validator="#{user.validateEmail(context, toValidate, value)}"/>
</td>
</tr>
<tr>
<td> <h:outputLabel for="ser lev">Service lebel</h:outputLabel></td>
<td>
<h:selectOneMenu id="sel-id" value="#{user.servicelevel}" required="true">
<f:selectItem itemLabel="madium" itemValue="Medium"/>
<f:selectItem itemLabel="basic" itemValue="basic"/>
<f:selectItem itemLabel="premium" itemValue="Premium"/>
</h:selectOneMenu>
</td>
</tr>
</table>
<h:commandButton value="Edit" action="register" />
<h:commandButton value="Confirm" action="reg.xhtml" />
</h:form>
</h:body>
public class User {
String firstname;
String lastname;
Date dat;
String sex;
String email;
String servicelevel="medium";
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public void setDat(Date dat) {
this.dat = dat;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setEmail(String email) {
this.email = email;
}
public void setServicelevel(String servicelevel) {
this.servicelevel = servicelevel;
}
public String getFirstname() {
return firstname;
}
public String getLastname() {
return lastname;
}
public Date getDat() {
return dat;
}
public String getSex() {
return sex;
}
public String getEmail() {
return email;
}
public String getServicelevel() {
return servicelevel;
}
public void validateEmail(FacesContext context,UIComponent toValidate,Object value) throws ValidatorException
{
String emailstr=(String)value;
if(-1==emailstr.indexOf("@"))
{
FacesMessage msg=new FacesMessage("invalid email addres");
throw new ValidatorException(msg);
}
}
public String addConfirmedUser()
{
boolean aded=true;
FacesMessage done=null;
String outcome=null;
if(aded)
{
done=new FacesMessage("sucesfuly added new user");
outcome="done";
}
else
{
done=new FacesMessage("failed to add new user");
outcome="reigster";
}
FacesContext.getCurrentInstance().addMessage(null, done);
return outcome;
}
}
<h:head>
<title>user comformation</title>
</h:head>
<h:body>
<table>
<tr>
<td> <b> First name: </b></td>
<td> <h:outputText value="#{user.firstname}"/><br/></td>
</tr>
<tr>
<td> <b> last name: </b></td>
<td> <h:outputText value="#{user.lastname}"/><br/></td>
</tr>
<tr>
<td> <b> sex </b></td>
<td> <h:outputText value="#{user.sex}"/><br/></td>
</tr>
<tr>
<td> <b> date: </b></td>
<td> <h:outputText value="#{user.dat}"/><br/></td>
</tr>
<tr>
<td> <b> EMAIL: </b></td>
<td> <h:outputText value="#{user.email}"/><br/></td>
</tr>
<tr>
<td> <b> service level </b></td>
<td> <h:outputText value="#{user.servicelevel}"/><br/></td>
</tr>
</table>
</h:body>