我的目标是使用ajax根据单选按钮的值激活/停用菜单。 我的问题是带有id =" selectCategoryType"的单选按钮。不更新Bean中的值(catWordGuiBean.addCategoryType) - 因为我没有复杂的对象而是字符串我不需要自定义转换器。
这是我的代码:
<!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:c="http://java.sun.com/jsp/jstl/core"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:p="http://primefaces.org/ui"
>
<h:head>
<link rel="shortcut icon"
href="http://localhost:8080/DeviceCommander/resources/images/favicon.ico" />
<title>
<h:outputText value=" Pronounciator "/>
</title>
</h:head>
<h:body>
<p:layout fullPage="true">
<p:layoutUnit id="up" position="north" header="" resizable="true" closable="false" collapsible="true">
<h:form id="header">
<p:panelGrid columns="6" layout="grid" cellspacing="20" styleClass="ui-noborder">
<h:graphicImage value="resources/images/pronuncia.jpg" height="70" width="120" />
<h:outputText value=" #{display.date}: " />
<h:outputText id="now" value="#{timeBean.now}" />
<p:poll interval="1" listener="#{timeBean.updateDate}" update="now" />
<h:outputText value=" #{display.theme}: " />
<p:themeSwitcher effectSpeed="normal" effect="fade" style="width:165px" id="defaultSwitcher" value="#{themeSwitcherBean.theme}">
<f:selectItem itemLabel=" #{display.select} #{display.theme} " itemValue="" />
<f:selectItems value=" #{themeSwitcherBean.themes} " />
<p:ajax global="false" listener="#{themeSwitcherBean.saveTheme}" />
</p:themeSwitcher>
</p:panelGrid>
</h:form>
</p:layoutUnit>
<p:layoutUnit position="center">
<p:panelGrid styleClass="ui-noborder">
<p:row>
<p:column colspan="1">
<p:panelGrid>
<h:form id="db">
<p:row>
<p:column colspan="4">
<h3>
<h:outputText value=" ADD: " />
</h3>
</p:column>
</p:row>
<p:row>
<p:column>
<h:outputText for="selectCategoryType" value=" Category: " />
</p:column>
<p:column colspan="3">
<p:selectOneRadio id="selectCategoryType" value="#{catWordGuiBean.addCategoryType}" >
<p:ajax event="click" update="addOldCategory" listener="#{catWordGuiBean.handleCategorySelection}" />
<f:selectItems value="#{catWordGuiBean.addCategoryTypes}" var="addCategoryType" itemLabel="#{addCategoryType}" itemValue="#{addCategoryType}"/>
</p:selectOneRadio>
</p:column>
</p:row>
<p:row>
<p:column>
<h:outputText value=" Old Category: " />
</p:column>
<p:column colspan="3">
<p:selectOneMenu id="addOldCategory" value="" disabled="#{catWordGuiBean.disabledOldCategoryGui}">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItem itemLabel="AH" itemValue="AH" />
<f:selectItem itemLabel="UH" itemValue="UH" />
</p:selectOneMenu>
</p:column>
</p:row>
<p:row>
<p:column>
<h:outputText value=" New Category: " />
</p:column>
<p:column>
<p:inputTextarea id="addNewCategoryName" rows="1" cols="4" maxlength="4" autoResize="false" value="#{catWordGuiBean.newCategoryName}" disabled="#{catWordGuiBean.disabledNewCategoryGui}"/>
</p:column>
<p:column>
<h:outputText value=" IPA: "/>
</p:column>
<p:column>
<p:inputTextarea id="addNewIpa" rows="1" cols="3" maxlength="3" autoResize="false" value="#{catWordGuiBean.newIpa}" disabled="#{catWordGuiBean.disabledNewCategoryGui}"/>
</p:column>
</p:row>
<p:row>
<p:column>
<h:outputText value=" Word: "/>
</p:column>
<p:column colspan="3">
<p:inputText id="addNewWord"/>
</p:column>
</p:row>
<p:row>
<p:column>
<h:outputText value=" Submit: "/>
</p:column>
<p:column colspan="3">
<p:commandButton id="addCategoryAndWord" value="Add" />
</p:column>
</p:row>
<p:row>
<p:column colspan="4">
<h3>
<h:outputText value=" DELETE: "/>
</h3>
</p:column>
</p:row>
<p:row>
<p:column>
<h:outputText value=" Category: "/>
</p:column>
<p:column colspan="2">
<p:selectOneMenu id="deleteOldCategory" value="" >
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItem itemLabel="AH" itemValue="AH" />
<f:selectItem itemLabel="UH" itemValue="UH" />
</p:selectOneMenu>
</p:column>
<p:column>
<p:commandButton id="deleteCategory" value="Delete" />
</p:column>
</p:row>
<p:row>
<p:column>
<h:outputText value=" Word: "/>
</p:column>
<p:column colspan="2">
<p:selectOneMenu id="deleteOldWord" value="" >
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItem itemLabel="AH" itemValue="AH" />
<f:selectItem itemLabel="UH" itemValue="UH" />
</p:selectOneMenu>
</p:column>
<p:column>
<p:commandButton id="deleteWord" value="Delete" />
</p:column>
</p:row>
</h:form>
</p:panelGrid>
</p:column>
<p:column colspan="4">
<p:panelGrid>
<h:form id="randomizer">
<p:row>
<p:column>
<h3>
<h:outputText value=" Select Categories: "/>
</h3>
</p:column>
<p:column colspan="2">
<p:selectOneMenu id="selectWordCategories" value="" >
<f:selectItem itemLabel="Select" itemValue="" />
<f:selectItem itemLabel="AH" itemValue="Xbox One" />
<f:selectItem itemLabel="UH" itemValue="PS4" />
</p:selectOneMenu>
</p:column>
</p:row>
<p:row>
<p:column>
<p:commandButton id="wordGenerator" value="Generate Word" />
</p:column>
<p:column>
<p:commandButton id="categoryFinder" value="Show Word Category" />
</p:column>
<p:column>
<h3>
<h:outputText value=" Show Category in: "/>
</h3>
<p:knob value="{wordRandomizerBean.seconds}">
<p:ajax listener="#{wordRandomizerBean.knobChange}"/>
</p:knob>
</p:column>
</p:row>
<p:row>
<p:column>
<h1>
<h:outputText value=" Word: "/>
</h1>
</p:column>
<p:column colspan="2">
<h1>
<h:outputText value=" #{wordRandomizerBean.randomWord} "/>
</h1>
</p:column>
</p:row>
<p:row>
<p:column>
<h1>
<h:outputText value=" Category: "/>
</h1>
</p:column>
<p:column>
<h1>
<h:outputText value=" IPA: "/>
</h1>
</p:column>
<p:column>
<h1>
<h:outputText value=" Category: "/>
</h1>
</p:column>
</p:row>
</h:form>
</p:panelGrid>
</p:column>
</p:row>
</p:panelGrid>
</p:layoutUnit>
</p:layout>
</h:body>
和豆:
package com.linknet.beans;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.AjaxBehaviorEvent;
@ManagedBean(name = "catWordGuiBean")
@ViewScoped
public class CategoryWordManagementBean {
public CategoryWordManagementBean() {
this.disabledNewCategoryGui = true;
this.disabledOldCategoryGui = true;
this.addCategoryType = "";
}
@PostConstruct
public void init() {
addCategoryTypes = new ArrayList<String>();
addCategoryTypes.add("New");
addCategoryTypes.add("Old");
}
public void enableNewCategory() {
this.disabledNewCategoryGui = false;
}
public void disableNewCategory() {
this.disabledNewCategoryGui = true;
}
public void enableOldCategory() {
this.disabledOldCategoryGui = false;
}
public void disableOldCategory() {
this.disabledOldCategoryGui = true;
}
public void handleCategorySelection(AjaxBehaviorEvent e) {
System.out.println("Test 1");
System.out.println(addCategoryType);
}
public boolean isDisabledNewCategoryGui() {
return this.disabledNewCategoryGui;
}
public void setDisabledNewCategoryGui(boolean disabledNewCategoryGui) {
this.disabledNewCategoryGui = disabledNewCategoryGui;
}
public boolean isDisabledOldCategoryGui() {
return disabledOldCategoryGui;
}
public void setDisabledOldCategoryGui(boolean disabledOldCategoryGui) {
this.disabledOldCategoryGui = disabledOldCategoryGui;
}
public String getAddCategoryType() {
return addCategoryType;
}
public void setAddCategoryType(String addCategoryType) {
this.addCategoryType = addCategoryType;
}
public String getNewCategoryName() {
return newCategoryName;
}
public void setNewCategoryName(String newCategoryName) {
this.newCategoryName = newCategoryName;
}
public String getNewIpa() {
return newIpa;
}
public void setNewIpa(String newIpa) {
this.newIpa = newIpa;
}
private boolean disabledNewCategoryGui;
private boolean disabledOldCategoryGui;
private List<String> addCategoryTypes;
public List<String> getAddCategoryTypes() {
return addCategoryTypes;
}
public void setAddCategoryTypes(List<String> addCategoryTypes) {
this.addCategoryTypes = addCategoryTypes;
}
private String addCategoryType;
private String newCategoryName;
private String newIpa;
}
我的错误在哪里?
我感谢任何帮助,提前谢谢。
答案 0 :(得分:1)
将您的ajax事件从<button ion-button block outline (click)="showAlert()">Alert</button>
<button ion-button block outline (click)="showAlert2()">Alert2</button>
更改为 showAlert() {
let alert = this.alertCtrl.create({
title: 'New Friend!',
subTitle: 'Your friend, Obi wan Kenobi, just accepted your friend request!',
buttons: ['OK'],
cssClass: 'alertCustomCss'
});
alert.present();
}
showAlert2() {
let alert = this.alertCtrl.create({
title: 'New Friend!',
subTitle: 'Your friend, Obi wan Kenobi, just accepted your friend request!',
buttons: ['OK']
});
alert.present();
}
,以便在更新click
的值后change
执行
listener