将selectOneRadio与Ajax一起使用

时间:2012-07-19 09:00:10

标签: ajax jsf jsf-2 primefaces radio-button

当我在加载index.xhtml页面后尝试此代码时,该页面是我的欢迎页面(并且首页包含strana1.xhtml初始包含为​​p:outputPanel“sadrzaj”的内容)并单击“Napred”按钮(意味着转发) )一切正常,我得到了ajax的更新 p:outputPanel id =“sadrzaj”(页面strana2.xhtml加载并替换index.xhtml页面内的页面strana1.xhtml,位于p:outputPanel“sadrzaj”下)。 但之后,任何radioButtons的更改(这是页面strana2.xhtml的一部分)都不会触发任何ajax请求,并且按钮“Nazad”(意味着返回)也不起作用(点击后没有任何事情发生)。可能有什么问题?

Page index.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:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title>Proba1</title>
    </h:head>
    <h:body>
        <h:form> 
            <h:panelGroup id="sadrzaj">
                <ui:include src="#{user.sadrzaj}"/>
            </h:panelGroup>

            <h:panelGrid id="dugmad" columns="2">
                <p:commandButton value="Nazad" disabled="#{user.iscrtajNazad}" 
                                 actionListener="#{user.nazad}" update="sadrzaj, dugmad"/>
                <p:commandButton value="Napred" disabled="#{user.iscrtajNapred}" 
                                 actionListener="#{user.napred}" update="sadrzaj, dugmad"/>
            </h:panelGrid>
        </h:form>
    </h:body>
</html>

最初包含在index.xhtml中的Page strana1.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://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>IGNORED</title>
    </h:head>
    <h:body>
        <h3>Dobrodoslicu zelim svima!</h3>
        <p:outputLabel for="tekst1" value="Tekst1: "/>
        <p:inputText id="tekst1" value="#{user.tekst1}"/>

        <br/>

        <p:selectBooleanCheckbox id="personalizovana" value="#{user.personalizovana}"/>
        <p:outputLabel for="personalizovana" value="personalizovana"/>

        <br/>

        <h:outputText value="Broj strana: "/>
        <p:inputText value="#{user.brojStrana}"/>

        <br/>

        <p:outputLabel for="datum" value="Datum: "/>
        <p:calendar id="datum" mode="popup" showOn="button" pattern="dd/MM/yyyy" 
                    value="#{user.datum}"/>
    </h:body>
</html>

Page strana2.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://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>IGNORED</title>
    </h:head>
    <h:body>      
        <p:selectOneRadio id="izborNacinaKreiranjaAnkete" 
                          value="#{user.izborNacinaKreiranjaAnkete}" >
            <f:selectItems value="#{user.ponudaNacinaKreiranjaAnkete}"/>
            <f:ajax event="change" listener="#{user.izborNacinaKreiranjaAnketeListener}" 
                                   render="sadrzajNacinaKreiranjaAnkete"/>
        </p:selectOneRadio>

        <br/>

        <p:outputPanel id="sadrzajNacinaKreiranjaAnkete">
            <h:outputText value="#{user.text}"/>
        </p:outputPanel>
    </h:body>
</html>    

