Javascript 动态div被重置

Javascript 动态div被重置,javascript,jsf,jsf-2,Javascript,Jsf,Jsf 2,我试图在动态div中显示chatBean.selectedUser,该动态div是在单击h:commandLink时由javascript生成的。我面临的问题是,由于action=#{chatBean.refresh},每当我单击commandLink按钮时,页面都会刷新,因此动态div会被重置,我无法创建动态div。我需要提交表单以更新selectedUser属性,但表单提交会重置页面 1) 这是xhtml页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHT

我试图在动态div中显示
chatBean.selectedUser
,该动态div是在单击h:commandLink时由javascript生成的。我面临的问题是,由于
action=#{chatBean.refresh}
,每当我单击commandLink按钮时,页面都会刷新,因此动态div会被重置,我无法创建动态div。我需要提交表单以更新selectedUser属性,但表单提交会重置页面

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>
<script>
var counter = 0;
var numBoxes = 5;
function toggle4(showHideDiv) {
       var ele = document.getElementById(showHideDiv + counter);
       if(ele.style.display == "block") {
              ele.style.display = "none";
       }
       else {
              ele.style.display = "block";
       }
       if(counter == numBoxes) {
                document.getElementById("toggleButton").style.display = "none";
       }
}


</script>
</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.firstName}"/>
        </p:selectOneMenu>

        <h:commandLink onclick="counter++; toggle4('chat');" id="chat" value="Chat" action="#{chatBean.refresh}"></h:commandLink>
        </h:form>
        <br>
        </br>
        <h:outputLabel id="theLabel" value="#{chatBean.selectedUser}"/>


        </div>




</div>
 <div id="chat1" class="chat"><h:outputLabel value="#{chatBean.selectedUser}"/></div>
 <div id="chat2" class="chat"><h:outputLabel value="#{chatBean.selectedUser}"/></div>
 <div id="chat3" class="chat"><h:outputLabel value="#{chatBean.selectedUser}"/></div>
 <div id="chat4" class="chat"><h:outputLabel value="#{chatBean.selectedUser}"/></div>
 <div id="chat5" class="chat"><h:outputLabel value="#{chatBean.selectedUser}"/></div>
</h:body>
</html>
开始改变

public String refresh(){
    System.out.println("inside refresh...");
    return "NewFile";
}
进入


如果它对您正常工作,我会建议您做一些改进…

如果我使用
public void refresh(){System.out.println(“内部刷新…”);}
则它不会返回任何内容,页面也不会被刷新。
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;
    }

}
public String refresh(){
    System.out.println("inside refresh...");
    return "NewFile";
}
public void refresh(){
    System.out.println("inside refresh...");
}