在F:在H阿贾克斯监听方法:不执行selectOneMenu用于

编程入门 行业动态 更新时间:2024-10-11 09:30:19
本文介绍了在F:在H阿贾克斯监听方法:不执行selectOneMenu用于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

该页面与托管bean合适的值,但是这两个H在AJAX事件正确生成:selectOneMenus不工作。监听器不叫。错误必须在标签的地方,但我没有看到它。

The page is generated correctly with appropriate values in managed bean, but ajax events in these two h:selectOneMenus don't works. Listener is not called. An error has to be somewhere within tags, but I don't see it.

<f:view> <h:form> <h:messages /> <h:panelGrid columns="3"> <h:outputLabel value="Choose your faculty: *" for="faculties" /> <h:selectOneMenu id="faculties" value="#{registrateStudent.selectedFaculty}" > <f:ajax event="change" listener="#{registrateStudent.genSpecializations}" execute="faculties" render="specializations" /> <f:selectItems value="#{registrateStudent.listFaculty}" var="curFac" itemLabel="#{curFac.name}" itemValue="#{curFac}" /> </h:selectOneMenu> <h:message id="message_faculties" for="faculties" /> <h:outputLabel value="Choose your specialization: *" for="specializations" /> <h:selectOneMenu id="specializations" value="#{registrateStudent.selectedSpecialization}" > <f:selectItems value="#{registrateStudent.listSpecialization}" var="curSpec" itemLabel="#{curSpec.name}" itemValue="#{curSpec}"/> </h:selectOneMenu> <h:message id="message_specializations" for="specializations" />

托管Bean:

Managed Bean:

@ManagedBean(name = "registrateStudent") @ViewScoped public class RegistrateStudent { private Faculty selectedFaculty; private List<Faculty> listFaculty; private Specialization selectedSpecialization; private List<Specialization> listSpecialization; private boolean showSpecialization = false; /** Creates a new instance of RegistrateStudent */ public RegistrateStudent() { users = new Users(); System.out.println("poaposd1"); student = new Student(); } @PostConstruct public void init() { listFaculty = ff.findAll(); if (listFaculty != null) { selectedFaculty = listFaculty.get(0); listSpecialization = sf.findByFaculty(selectedFaculty.getIdFaculty()); if (listSpecialization != null) { selectedSpecialization = listSpecialization.get(0); } else {} } else {} } public void genSpecializations(AjaxBehaviorEvent event) { if (sf.findByFaculty(selectedFaculty.getIdFaculty()) != null) { this.showSpecialization = true; } else { JsfUtil.addSuccessMessage("faculties", "We don't have specializations for such faculty"); } } }

更新:

我发现了一些有趣的东西:

I've found out a few interesting things:

&LT; F:AJAX&GT; H; 标记不为&LT工作:链接&GT; ,&LT; H:selectOneMenu用于&GT; ,&LT; H:按钮&GT; ,&LT; H:的commandButton&GT; 。在这种情况下不正确的值呈现属性没有注意到,但事件的不正确的值属性产生错误。

<f:ajax> tag doesn't work at <h:link>, <h:selectOneMenu>, <h:button>, <h:commandButton>. In this cases incorrect values in render attribute is not noticed, but incorrect value of event attribute generate an error.

&LT; H:outputLabel&GT; ,&LT; H:inputText的&GT; 与工作&LT; F:AJAX&GT; 正常。 WTF?

<h:outputLabel>, <h:inputText> work with <f:ajax> properly. WTF?

推荐答案

在&LT; F:AJAX&GT; 要求 jsf.js 文件被包含在HTML &LT; HEAD&GT; 。它包含做JSF AJAX魔术所有的JS功能。

The <f:ajax> requires jsf.js file being included in the HTML <head>. It contains all JS functions for doing the JSF ajax magic.

要实现这一目标,确保你使用&LT; H:头&GT; 而不是&LT; HEAD&GT; 主模板研究。那么JSF将自动包含必要的&LT;脚本&GT; 元素有指向 jsf.js

To achieve this, ensure that you're using <h:head> instead of <head> in the master template. JSF will then automatically include the necessary <script> element there pointing to jsf.js.

<!DOCTYPE html> <html lang="en" xmlns="www.w3/1999/xhtml" xmlns:f="java.sun/jsf/core" xmlns:h="java.sun/jsf/html" xmlns:ui="java.sun/jsf/facelets"> <h:head> <title>Look, with h:head</title> </h:head> <h:body> Put your content here. </h:body> </html>

