我有一个表单,其中包含一些在处理表单数据时验证的字段。这是一种合理性/一致性检查。检查本身很昂贵,因此不应该执行两次。但是在成功提交表单期间必须才能执行,因此它是支持bean中commandButton操作方法的一部分。
我认为如果我使用< f:validator>,它将在验证阶段执行一次,然后在提交时再次执行。正如我所说,这是不可取的。
据我了解文档,只需在表单的某个组件中添加一条消息就足以保留在表单上。
所以我试过
FacesContext.getCurrentInstance().addMessage("comandButtonID", "some message");
但它不会停留在表格上。
我是否需要执行其他操作才能将表单标记为无效?
如果重要,则支持bean是请求范围的。
所以,这是最小的完整例子
操作系统:Linux
浏览器:Chromium
服务器:Tomcat 8.0.36
JDK:Oracle-JDK 1.8.0_92
JSF实现:myfaces 2.1.18 + tomahawk 20-1.1.14
在创建此示例时,我发现了一件事: 当我将javax.faces.PROJECT_STAGE设置为"开发"和facelets.DEVELOPMENT到" true",并显示消息。 使用下面的web.xml设置,它不是。
web.xml(省略说明)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>facelets.REFRESH_PERIOD</param-name>
<param-value>2</param-value>
</context-param>
<context-param>
<param-name>facelets.SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<context-param>
<param-name>
org.apache.myfaces.NUMBER_OF_VIEWS_IN_SESSION
</param-name>
<param-value>20</param-value>
</context-param>
<!-- START MyFaces filter -->
<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
<filter-class>
org.apache.myfaces.webapp.filter.ExtensionsFilter
</filter-class>
<init-param>
<param-name>uploadMaxFileSize</param-name>
<param-value>100m</param-value>
</init-param>
<init-param>
<param-name>uploadThresholdSize</param-name>
<param-value>100k</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<url-pattern>/faces/myFacesExtensionResource/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<url-pattern>*.xhtml</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<url-pattern>/faces/*</url-pattern>
</filter-mapping>
<!-- END MyFaces filter -->
<!-- START MyFaces config -->
<context-param>
<param-name>org.apache.myfaces.SERIALIZE_STATE_IN_SESSION</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.PRETTY_HTML</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>
org.apache.myfaces.READONLY_AS_DISABLED_FOR_SELECTS
</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>
org.apache.myfaces.CHECK_EXTENSIONS_FILTER
</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.ADD_RESOURCE_CLASS</param-name>
<param-value>
org.apache.myfaces.renderkit.html.util.DefaultAddResource
</param-value>
</context-param>
<context-param>
<param-name>
org.apache.myfaces.RESOURCE_VIRTUAL_PATH
</param-name>
<param-value>/faces/myFacesExtensionResource</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<!-- END MyFaces config -->
<welcome-file-list>
<welcome-file>testform.xhtml</welcome-file>
</welcome-file-list>
</web-app>
faces-config.xml中
<?xml version='1.0' encoding='UTF-8'?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<managed-bean>
<managed-bean-name>FormTest</managed-bean-name>
<managed-bean-class>FormTest</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<navigation-case>
<from-action>#{FormTest.formAction}</from-action>
<from-outcome>OK</from-outcome>
<to-view-id>/testform_OK.xhtml</to-view-id>
</navigation-case>
<!-- I've also tried to remove the second navigation-case -->
<navigation-case>
<from-action>#{FormTest.formAction}</from-action>
<from-outcome>FAIL</from-outcome>
<to-view-id>/testform.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
FormTest.java
public class FormTest {
String input;
public String getInput() {
return input;
}
public void setInput(String input) {
this.input = input;
}
public String formAction() {
final boolean valid = "ok".equalsIgnoreCase(input);
if (!valid) {
final FacesMessage message = new FacesMessage("not OK");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext.getCurrentInstance().addMessage("submit", message);
}
return valid ? "OK" : "FAIL";
}
}
testform.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:t="http://myfaces.apache.org/tomahawk">
<h:head>
<title>Test Form</title>
</h:head>
<h:body>
<h:form id="form">
<h:outputText value="Enter "ok" (case insensitive) to get to the next page."/>
<br/>
<h:outputText value="Any other value should give an error message and stay on this page."/>
<br/>
<h:inputText id="input" value="#{FormTest.input}"/>
<br/>
<h:message id="submitMessage" for="submit"/>
<t:commandButton id="submit" value="Submit" action="#{FormTest.formAction}"/>
</h:form>
</h:body>
</html>
testform_OK.xhtml
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Form was submitted</title>
</h:head>
<h:body>
Form was submitted with value "OK".
</h:body>
</html>