gpt4 book ai didi

java - 为什么 renderer add a hidden field with form ID as name and value

转载 作者:搜寻专家 更新时间:2023-11-01 01:43:59 24 4
gpt4 key购买 nike

我正在阅读 JSF 实现 <h:form>渲染。令我惊讶的是,我看到(在 Mojarra、MyFaces + Tomahawk 中),他们在 encodeBegin() 上添加了一个隐藏的输入字段。方法。

这是 FormRenderer 的示例代码在莫哈拉:

@Override
public void encodeBegin(FacesContext context, UIComponent component)
throws IOException {

rendererParamsNotNull(context, component);

if (!shouldEncode(component)) {
return;
}

ResponseWriter writer = context.getResponseWriter();
assert(writer != null);
String clientId = component.getClientId(context);
// since method and action are rendered here they are not added
// to the pass through attributes in Util class.
writer.write('\n');
writer.startElement("form", component);
writer.writeAttribute("id", clientId, "clientId");
writer.writeAttribute("name", clientId, "name");
writer.writeAttribute("method", "post", null);
writer.writeAttribute("action", getActionStr(context), null);
String styleClass =
(String) component.getAttributes().get("styleClass");
if (styleClass != null) {
writer.writeAttribute("class", styleClass, "styleClass");
}
String acceptcharset = (String)
component.getAttributes().get("acceptcharset");
if (acceptcharset != null) {
writer.writeAttribute("accept-charset", acceptcharset,
"acceptcharset");
}

RenderKitUtils.renderPassThruAttributes(context,
writer,
component,
ATTRIBUTES);
writer.writeText("\n", component, null);

// this hidden field will be checked in the decode method to
// determine if this form has been submitted.
writer.startElement("input", component);
writer.writeAttribute("type", "hidden", "type");
writer.writeAttribute("name", clientId,
"clientId");
writer.writeAttribute("value", clientId, "value");
writer.endElement("input");
writer.write('\n');

// Write out special hhidden field for partial submits
String viewId = context.getViewRoot().getViewId();
String actionURL =
context.getApplication().getViewHandler().getActionURL(context, viewId);
ExternalContext externalContext = context.getExternalContext();
String encodedActionURL = externalContext.encodeActionURL(actionURL);
String encodedPartialActionURL = externalContext.encodePartialActionURL(actionURL);
if (encodedPartialActionURL != null) {
if (!encodedPartialActionURL.equals(encodedActionURL)) {
writer.startElement("input", component);
writer.writeAttribute("type", "hidden", "type");
writer.writeAttribute("name", "javax.faces.encodedURL", null);
writer.writeAttribute("value", encodedPartialActionURL, "value");
writer.endElement("input");
writer.write('\n');
}
}

if (!writeStateAtEnd) {
context.getApplication().getViewHandler().writeState(context);
writer.write('\n');
}
}

我的问题:

  1. 为什么有一个隐藏的输入字段被分配了 id component.getClientId(context) ,即 UIForm成分?隐藏字段的用途是什么?
  2. 在 Mojarra 中,您无法选择分配自己的 id <h:form> 上的属性但你可以在 MyFaces 上。 JSF如何对待UIForm在每种情况下(例如,当 MyFaces 明确提供 id 时)?
  3. Mojarra 形式 enctype默认为 application/x-www-form-urlencoded .能支持multipart/form-data吗或 text/plain

谢谢

最佳答案

Why is there a hidden input field that gets assigned the id component.getClientId(context), i.e., a UIForm component? What's the purpose of the hidden field?

仔细观察评论(行号来自 Mojarra 2.1.19 ):

153        // this hidden field will be checked in the decode method to
154 // determine if this form has been submitted.
155 writer.startElement("input", component);
156 writer.writeAttribute("type", "hidden", "type");
157 writer.writeAttribute("name", clientId,
158 "clientId");
159 writer.writeAttribute("value", clientId, "value");
160 writer.endElement("input");
161 writer.write('\n');

这因此被用来确定提交了哪个表单。此确定依次发生在 FormRenderer#decode() 中当 JSF 需要在应用请求值阶段确定 HTTP 请求参数时调用:

96         // Was our form the one that was submitted?  If so, we need to set
97 // the indicator accordingly..
98 Map<String, String> requestParameterMap = context.getExternalContext()
99 .getRequestParameterMap();
100 if (requestParameterMap.containsKey(clientId)) {
101 if (logger.isLoggable(Level.FINE)) {
102 logger.log(Level.FINE,
103 "UIForm with client ID {0}, submitted",
104 clientId);
105 }
106 ((UIForm) component).setSubmitted(true);
107 } else {
108 ((UIForm) component).setSubmitted(false);
109 }

换句话说,它控制着 UIForm#isSubmitted() 的结果。属性(property)。它可能在以下涉及页面中多个表单的用例中很有用:


In Mojarra, you don't have the option to assign your own id attribute on <h:form> but you can on MyFaces. How does JSF treat UIForm in each cases (e.g. when MyFaces has its an explicit provided id)?

我不确定你在说什么。这,

<h:form id="formId">

在 Mojarra 中也适合我。


Mojarra form enctype is defaulted to application/x-www-form-urlencoded. Can it support multipart/form-data or text/plain?

这符合 JSF 规范。是的,你可以只使用 enctype以通常的方式归因。

<h:form enctype="multipart/form-data">

这种表单的提交是否被 JSF 正确识别,是其次。在 JSF 2.2 之前,JSF 没有内置支持 multipart/form-data . JSF 组件库通过提供一个 servlet 过滤器及其文件上传组件来解决这个问题,通常在幕后使用 Apache Commons FileUpload。 JSF 2.2 直接委托(delegate)给新的 Servlet 3.0 HttpServletRequest#getPart() API,无需使用第 3 方 API 的 servlet 过滤器。另见 How to upload files to server using JSP/Servlet?

关于java - 为什么 <h :form> renderer add a hidden field with form ID as name and value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17558380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com