请注意,在一个有点像样的网页浏览器有一点像样的webdeveloper工具如Firefox的的Web开发工具栏和/或萤火你应该马上注意到JS的错误,如 JSF是未定义当Ajax请求被执行。这至少应该给一些思考。

Note that in a bit decent webbrowser with a bit decent webdeveloper toolset like Firefox's Web Developer Toolbar and/or Firebug you should immediately have noticed JS errors like jsf is undefined when the ajax request is to be executed. That should at least have given something to think about.

更新:根据您的更新

我发现了一些有趣的东西:

I've found out a few interesting things:

&LT; F:AJAX&GT; H; 标记不为&LT工作:链接&GT; ,&LT; H:selectOneMenu用于&GT; ,&LT; H:按钮&GT; ,&LT; H:的commandButton&GT; 。在这种情况下不正确的值呈现属性没有注意到,但事件的不正确的值属性产生错误。

<f:ajax> tag doesn't work at <h:link>, <h:selectOneMenu>, <h:button>, <h:commandButton>. In this cases incorrect values in render attribute is not noticed, but incorrect value of event attribute generate an error.

&LT; H:outputLabel&GT; ,&LT; H:inputText的&GT; 与工作&LT; F:AJAX&GT; 正常。 WTF?

<h:outputLabel>, <h:inputText> work with <f:ajax> properly. WTF?

在&LT; H:链接&GT; 和&LT; H:按钮&GT; 的intented仅供GET请求,不是POST请求。不过应当只是正常工作的&LT; H:selectOneMenu用于&GT; 和&LT; H:的commandButton&GT; 。难道你有更多的code到完整的画面,你从为简便起见,省略了问题?其中JSF实现了一套/版本您使用的?您是否使用类路径正确的库?它看起来像你必须真的搞砸了一些东西。

The <h:link> and <h:button> are intented for GET requests only, not POST requests. It should however work just fine on <h:selectOneMenu> and <h:commandButton>. Don't you have more code into the complete picture which you omitted from the question for simplicity? Which JSF impl/version are you using? Are you using the right libraries in classpath? It look like that you must really have messed up something.

要说服你(和我自己),我刚刚创建以下copy'n'paste'n'runnable测试用例

To convince you (and myself) I just created the following copy'n'paste'n'runnable testcase

<!DOCTYPE html> <html lang="en" xmlns:h="java.sun/jsf/html" xmlns:f="java.sun/jsf/core" xmlns:ui="java.sun/jsf/facelets" > <h:head> <title>SO question 6089924</title> </h:head> <h:body> <h:form> <h:selectOneMenu value="#{bean.selected}"> <f:selectItem itemValue="#{null}" itemLabel="Select..." /> <f:selectItem itemValue="one" /> <f:selectItem itemValue="two" /> <f:selectItem itemValue="three" /> <f:ajax listener="#{bean.listener}" render="result" /> </h:selectOneMenu> <h:commandButton value="commandButton" action="#{bean.submit}"> <f:ajax listener="#{bean.listener}" render="result" /> </h:commandButton> <h:outputText id="result" value="#{bean.selected} #{bean.result}" /> <h:messages /> </h:form> </h:body> </html>

通过这个bean

package com.example; import java.io.Serializable; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; import javax.faces.event.AjaxBehaviorEvent; @ManagedBean @ViewScoped public class Bean implements Serializable { private String selected; private String result; public void submit() { System.out.println("submit"); } public void listener(AjaxBehaviorEvent event) { System.out.println("listener"); result = "called by " + event.getComponent().getClass().getName(); } public String getSelected() { return selected; } public void setSelected(String selected) { this.selected = selected; } public String getResult() { return result; } }

这与Mojarra 2.1.1在Tomcat 7.0.12运行正常。

It runs fine with Mojarra 2.1.1 on Tomcat 7.0.12.

INFO: Starting Servlet Engine: Apache Tomcat/7.0.12 INFO: Initializing Mojarra 2.1.1 (FCS 20110408) for context '/playground'

更多推荐

在F:在H阿贾克斯监听方法:不执行selectOneMenu用于

本文发布于:2023-11-24 19:38:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626548.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   阿贾克斯   selectOneMenu

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!