我在实现对我的托管 Bean 的 Ajax 调用时遇到了很多问题。我准备了一个简单的测试用例 - 我希望 outputText 值更改为 Beta,但它仍为 Alpha。我究竟做错了什么?
package com.example.controllers;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;
@Named(value = "tester")
@ViewScoped
public class tester{
private String testString;
/**
* Creates a new instance of tester
*/
public tester() {
testString = "Alpha";
}
public String changeText(){
testString = "Beta";
return null;
}
/**
* @return the testString
*/
public String getTestString() {
return testString;
}
/**
* @param testString the testString to set
*/
public void setTestString(String testString) {
this.testString = testString;
}
}
<?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"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:commandButton value="Add Row" action="#{tester.changeText}">
<f:ajax event="action" execute="@form" render="out"></f:ajax>
</h:commandButton>
<h:outputText id="out" value="#{tester.testString}"/>
</h:form>
</h:body>
</html>