2012-10-02 100 views
0

我面臨的問題是,我點擊h:commandLink按鈕後,h:outputLabel沒有顯示任何東西。我已經使用selectedUser來存儲表單提交時的值,但似乎selectedUser沒有存儲任何值。h:outputLabel沒有顯示任何東西

1)這裏是xhtml文件。

<!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:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:p="http://primefaces.org/ui" 
    xmlns:c="http://java.sun.com/jsp/jstl/core"> 


<h:head></h:head> 



<h:body class="thrColElsHdr"> 


     <div class="friends"> 

     <h4>Friends</h4> 
     <h:form> 
     <p:selectOneMenu value="#{chatBean.selectedUser}"> 
     <f:selectItems value="#{chatBean.friendList}" var="users" itemLabel="#{users.firstName}" itemValue="#{users}"/> 
     </p:selectOneMenu> 

     <h:commandLink>Chat</h:commandLink> 
     </h:form> 
     <br> 
     </br> 
     <h:outputLabel value="#{chatBean.selectedUser.firstName}"/> 


     </div> 




</h:body> 
</html> 

2)這裏是chatBean,它是會話範圍使用facesconfig.xml

package com.bean; 

import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 

import javax.faces.context.FacesContext; 
import javax.persistence.EntityManager; 
import javax.persistence.EntityManagerFactory; 
import javax.persistence.Persistence; 
import javax.persistence.Query; 
import javax.servlet.http.HttpSession; 

import com.entity.Friend; 
import com.entity.User; 

public class ChatBean { 
    private EntityManager em; 
    private User selectedUser; 


    public User getSelectedUser() { 
     return selectedUser; 
    } 


    public void setSelectedUser(User selectedUser) { 
     this.selectedUser = selectedUser; 
    } 


    public ChatBean(){ 
     selectedUser= new User(); 
     EntityManagerFactory emf=Persistence.createEntityManagerFactory("FreeBird"); 
     em =emf.createEntityManager(); 
    } 


public List<User> getFriendList(){ 

     FacesContext context = FacesContext.getCurrentInstance(); 
     HttpSession session = (HttpSession) context.getExternalContext().getSession(true); 
     User user=(User) session.getAttribute("userdet"); 
     Query query = em.createQuery("SELECT f FROM Friend f WHERE f.email='"+user.getEmail()+"' AND f.status=1",Friend.class); 
     List<Friend> results =query.getResultList(); 
     ArrayList<User> friends = new ArrayList<User>(); 
     Iterator<Friend> it = results.iterator(); 
      while(it.hasNext()){ 
       System.out.println("in getFriendList..."); 

      User friend =em.find(User.class,it.next().getFriendEmail()); 
      friends.add(friend); 
     } 
      return friends; 
} 
} 

3)下面是用戶實體類

package com.entity; 

import java.io.Serializable; 
import javax.persistence.*; 


