简体   繁体   English

如何从视图中为jsf支持bean设置隐藏值

[英]How to set a hidden value to jsf backing bean from view

I am having a hard-coded value that needs to be set to the jsf backing bean on form submit. 我有一个硬编码的值,需要在表单提交时将其设置为jsf支持bean。

Can any one tell this please. 谁能告诉这个。

 <h:inputHidden value="#{leaveBean.fApproverEmail}"></h:inputHidden>

but i want to send a hard coded value inplace of "#{leaveBean.fApproverEmail}" and set it to a property of backing bean.. 但我想发送一个硬编码值"#{leaveBean.fApproverEmail}" ,并将其设置为支持bean的属性。

Option 1. 选项1。

Have your property initialized to your hardcoded value. 将您的属性初始化为您的硬编码值。 JSF will auto-update this property on form submit. JSF将在表单提交时自动更新此属性。 So, in case it has changed you will end up with a renewed property value in your action method. 因此,万一发生更改,您最终将在action方法中获得一个更新的属性值。

String fApproverEmail = "default";

<h:inputHidden id="app" value="#{leaveBean.fApproverEmail}" />

Option 2. 选项2。

Have a plain HTML <input type="hidden"> or valueless <h:inputHidden> . 具有纯HTML <input type="hidden">或无值<h:inputHidden> This way the submitted value is available in request parameter map. 这样,提交的值在请求参数映射中可用。 So you'll be able to grab it from ExternalContext#getRequestParameterMap() with its name as the key. 因此,您可以使用名称作为关键字的ExternalContext#getRequestParameterMap()来获取它。 But beware that in case your object's not a string you'll have to do conversion/validation on your own and action method is a wrong place to put that logic. 但是要注意,如果您的对象不是字符串,则必须自己进行转换/验证,而操作方法是放置该逻辑的错误位置。

String fApproverEmail;
public void action() {
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    String s1 = ec.getRequestParameterMap().get("plain");
    String s2 = ec.getRequestParameterMap().get("form:jsf");
    fApproverEmail = ...;//and-or other logic
}

<h:form id="form">
    <h:inputHidden id="jsf" />
    <input type="hidden" id="plain" name="plain" value="#{backingBean.fApproverEmail}"/>
    ...
</h:form>

您可以直接从h:inputHidden标记直接调用bean方法,这样您就可以使用另一个bean方法来获取所需的值。

<h:inputHidden value="#{leaveBean.fApproverEmail(otherBean.methodOrProperty)}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM