[英]How to reload a JSF page from a ValueChangeEvent?
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)? 那么,如何使用selectonemenu控件模拟命令按钮的动作处理(或者有更优雅的方法来实现这一点)?
You can't go around a shot of Javascript for this. 你不能为此尝试Javascript。 Basically you need to let the Javascript submit the request to the server side.
基本上你需要让Javascript将请求提交给服务器端。 In a HTML
<select>
element (which is been generated by the JSF h:selectOneMenu
) you can best use the onchange
attribute for this. 在HTML
<select>
元素(由JSF h:selectOneMenu
生成)中,您最好使用onchange
属性。 Any JS which you attach to this event will be invoked whenever the user changes the value of the element. 只要用户更改元素的值,就会调用附加到此事件的任何JS。
<h:selectOneMenu onchange="this.form.submit();">
or if you're lazy in writing, this shorthand is also correct: 或者如果你在写作时很懒,这个简写也是正确的:
<h:selectOneMenu onchange="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
). 这将在同一表单(由JSF
h:commandButton
生成)中提交表单和第一个下一个HTML input type="submit"
元素。
<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>
You need to write logic in the action method which causes the navigation action as definied in faces-config.xml
to be taken place. 您需要在action方法中编写逻辑,这会导致在
faces-config.xml
定义导航操作。 Example: 例:
public String submit() {
return this.page;
}
If you do not want to use an commandButton
, then you can also go ahead with abusing the valueChangeListener
: 如果你不希望使用
commandButton
,那么你也可以用滥用继续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>
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);
}
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: 或者,如果您已经将所需的URL作为
f:selectItem
值,那么您也可以只使用JS而不执行bean操作:
<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>
<h:selectOneMenu onchange="document.getElementById('myform').submit();" ...>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.