سؤال

I am new to jsf. I had trouble in working in ajax and viewscope bean.

Here is my code:

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://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
    <h:form>

        <h:inputText id ="text" value="#{bean2.str1}" />
        <h:inputText id ="text2" value="#{bean2.str1}" />
        <h:commandButton value="Test">
            <f:ajax execute="text" render="text2"/>
        </h:commandButton>

        <h:commandButton value="Home" action="home"/>
    </h:form>        
</h:body>
</html>

Bean2.java

package com.hem.beans;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class Bean2 implements Serializable {

public String str1;

public String getStr1() 
{
    return str1;
}
public void setStr1(String str) 
{
    str1 = str;
}
}

The page shows up with 2 inputboxes and a command Button "Test". When I enter value in the first inputbox and click Test nothing happens (no error either).

But when In I make the bean as Sessionscoped It works fine.

Please guide me through this.

هل كانت مفيدة؟

المحلول

It should work fine with @ViewScoped.. However, you can change the value of the execute attribute's value by: @form in order to submit all the form :

<h:form>

    <h:inputText id ="text" value="#{bean2.str1}" /><br />
    <h:outputText id ="text2" value="#{bean2.str1}" /><br />
    <h:commandButton value="Test">
        <f:ajax execute="@form" render="text2"/>
    </h:commandButton>

    <h:commandButton value="Home" action="home"/>
</h:form>  

Also, you had better make the result shows in a <h:outputText /> tag component rather than another <h:inputText />.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top