(1)创建项目 选择web----->选择web application---->下一步起项目名称---->下一步选择Javaserver faces复选框---->完成
(2)source package上右键创建包 包名右键选择Javaserver faces------>JSF managed Bean---->下一步起类名----->完成
(3)在类里写要写的属性并生成get和set和要调用的方法(方法的要注意的事项时1.public
2,非静态的3.必须是String类型的;4.必须是无参的方法)例如做登录的例子:
属性及生成get和set方法和方法
private String name;
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPaw() {
return paw;
}
public void setPaw(String paw) {
this.paw = paw;
}
private String paw;
方法:
public String Login(){
if(this.type.equals("admin")){//根据登录页面下拉列表选择的值来进行判断
if(this.name.equals("accp") && this.paw.equals("accp")){//判断密码和姓名是否正确
return "ok";
}
else
{
return "err";
}
}
else
{
if(this.name.equals("123") && this.paw.equals("123")){
return "ok";
}
else
{
return "err";
}
}
(3)画页面
<h1><h:outputText value="JavaServer Faces" /></h1>
<h:panelGrid columns="3">//控制面板显示三行自动换行
登录名 :<h:inputText id="name" required="true" value="#{login.name}" requiredMessage="用户名不能为空!"></h:inputText>
<h:message for="name"></h:message>
登录密码:<h:inputSecret value="#{login.paw}" id="pwd" required="true" requiredMessage="用户名不能为空!"></h:inputSecret>
<h:message for="pwd"></h:message>
<h:selectOneMenu value="#{login.type}">
<f:selectItem itemLabel="管理员" itemValue="admin"></f:selectItem>
<f:selectItem itemLabel="普通用户" itemValue="123"></f:selectItem>
</h:selectOneMenu>
<h:commandButton value="提交" action="#{login.Login}"></h:commandButton>
</h:panelGrid>
</h:form>
转载于:https://blog.51cto.com/900104/538394