iLMS知識社群(Sites)師生部落格(Blogs)朝陽首頁(Homepage)TronClass教學平台登入
位置: 陳鉞勳 > 未分類
JSF
by 陳鉞勳 2018-02-27 18:12:28, 回應(9), 人氣(430)
NetBeans EE

<h:inputText value="#{guessBean.guess}"/>
<h:commandButton value="Submit" action="#{guessBean.checkAnswer()}"/>

Source Packages ->右鍵->New->JSF Managed Bean-> 找Scope 選 Session

Alt+Ins 自動完成 ()

自動顯示@SessionScoped 因預設為ctrl+space 但被輸入法搶走 所以快捷須自改
Tools -> option->Show Code Completion Popup->alt+a

http://primefaces.org/
回應(9)
faces-config.xml

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
              xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
    <managed-bean>
        <description>the current date</description> 
        <managed-bean-name>currentTime</managed-bean-name>
        <managed-bean-class>java.util.Date</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
</faces-config>

index.xhtml

<?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>
        Hello from Facelets
        <h:outputText value="Hellow Hellow Hellow" /><br/>
        <h:outputText value="Today is #{currentTime}"/>
    </h:body>
</html>

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.Random;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author M514
 */
@Named(value = "guessBean")
@SessionScoped
public class GuessBean implements Serializable {
    private int answer;
    private int guess;

    public int getAnswer() {
        return answer;
    }

    public void setAnswer(int answer) {
        this.answer = answer;
    }

    public int getGuess() {
        return guess;
    }

    public void setGuess(int guess) {
        this.guess = guess;
    }
   
    /**
     * Creates a new instance of GuessBean
     */
    public GuessBean() {
       
    }
   
    @PostConstruct
    public void reset(){
        this.guess = 0;
        this.answer=new Random().nextInt(100);
        System.out.println("The correct answer is"+ this.answer);
    }
    public void checkAnswer(){
        String msg="";
        if(answer==guess){
            msg="You got it!!!!";
        }else if(answer > guess){
            msg="Too low";
        }else if(answer < guess){
            msg="Too high";
        }
        FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(msg));
    }





<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>
        <h:form>           
            <h:inputText value="#{newJSFManagedBean1.a}"/><br/>
            <h:inputText value="#{newJSFManagedBean1.b}"/><br/>
            <h:outputText value="#{newJSFManagedBean1.c}"/><br/>
            <h:commandButton value="確認" action="#{newJSFManagedBean1.sum()}"/>     
        </h:form>
    </h:body>
</html>

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;

/**
 *
 * @author M514
 */
@Named(value = "newJSFManagedBean1")
@SessionScoped
public class NewJSFManagedBean1 implements Serializable {
      private int a,b,c;

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public int getB() {
        return b;
    }

    public void setB(int b) {
        this.b = b;
    }

    public int getC() {
        return c;
    }

