12 September 2016

JSF Validate Form Register Java NetBeans


index.xhtml
Java 2016
<?xml version='1.0' encoding='UTF-8' ?>
<!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://xmlns.jcp.org/jsf/html"
      xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h1>Register</h1>
        <h:form>
            Name: <h:inputText id="name" value="#{registerManagedBean.r.name}" required="true" requiredMessage="Enter your name!" />
            
            <h:message for="name" style="color: red"/>
            <br/>
           
            Address: <h:inputText id="address" value="#{registerManagedBean.r.address}" required="true" requiredMessage="Enter your address"/>
            
            <h:message for="address" style="color: red"/>
            <br/>
          
            Email: <h:inputText id="email" value="#{registerManagedBean.r.email}" required="true" requiredMessage="Enter your email!" validatorMessage="Khong dung dinh dang!">
            <f:validateRegex pattern="^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"/>
            </h:inputText>
            <h:message for="email" style="color: red"/>
            <br/>
           
            Phone: <h:inputText id="phone" value="#{registerManagedBean.r.phone}" required="true" requiredMessage="Enter your phone!" validatorMessage="Khong dung dinh dang">
            <f:validateRegex pattern="^[0-9]+$"/>
            </h:inputText>
            <h:message for="phone" style="color: red"/>
            <br/>
            
            Password: <h:inputSecret id="password" value="#{registerManagedBean.r.password}" required="true" requiredMessage="Enter your password!"/>
            
            <h:message for="password" style="color: red"/>
            <br/>
            
            <h:commandButton value="Register" action="#{registerManagedBean.hello()}"/>
        </h:form>
    </h:body>
</html>

RegisterManagedBean.java
Java 2016
package controller;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import model.Register;

/**
 *
 * @author Lonely
 */
@ManagedBean
@SessionScoped
public class RegisterManagedBean {

    private Register r = new Register();

    public Register getR() {
        return r;
    }

    public void setR(Register r) {
        this.r = r;
    }
    public String hello(){
        return "hello";
    }
}
Register.java
Java 2016
package model;

public class Register {

    private String name;
    private String address;
    private String email;
    private String phone;
    private String password;

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Register() {
    }

}
hello.jsp
Java 2016
<?xml version='1.0' encoding='UTF-8' ?>
<!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://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h1>Wellcome to user</h1>
    </h:body>
</html>
Cách viết style cho JSF: 
Markup
<div class="buttons">
    <h:commandButton value="Button Text" styleClass="apply"/>
    <h:commandButton value="Button Text" styleClass="cross"/>
</div>
CSS

.buttons .apply {
    background: #eee url(path/to/apply.png) no-repeat 0 50%;
} 

.buttons .cross {
    background: #eee url(path/to/cross.png) no-repeat 0 50%;
} 
public String login() throws SQLException {
        UserDAO udao = new UserDAO();
        if (udao.checkLogin(username, password)) {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
            session.setAttribute("user", username);
            return "show";
        } else {
            FacesContext fc = FacesContext.getCurrentInstance();
            fc.addMessage(null, new FacesMessage("Sai username hoặc password!"));
            return null;
        }
    }

public void logout() throws IOException {
        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
        ec.invalidateSession();
        ec.redirect(ec.getRequestContextPath() + "/faces/login.xhtml");
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang