【问题标题】:What URL should I use in $.ajax() in order to submit a form我应该在 $.ajax() 中使用什么 URL 来提交表单
【发布时间】:2014-11-23 14:33:22
【问题描述】:

在 JSF2 和 ajax 中工作,我无法弄清楚 JSF 上下文中 $.ajax URL 属性的值是什么,因为我想提交表单并更新停留在同一页面中的 div

是不是下面这个?

((HttpServletRequest) facesContext.getExternalContext().getRequest()).getContextPath()`

【问题讨论】:

    标签: ajax forms url jsf-2


    【解决方案1】:

    在我看来,您需要将值传递给支持 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

    【讨论】:

      【解决方案2】:

      假设您有这样的表格:

      <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);
      

      【讨论】:

      • 非常适合使用 f:ajx 标签。尝试实现我自己的 ajax 处理程序类来执行与 f:ajax 相同的任务怎么样?这就是我正在寻找了解 JSF 如何处理 URL ajax 属性的原因
      • @farouk,这是一种复杂的方法,但如果你想从请求 URL 中获取参数,请查看我的更新!
      • 感谢拉米。这很有帮助。换句话说,我如何在不使用 f:ajax 的情况下在 JSF2 中提交表单;获取表单参数并将它们发送到 FacesServlet,或者我们需要其他东西?
      • 在使用 JSP 和 Servlet 时,我们所需要的只是 servlet 的 ID 和表单中的 action 属性。更何况 ib 应该把 ajax 语句的 URL 放在什么地方?
      猜你喜欢
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      相关资源
      最近更新 更多