    public void setC(int c) {
        this.c = c;
    }
    /**
     * Creates a new instance of NewJSFManaged
 public void sum(){
        String msg="";
        if(b==0){
             msg="The denominator cannot be zero!";
             c=0;
        }else{
            c=a/b;
        }
        FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(msg));
    }
 03/13小考

<h:form>          
           <h:outputText value="Numberator:"/>
           <h:inputText value="#{newJSFManagedBean.a}"/><br/>
           <h:outputText value="Denominatator:"/>
           <h:inputText value="#{newJSFManagedBean.b}"/><br/>
           <h:outputText value="Result :"/> 
           <h:outputText value="#{newJSFManagedBean.c}"/><br/>
           <h:commandButton value="GO" action="#{newJSFManagedBean.sum()}"/>               
</h:form>


private int a,b,c;

Alt+Ins->Getter and Setter -> 全選

public void sum(){
        String msg="";
        if(b==0){
             msg="The denominator cannot be zero!";
             c=0;
        }else{
            c=a/b;
        }
        FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(msg));
    }
Web Pages ->新建resources ->新建img->qq.jpg

<h:form>
            <h2>Test image</h2>
            <h:graphicImage url="/resources/img/qq.jpg"/>
            <h2>Test image</h2>
            <h:graphicImage library="img" name="qq.jpg"/>
          //換頁
            <h:commandLink action="registration">Registration</h:commandLink>
 
        </h:form>
private String name,tel;
    private UICommand commandComponent;

    public String getName() {
        return name;
    }

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

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public UICommand getCommandComponent() {
        return commandComponent;
    }

    public void setCommandComponent(UICommand commandComponent) {
        this.commandComponent = commandComponent;
    }
    public void turnImmediateOn(){
        commandComponent.setImmediate(true);
    }
    public void turnImmediateOff(){
        commandComponent.setImmediate(false);
    }
    public String cancel(){
        return "index";
    }
    public boolean getimmediateStatus(){
        return commandComponent.isImmediate();
    }



index.html

 <h:form>           
            <h:panelGrid columns="2">
               <h:outputLabel for="name" value="Name"/>
                <h:inputText id="name" required="true" requiredMessage="please provide your name"></h:inputText>
                <h:outputLabel for="tel" value="Tel :"/>
                <h:inputText id="tel" required="true" requiredMessage="please provide your number"></h:inputText>
            </h:panelGrid>
            <h:commandButton value="Cancel" action="#{newJSFManagedBean.cancel()}" binding="#{newJSFManagedBean.commandComponent}"/>          
            <h:commandButton value="Immediate ON" actionListener="#{newJSFManagedBean.turnImmediateOn()}" immediate="true"/> 
            <h:commandButton value="Immediate Off" actionListener="#{newJSFManagedBean.turnImmediateOff()}" immediate="true"/>

        </h:form>
 registration.xhtml
<h:body>
        <h:form>
            <h:outputLabel for="email" value="Email :"/>
            <h:inputText id="email" required="true" requiredMessage="please provide your Email" value="#{index.email}"></h:inputText>
            <h:outputLabel for="password" value="Password :"/>
            <h:inputText id="password" required="true" requiredMessage="please provide your Password" value="#{index.password}"></h:inputText>
            <p></p>
                <h:commandButton value="Cancel" action="#{index.cancel()}" immediate="true"></h:commandButton>
                 <h:commandButton value="Submit" action="#{index.submit()}"></h:commandButton>
        </h:form>
    </h:body>

@RequestScoped
public class index {
    private String email,password;

    public String getEmail() {
        return email;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
           
    /**
     * Creates a new instance of index
     */
    public index() {
    }
    public String cancel(){
        return "index";
    }
     public String submit(){   
        return "display";
    }
}

display.xhtml
<h:body>
       <h:outputText value="email:#{index.email}"/><br/>
       <h:outputText value="password :#{index.password}"/><br/>
 </h:body>

index.xhtml
 <h:form>
            <h:commandLink action="registration">Welcome to registration</h:commandLink>
</h:form>

index.xhtml
<h:body>
        <h:form>
            Item Label : <h:inputText required="true" requiredMessage="please provide yuor label"
                                      value="#{SelectionBean.itemLabelToAdd}"/><br />
            Item Value: <h:inputText required="true" requiredMessage="please provide yuor value"
                                     value="#{SelectionBean.itemValueToAdd}"/> <br />
            <h:commandButton value="Add" action="#{SelectionBean.addItem()}"/>
            <p>Select One List-Box</p>
            <h:selectOneListbox size="6" value="#{SelectionBean.selectedValue}">
                <f:selectItem itemLabel="Please Select one" itemValue="null"/>
                <f:selectItems value="#{SelectionBean.options}"/>
            </h:selectOneListbox>
            <p>Select One Drop-Down Menu </p>
            <h:selectOneRadio  value="#{SelectionBean.selectedValue}"  >
                <f:selectItem itemLabel="Select" itemValue="null" />
                <f:selectItems value="#{SelectionBean.options}" />
            </h:selectOneRadio> <br/>
            The value you select: <h:outputText value="#{SelectionBean.selectedValue}" /> <br/>
            <h:commandButton value="Refresh selected value field"  />
        </h:form>
    </h:body>


SelectioBean.jave

private String itemLabelToAdd;
private String itemValueToAdd;
private List<SelectItem> options;
private String selectedValue;
//
 public SelectionBean() {
         options = new ArrayList<>();
    }
    public String getItemLabelToAdd() {
        return itemLabelToAdd;
    }

    public void setItemLabelToAdd(String itemLabelToAdd) {
        this.itemLabelToAdd = itemLabelToAdd;
    }

    public String getItemValueToAdd() {
        return itemValueToAdd;
    }

    public void setItemValueToAdd(String itemValueToAdd) {
        this.itemValueToAdd = itemValueToAdd;
    }

    public List<SelectItem> getOptions() {
        return options;
    }

    public void setOptions(List<SelectItem> options) {
        this.options = options;
    }

    public String getSelectedValue() {
        return selectedValue;
    }

    public void setSelectedValue(String selectedValue) {
        this.selectedValue = selectedValue;
    }

    //
    public void addItem(){
         options.add(new SelectItem(this.itemValueToAdd, this.itemLabelToAdd));
    }

回應