这是我的名为“user”的bean:

   package beans;

    import java.io.Serializable;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    import javax.faces.event.ActionEvent;
    import javax.faces.event.AjaxBehaviorEvent;
    import javax.faces.model.SelectItem;


    @ManagedBean(name="user")
    @SessionScoped
    public class UserBean implements Serializable {
        private SelectItem[] ponudaNacinaKreiranjaAnkete = {
            new SelectItem("1", "postojeca pitanja"),
            new SelectItem("2", "kreiraj pitanje")
        };

        private String sadrzaj="strana1.xhtml";
        private String tekst1;
        private java.util.Date datum;
        private boolean bolje;
        private boolean personalizovana;
        private String brojStrana;
        private String izborNacinaKreiranjaAnkete = "2";
        private String sadrzajNacinaKreiranjaAnkete = "kreirajPitanje.xhtml";
        private boolean iscrtajNazad = true;
        private boolean iscrtajNapred = false;
        private String text = "Drugi text";
        private String homeSadrzaj = "homePrva.xhtml";

        public void napred(ActionEvent e) {
            setSadrzaj("strana2Proba.xhtml");
            setIscrtajNazad(false);
            setIscrtajNapred(true);
        }

        public void nazad(ActionEvent e) {
            setSadrzaj("strana1.xhtml");
            setIscrtajNazad(true);
            setIscrtajNapred(false);
        }

        /**
         * @return the sadrzaj
         */
        public String getSadrzaj() {
            return sadrzaj;
        }

        /**
         * @param sadrzaj the sadrzaj to set
         */
        public void setSadrzaj(String sadrzaj) {
            this.sadrzaj = sadrzaj;
        }

        /**
         * @return the tekst1
         */
        public String getTekst1() {
            return tekst1;
        }

        /**
         * @param tekst1 the tekst1 to set
         */
        public void setTekst1(String tekst1) {
            this.tekst1 = tekst1;
        }

        /**
         * @return the datum
         */
        public java.util.Date getDatum() {
            return datum;
        }

        /**
         * @param datum the datum to set
         */
        public void setDatum(java.util.Date datum) {
            this.datum = datum;
        }

        /**
         * @return the bolje
         */
        public boolean isBolje() {
            return bolje;
        }

        /**
         * @param bolje the bolje to set
         */
        public void setBolje(boolean bolje) {
            this.bolje = bolje;
        }

        /**
         * @return the iscrtajNazad
         */
        public boolean isIscrtajNazad() {
            return iscrtajNazad;
        }

        /**
         * @param iscrtajNazad the iscrtajNazad to set
         */
        public void setIscrtajNazad(boolean iscrtajNazad) {
            this.iscrtajNazad = iscrtajNazad;
        }

        /**
         * @return the iscrtajNapred
         */
        public boolean isIscrtajNapred() {
            return iscrtajNapred;
        }

        /**
         * @param iscrtajNapred the iscrtajNapred to set
         */
        public void setIscrtajNapred(boolean iscrtajNapred) {
            this.iscrtajNapred = iscrtajNapred;
        }

        /**
         * @return the personalizovana
         */
        public boolean isPersonalizovana() {
            return personalizovana;
        }

        /**
         * @param personalizovana the personalizovana to set
         */
        public void setPersonalizovana(boolean personalizovana) {
            this.personalizovana = personalizovana;
        }

        /**
         * @return the brojStrana
         */
        public String getBrojStrana() {
            return brojStrana;
        }

        /**
         * @param brojStrana the brojStrana to set
         */
        public void setBrojStrana(String brojStrana) {
            this.brojStrana = brojStrana;
        }

        /**
         * @return the izborNacinaKreiranjaAnkete
         */
        public String getIzborNacinaKreiranjaAnkete() {
            return izborNacinaKreiranjaAnkete;
        }

        /**
         * @param izborNacinaKreiranjaAnkete the izborNacinaKreiranjaAnkete to set
         */
        public void setIzborNacinaKreiranjaAnkete(String izborNacinaKreiranjaAnkete) {
            this.izborNacinaKreiranjaAnkete = izborNacinaKreiranjaAnkete;
        }

        /**
         * @return the ponudaNacinaKreiranjaAnkete
         */
        public SelectItem[] getPonudaNacinaKreiranjaAnkete() {
            return ponudaNacinaKreiranjaAnkete;
        }

        /**
         * @param ponudaNacinaKreiranjaAnkete the ponudaNacinaKreiranjaAnkete to set
         */
        public void setPonudaNacinaKreiranjaAnkete(SelectItem[] ponudaNacinaKreiranjaAnkete) {
            this.ponudaNacinaKreiranjaAnkete = ponudaNacinaKreiranjaAnkete;
        }

        public void izborNacinaKreiranjaAnketeListener(AjaxBehaviorEvent e) {
            if(izborNacinaKreiranjaAnkete.equals("1"))
                text = "Prvi tekst";
            else if(izborNacinaKreiranjaAnkete.equals("2"))
                text = "Drugi tekst";
        }

        /**
         * @return the sadrzajNacinaKreiranjaAnkete
         */
        public String getSadrzajNacinaKreiranjaAnkete() {
            return sadrzajNacinaKreiranjaAnkete;
        }

        /**
         * @param sadrzajNacinaKreiranjaAnkete the sadrzajNacinaKreiranjaAnkete to set
         */
        public void setSadrzajNacinaKreiranjaAnkete(String sadrzajNacinaKreiranjaAnkete) {
            this.sadrzajNacinaKreiranjaAnkete = sadrzajNacinaKreiranjaAnkete;
        }


        public void prvi(ActionEvent e) {
            setHomeSadrzaj("homePrva.xhtml");
        }

        public void drugi(ActionEvent e) {
            setHomeSadrzaj("index.xhtml");
        }

        /**
         * @return the text
         */
        public String getText() {
            return text;
        }

        /**
         * @param text the text to set
         */
        public void setText(String text) {
            this.text = text;
        }

        /**
         * @return the homeSadrzaj
         */
        public String getHomeSadrzaj() {
            return homeSadrzaj;
        }

        /**
         * @param homeSadrzaj the homeSadrzaj to set
         */
        public void setHomeSadrzaj(String homeSadrzaj) {
            this.homeSadrzaj = homeSadrzaj;
        }


        public void izmeni(ActionEvent e) {
            sadrzaj = "homePrva.xhtml";
        }

        public void izmeni2(ActionEvent e) {
            text = "Prvi tekst";
        }
    }

