[英]JSF - f:ajax execute does not update the backing property of bean
我有兩個組件,一個commandButton和一個selectOneListbox。
單擊命令按鈕后,我需要更新綁定到selectOneListBox的后備屬性的值。
兩個組件都在同一個<h:form>
。 支持Bean是Named / SessionScoped Bean。
這是我的頁面中定義組件的部分:
按鍵:
<h:commandButton formnovalidate="formnovalidate" title="Select Variable"
type="button" styleClass="button_green close_window"
tabindex="2" id="btnSelectVar" value="Confirm">
<f:ajax execute="lstVarSelected" event="click"
listener="#{systemOptionsControl.updateText}" onevent="updateParent"/>
</h:commandButton>
SelectOneListBox: (selectedVariable is a String and variables is a List of String)
<h:selectOneListbox id="lstVarSelected" name="lstVarSelected"
tabindex="1" value="#{systemOptionsControl.selectedVariable}" >
<f:selectItems value="#{systemOptionsControl.variables}"/>
</h:selectOneListbox>
單擊按鈕后,我需要獲取oneListBox的后備屬性,但是JSF不會在后備bean上調用set方法。 然后,當我嘗試獲取updateText方法的值時,出現了NullPointerException。
我嘗試過很多解決方案,但都沒有用,任何人都可以提出任何想法?
這是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:escriba="http://java.sun.com/jsf/composite/components/escriba"
xmlns:f="http://java.sun.com/jsf/core" lang="pt-br" class="no-js">
<body>
<h:head>
<h:outputScript name="jsf.js" library="javax.faces"/>
<script language="javascript">
function callParentButton(data) {
//Verifica o status da execução do ajax.
var status = data.status;
//alert(status);
switch(status)
{
case 'begin':
break;
case 'complete':
//Gets the name of parent button
var a = #{systemOptionsControl.idBtnAtualizarTextAreas};
//Calls the click.
var btn = document.getElementById(a.valueOf().name);
btn.click();
break;
}
}
</script>
</h:head>
<h:form prependId="false" id="frmVariables">
<section class="box_padrao">
<header id="box_padrao_header">
<h1>System Options</h1>
<div id="actions_menu">
<h:commandButton type="button" title="Select Variable" styleClass="button_green" tabindex="2"
id="btnConfirmar" value="Confirm">
<f:ajax execute="lstVariables" event="click" listener="systemOptionsControl.updateText" onevent="callParentButton"/>
</h:commandButton>
<button type="button" title="Cancel" class="button_red close_window" tabindex="3" id="btnCancelar" name="btnCancel">Cancel</button>
</div>
</header>
<div class="box_padrao_content">
<div class="grid_100">
<h2 class="legend">» Variables List </h2>
<fieldset class="fieldset">
<div class="grid_100">
<h:selectOneListbox id="lstVariables" name="lstVariables" tabindex="1" value="#{systemOptionsControl.selectedVariable}" >
<f:selectItems value="#{systemOptionsControl.variables}"/>
</h:selectOneListbox>
</div>
</fieldset>
</div>
</div>
</section>
</h:form>
</body>
</html>
這是Bean:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.xxx.rodrigoms.control;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
/**
*
* @author rodrigos
*/
@Named
@SessionScoped
public class SystemOptionsControl{
private String selectedVariable;
@PostConstruct
public void initialize() {
//non-matter code
}
//<editor-fold desc="GETS/SETS">
public String getIdBtnAtualizarTextAreas(){
return "btnUpdateTextAreas";
}
public String getSelectedVariable() {
return selectedVariable;
}
public void setSelectedVariable(String selectedVariable) {
this.selectedVariable = selectedVariable;
}
public List<String> getVariables() {
List<String> ret = new ArrayList<>();
//just simulating
ret.add("1");
ret.add("2");
ret.add("3");
ret.add("4");
return ret;
}
//</editor-fold>
//<editor-fold desc="Other methods">
public void updateText(){
//I get the NullPointerException right on next line
String variavelAInserir = "<".concat(this.selectedVariable).concat(">");,
/*
code do other suffs
*/
}
//</editor-fold>
}
抱歉任何語言錯誤,需要更改一些名稱,因為它是公司的“工作”。
您沒有提供有關后備bean或xhtml頁面的足夠信息。 甚至您提供的部分包含太多無效的語法,因此我決定舉一個例子。
這是給你的。
SystemOptionsControl.java
import java.util.Arrays;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
@ManagedBean
@ViewScoped
public class SystemOptionsControl {
private Integer selectedVariable;
private List<SelectItem> variables = Arrays.asList(new SelectItem(1),
new SelectItem(2),
new SelectItem(3));
public void updateText(){
System.out.print("updateText method invoked");
System.out.print("Selected Value " + selectedVariable);
}
public Integer getSelectedVariable() {
return selectedVariable;
}
public void setSelectedVariable(Integer selectedVariable) {
this.selectedVariable = selectedVariable;
}
public List<SelectItem> getVariables() {
return variables;
}
public void setVariables(List<SelectItem> variables) {
this.variables = variables;
}
}
Page.xhtml
<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>Title</title>
</h:head>
<h:body>
<h:form prependId="false">
<h:commandButton type="button" id="btnSelectVar" value="Confirm">
<f:ajax execute="lstVarSelected" event="click" listener="#{systemOptionsControl.updateText}" />
</h:commandButton>
<h:selectOneListbox id="lstVarSelected" value="#{systemOptionsControl.selectedVariable}" >
<f:selectItems value="#{systemOptionsControl.variables}"/>
</h:selectOneListbox>
</h:form>
</h:body>
</html>
好吧,我解決了使用綁定屬性。
在我的bean中綁定了inputTextArea之后。 我能夠在偵聽器方法中獲取selectedValue。
我尚未發現的偵聽器執行之前,執行不是“真正”執行set方法的原因。
無論如何,謝謝你們兩個人的大力幫助:)這是我第一次問stackoverflow,你們兩個都很pre弱:)
謝謝!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.