/** 
* The persistent class for the user database table. 
* 
*/ 
@Entity 
public class User implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @Id 
    private String email; 

    @Lob() 
    private String aboutMe; 

    private String birthDate; 

    private String city; 

    private String college; 

    private String confirmation; 

    private String contactNo; 

    private String country; 

    private String degree; 

    private String firstName; 

    private String gender; 

    private String highSchool; 

    private String image; 

    private String interest; 

    private String lastName; 

    private String password; 

    private int pincode; 

    @Lob() 
    private String quote; 

    private String relationship; 

    private String secondarySchool; 

    private String state; 

    private String street; 

    private String university; 

    private String userName; 

    public User() { 
    } 

    public String getEmail() { 
     return this.email; 
    } 

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

    public String getAboutMe() { 
     return this.aboutMe; 
    } 

    public void setAboutMe(String aboutMe) { 
     this.aboutMe = aboutMe; 
    } 

    public String getBirthDate() { 
     return this.birthDate; 
    } 

    public void setBirthDate(String birthDate) { 
     this.birthDate = birthDate; 
    } 

    public String getCity() { 
     return this.city; 
    } 

    public void setCity(String city) { 
     this.city = city; 
    } 

    public String getCollege() { 
     return this.college; 
    } 

    public void setCollege(String college) { 
     this.college = college; 
    } 

    public String getConfirmation() { 
     return this.confirmation; 
    } 

    public void setConfirmation(String confirmation) { 
     this.confirmation = confirmation; 
    } 

    public String getContactNo() { 
     return this.contactNo; 
    } 

    public void setContactNo(String contactNo) { 
     this.contactNo = contactNo; 
    } 

    public String getCountry() { 
     return this.country; 
    } 

    public void setCountry(String country) { 
     this.country = country; 
    } 

    public String getDegree() { 
     return this.degree; 
    } 

    public void setDegree(String degree) { 
     this.degree = degree; 
    } 

    public String getFirstName() { 
     return this.firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getGender() { 
     return this.gender; 
    } 

    public void setGender(String gender) { 
     this.gender = gender; 
    } 

    public String getHighSchool() { 
     return this.highSchool; 
    } 

    public void setHighSchool(String highSchool) { 
     this.highSchool = highSchool; 
    } 

    public String getImage() { 
     return this.image; 
    } 

    public void setImage(String image) { 
     this.image = image; 
    } 

    public String getInterest() { 
     return this.interest; 
    } 

    public void setInterest(String interest) { 
     this.interest = interest; 
    } 

    public String getLastName() { 
     return this.lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public String getPassword() { 
     return this.password; 
    } 

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

    public int getPincode() { 
     return this.pincode; 
    } 

    public void setPincode(int pincode) { 
     this.pincode = pincode; 
    } 

    public String getQuote() { 
     return this.quote; 
    } 

    public void setQuote(String quote) { 
     this.quote = quote; 
    } 

    public String getRelationship() { 
     return this.relationship; 
    } 

    public void setRelationship(String relationship) { 
     this.relationship = relationship; 
    } 

    public String getSecondarySchool() { 
     return this.secondarySchool; 
    } 

    public void setSecondarySchool(String secondarySchool) { 
     this.secondarySchool = secondarySchool; 
    } 

    public String getState() { 
     return this.state; 
    } 

    public void setState(String state) { 
     this.state = state; 
    } 

    public String getStreet() { 
     return this.street; 
    } 

    public void setStreet(String street) { 
     this.street = street; 
    } 

    public String getUniversity() { 
     return this.university; 
    } 

    public void setUniversity(String university) { 
     this.university = university; 
    } 

    public String getUserName() { 
     return this.userName; 
    } 

    public void setUserName(String userName) { 
     this.userName = userName; 
    } 

} 
+0

你'時'缺少方法表達。另外,你的瘋狂沒有辦法去做你想做的事情。 –

+0

你的意思是action或actionListener屬性?其實我不想在點擊鏈接時觸發任何方法,我只想通過使用outputLabel顯示selectedUser.firstName的值。我想,當我提交表單時,selectedUser的值會被實例化。 –

+1

@BheshGurung您可以更新該值,而無需執行定義的操作。不過,你需要以某種方式更新控件。您應刷新整個頁面或通過ajax渲染更新控件。 –

回答

3

你的管理bean必須有相應的註釋:

@ViewScoped 
@ManagedBean 
public class ChatBean { 
    //... 
} 

這個例子可以使用這些技巧的一個更正爲:

  • 添加action<h:commandLink>並將其綁定到返回你的網頁的名稱的方法。通過這種方式,頁面將會被刷新,它會顯示在#{chatBean.selectedUser.firstName}新值:

    <h:commandLink action="#{chatBean.refresh}" value="Chat" /> 
    

在你的類:

public String refresh() { 
    return "index"; 
} 
  • <h:commandLink>添加AJAX行爲爲了更新<h:outputLabel>。您應該添加一個id屬性到您的<h:outputLabel>

    <h:commandLink value="Chat"> 
        <f:ajax render=":theLabel" /> 
    </h:commandLink> 
    <!-- jsf code --> 
    <h:outputLabel id="theLabel" value="#{chatBean.selectedUser.firstName}"/> 
    
+0

我是否需要爲「索引」結果設置任何導航規則? –

+0

用''''h:selectOneMenu'和'f:selectItems'怎麼辦?他不需要轉換器嗎? –

+1

這是在JSF 1.x的舊時代。自JSF 2.0以來,您只需返回要導航的視圖名稱,框架就會查找視圖(注意:不需要在返回的String的末尾添加'xhtml')。 –