此外,我尝试从p:selectOneRadio中移除f:ajax,然后按钮“Nazad”按原样移动, 我可以更改outputPanel“sadrzaj”,只需单击“Napred”(表示正向)和“Nazad”(表示返回)按钮。 我也尝试将页面strana2.xhtml作为我的欢迎页面,如下所示:

Page strana2.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://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>IGNORED</title>
    </h:head>
    <h:body>
        <h:form>

        <p:selectOneRadio id="izborNacinaKreiranjaAnkete" 
                          value="#{user.izborNacinaKreiranjaAnkete}" >
            <f:selectItems value="#{user.ponudaNacinaKreiranjaAnkete}"/>
            <f:ajax event="change" listener="#{user.izborNacinaKreiranjaAnketeListener}" 
                                   render="sadrzajNacinaKreiranjaAnkete"/>
        </p:selectOneRadio>

         <br/>

        <p:outputPanel id="sadrzajNacinaKreiranjaAnkete">
            <h:outputText value="#{user.text}"/>
        </p:outputPanel>

        </h:form>
    </h:body>
</html>

结果很好,一切正常,但在index.xhtml页面内(当然我删除了这些h:form标签)它不起作用。

请有人知道为什么会这样吗?

这是我的问题的一部分,只是为了让它更清晰:

我正在使用netBeans7.1.2,Tomcat7.0.22和Primefaces 3.3.1,我有时会在服务器控制台中收到错误消息,但我没有看到这些消息对我的项目有任何影响,实际上我想要说我有时会遇到这些错误,即使我的项目有效,有时即使我的项目不起作用,我也没有得到任何消息。这些消息是:

第一条消息:

SEVERE: IOException while loading persisted sessions: java.io.InvalidClassException: beans.UserBean; local class incompatible: stream classdesc serialVersionUID = -5783497785664294581, local class serialVersionUID = -8328898459300814685
java.io.InvalidClassException: beans.UserBean; local class incompatible: stream classdesc serialVersionUID = -5783497785664294581, local class serialVersionUID = -8328898459300814685
    at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:604)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1601)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1514)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1750)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    at org.apache.catalina.session.StandardSession.readObject(StandardSession.java:1613)
    at org.apache.catalina.session.StandardSession.readObjectData(StandardSession.java:1073)
    at org.apache.catalina.session.StandardManager.doLoad(StandardManager.java:284)
    at org.apache.catalina.session.StandardManager.load(StandardManager.java:204)
    at org.apache.catalina.session.StandardManager.startInternal(StandardManager.java:470)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5241)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:812)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:787)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:607)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:633)
    at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:558)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:468)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1329)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:389)
    at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:334)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1041)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:774)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1033)
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:291)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:727)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:148)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:621)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:450)

,另一个是:

NotifyUtil::java.net.SocketException: Software caused connection abort: recv failed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:150)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
    at java.io.FilterInputStream.read(FilterInputStream.java:133)
    at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3035)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at java.io.BufferedReader.fill(BufferedReader.java:154)
    at java.io.BufferedReader.readLine(BufferedReader.java:317)
    at java.io.BufferedReader.readLine(BufferedReader.java:382)
    at org.netbeans.modules.web.monitor.server.NotifyUtil$RecordSender.run(NotifyUtil.java:299)

如果我点击任何按钮或单选按钮,我会在Firebug控制台中收到此消息:

<?xml version='1.0' encoding='UTF-8'?>
<partial-response><error><error-name>class java.lang.ClassCastException</error-name><error-message><![CDATA[javax.faces.component.StateHolderSaver cannot be cast to [Ljava.lang.Object;]]></error-message></error></partial-response>

0 个答案:

没有答案