【发布时间】:2014-11-23 14:33:22
【问题描述】:
在 JSF2 和 ajax 中工作,我无法弄清楚 JSF 上下文中 $.ajax URL 属性的值是什么,因为我想提交表单并更新停留在同一页面中的 div
是不是下面这个?
((HttpServletRequest) facesContext.getExternalContext().getRequest()).getContextPath()`
【问题讨论】:
在 JSF2 和 ajax 中工作,我无法弄清楚 JSF 上下文中 $.ajax URL 属性的值是什么,因为我想提交表单并更新停留在同一页面中的 div
是不是下面这个?
((HttpServletRequest) facesContext.getExternalContext().getRequest()).getContextPath()`
【问题讨论】:
在我看来,您需要将值传递给支持 bean 并设置参数,之后您需要更新这部分(div)。
看到这个http://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/
<p:commandButton action="#{bean.setId}" update="div">
<f:attribute name="id" value="12" />
</p:commandButton>
另一种方法是使用闪存,请参见:https://javaserverfaces.java.net/nonav/docs/2.0/javadocs/javax/faces/context/Flash.html
【讨论】:
假设您有这样的表格:
<div id="yourDivId">
<h:outputText value='#{yourControllerClass.yourAttribute}'></h:outputText>
</div>
<h:form>
<h:commandButton value="update">
<f:ajax execute="@form" listener="#{yourControllerClass.updateAttribute}"
render=":yourDiveId">
</f:ajax>
</h:commandButton>
</h:form>
在您的 ControllerClass/bean 中:
public final void updateAttribute(AjaxBehaviorEvent event){
// do what ever you want, get values, set another values, after that your Div will be auto updated
}
这样,您就不需要关心 ajax 参数或 URL...等。
编辑:
如果你想从请求 URL 中获取参数,那么你可以这样获取:
String paramVal = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(key);
【讨论】: