提交表单而无需验证bean

编程入门 行业动态 更新时间:2024-10-10 12:26:31
本文介绍了提交表单而无需验证bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个表单,该表单具有带有一些JSR-303验证bean的域模型.现在,我想包括未经任何验证的保存草稿"功能.如果我在相应的commandButton上设置immediate=true,则将跳过验证,但也会提交表单.

I've got a form which has a domain model with some JSR-303 validation beans. Now I would like to include a "Save draft" feature without any validation. If I set immediate=true on my corresponding commandButton validation is skipped but also the form submit.

是否可以在保存草稿操作中更新模型?

Is there a way to update the model in my save draft action?

推荐答案

使用 <f:validateBean> ,在其中设置disabled属性.

<h:inputText value="#{bean.input}"> <f:validateBean disabled="#{bean.draft}" /> </h:inputText>

如果此结果评估为true,则将跳过对与输入值关联的属性的所有bean验证.您仅应确保在验证阶段之前设置boolean draft属性.例如

If this evaluates true, this will skip all bean validation on the property associated with the input's value. You should only ensure that the boolean draft property is set before the validations phase takes place. E.g.

<h:commandButton value="Save draft" action="#{bean.saveDraft}"> <f:param name="draft" value="true" /> </h:commandButton>

使用

@ManagedProperty("#{param.draft}") private boolean draft;

或者如果它是视图范围的Bean,而@ManagedProperty在该范围内将不起作用:

or if it's a view scoped bean on which @ManagedProperty won't work:

public boolean isDraft() { return "true".equals(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("draft")); }

另一种方法是通过确定按钮的参数名称是否存在来检查EL是否按下了按钮.例如,使用以下表单和按钮ID

Another way is to check in EL if the button is pressed by determining the presence of its parameter name. For example, with the following form and button ID

<h:form id="form"> <h:inputText value="#{bean.input}"> <f:validateBean disabled="#{not empty param['form:draft']}" /> </h:inputText> <h:commandButton id="draft" value="Save draft" action="#{bean.saveDraft}" /> </h:form>

更多推荐

提交表单而无需验证bean

本文发布于:2023-11-28 04:57:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641042.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   bean

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!