JSF作登陆及判断

(1)建立项目 选择web----->选择web application---->下一步起项目名称---->下一步选择Javaserver faces复选框---->完成web

(2)source package上右键建立包  包名右键选择Javaserver faces------>JSF managed  Bean---->下一步起类名----->完成app

(3)在类里写要写的属性并生成get和set和要调用的方法(方法的要注意的事项时1.publicide

2,非静态的3.必须是String类型的;4.必须是无参的方法)例如作登陆的例子:ui

属性及生成get和set方法和方法this

  private String name;
    private String type;orm

    public String getType() {
        return type;
    }server

    public void setType(String type) {
        this.type = type;
    }get

    public String getName() {
        return name;
    }input

    public void setName(String name) {
        this.name = name;
    }it

    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)画页面

 
代码以下:
 <h:form>
            <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>
(4)配置文件跳转路径
(5)运行
相关文章
相关标签/搜索