如何从ValueChangeEvent重新加载JSF页面? [英] How to reload a JSF page from a ValueChangeEvent?

查看:153
本文介绍了如何从ValueChangeEvent重新加载JSF页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个selectone菜单,选择中的更改应将用户导航到相关页面.

I have a selectonemenu where a change in the selection should navigate the user to the related page.

那么,如何使用selectonemenu控件模拟命令按钮的动作处理(或者是否有更优雅的方法来实现这一点)?

So, how do I simulate the action handling of a commandbutton using a selectonemenu control (or are there a more elegant ways to achieve this)?

推荐答案

您不能为此使用Java脚本.基本上,您需要让Javascript将请求提交到服务器端.在HTML <select>元素(由JSF h:selectOneMenu生成)中,您最好为此使用onchange属性.每当用户更改元素的值时,都将调用您附加到此事件的任何JS.

You can't go around a shot of Javascript for this. Basically you need to let the Javascript submit the request to the server side. In a HTML <select> element (which is been generated by the JSF h:selectOneMenu) you can best use the onchange attribute for this. Any JS which you attach to this event will be invoked whenever the user changes the value of the element.

<h:selectOneMenu onchange="this.form.submit();">

或者如果您懒于写作,那么此速记也是正确的:

or if you're lazy in writing, this shorthand is also correct:

<h:selectOneMenu onchange="submit()">

这将与同一表单(由JSF h:commandButton生成)中的firstnext HTML input type="submit"元素一起提交表单.

This will submit the form in combination with the firstnext HTML input type="submit" element inside the same form (which is been generated by the JSF h:commandButton).

<h:form>
    <h:selectOneMenu value="#{bean.page}" onchange="submit()" required="true">
        <f:selectItem itemLabel="Select page.." />
        <f:selectItems value="#{bean.pages}" />
    </h:selectOneMenu>
    <h:commandButton value="submit" action="#{bean.submit}" />
    <h:commandButton value="other" action="#{bean.other}" /> <!-- won't be submitted -->
</h:form>

您需要在action方法中编写逻辑,该逻辑将导致执行faces-config.xml中定义的导航操作.示例:

You need to write logic in the action method which causes the navigation action as definied in faces-config.xml to be taken place. Example:

public String submit() {
    return this.page;
}

如果您不想使用commandButton,那么您也可以继续滥用valueChangeListener:

If you do not want to use an commandButton, then you can also go ahead with abusing the valueChangeListener:

<h:form>
    <h:selectOneMenu value="#{bean.page}" onchange="submit()"
        valueChangeListener="#{bean.change}" required="true">
        <f:selectItem itemLabel="Select page.." />
        <f:selectItems value="#{bean.pages}" />
    </h:selectOneMenu>
</h:form>

在bean中,您将拥有:

In the bean you then have:

public void change(ValueChangeEvent event) {
    String page = (String) event.getNewValue(); // Must however be the exact page URL. E.g. "contact.jsf".
    FacesContext.getCurrentInstance().getExternalContext().redirect(page);
}

或者,如果您已经具有所需的URL作为f:selectItem值,那么您也可以仅使用JS而不进行bean操作:

Alternatively, if you already have the desired URL's as f:selectItem values, then you can also go ahead with just only JS and no bean actions:

<h:selectOneMenu value="#{bean.page}"
    onchange="window.location = this.options[this.selectedIndex].value">
    <f:selectItem itemLabel="Select page.." />
    <f:selectItem itemLabel="home" itemValue="home.jsf" />
    <f:selectItem itemLabel="faq" itemValue="faq.jsf" />
    <f:selectItem itemLabel="contact" itemValue="contact.jsf" />
</h:selectOneMenu>

这篇关于如何从ValueChangeEvent重新加载JSF页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