我正在使用Primefaces并尝试使用bean验证,但是当验证失败而不是获取适当的消息时,我只会得到一个空白页面。
继承JSF代码
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<ui:composition template="/WEB-INF/xhtml/templates/main.xhtml">
<ui:param name="header" value="small-header" />
<ui:define name="content">
<div style="margin-top: 60px;"></div>
<h1>Member Sign In</h1>
<h:form>
<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />
<h:panelGrid columns="3">
<h:outputLabel value="Username : "/>
<p:inputText id="username" value="#{signInView.username}"/>
<p:message for="username" style="color:red" />
<h:outputLabel value="Password :"/>
<p:password id="password" value="#{signInView.password}"/>
<p:message for="password" style="color:red" />
</h:panelGrid>
<p:commandButton style="margin-top: 20px;"
value="Sign In"
action="#{signInView.signIn}"
validateClient="false"
ajax="false"/>
</h:form>
<h:outputText>By signing in you agree to abide by our terms and conditions.</h:outputText>
<div style="margin-top: 50px;"></div>
<h:outputText>Not currently a member ?
<p:link outcome="/content/join/join" value="sign up for free"></p:link>
</h:outputText>
<div style="margin-top: 150px;"></div>
</ui:define>
</ui:composition>
这是主模板文件
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<f:facet name="first">
<meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<h:outputStylesheet library="css" name="style.css" />
<h:outputScript library="js" name="menu.js" />
<link href='http://fonts.googleapis.com/css?family=Roboto:300,300italic' rel='stylesheet' type='text/css'/>
<title>
<ui:insert name="title">TODO: Add title here</ui:insert>
</title>
</f:facet>
</h:head>
<h:body>
<ui:include src="/WEB-INF/xhtml/includes/main/title_banner.xhtml">
<ui:param name="header" value="#{header}" />
</ui:include>
<ui:include src="/WEB-INF/xhtml/includes/menu/main_menu.xhtml"/>
<div class='white-bg'>
<div id="content">
<ui:insert name="content">TODO: Add content here</ui:insert>
</div>
</div>
<ui:include src="/WEB-INF/xhtml/includes/main/footer.xhtml" />
</h:body>
在ManagedBean&#39;用户名&#39;用&#39; @Size(min = 2,max = 5)&#39;进行注释。只是为了测试。如果我在点击登录按钮后输入长度小于2或大于5的用户名,我会得到一个空白页。
为什么会发生这种情况?
感谢。
答案 0 :(得分:0)
由于您只发布了部分xhtml代码,我无法建议您出现问题的原因。
但是,我举一个例子来演示如何使用bean验证
login.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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<f:view contentType="text/html" encoding="UTF-8">
<!-- ################################################################# -->
<!-- ## Header -->
<h:head>
<f:facet name="first">
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
</f:facet>
</h:head>
<h:body>
<h:form>
<p:messages id="messages"
showDetail="true"
autoUpdate="true"
closable="true" />
<h:panelGrid columns="3">
<h:outputLabel value="Username : "/>
<p:inputText id="username" value="#{signInView.username}"/>
<p:message for="username" style="color:red" />
<h:outputLabel value="Password :"/>
<p:password id="password" value="#{signInView.password}"/>
<p:message for="password" style="color:red" />
</h:panelGrid>
<p:commandButton style="margin-top: 20px;"
value="Sign In"
action="#{signInView.signIn}"
validateClient="false"
ajax="false"/>
</h:form>
</h:body>
</f:view>
</html>
名为menu.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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<f:view contentType="text/html" encoding="UTF-8">
<!-- ################################################################# -->
<!-- ## Header -->
<h:head>
<f:facet name="first">
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
</f:facet>
</h:head>
<h:body>
Menu page
</h:body>
</f:view>
</html>
managedbean
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.validation.constraints.Size;
/**
*
* @author Wittakarn
*/
@SessionScoped
@ManagedBean(name = "signInView")
public class SignInView implements Serializable{
@Size(min=2,max=5)
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String signIn(){
System.out.println("username = " + username);
return "/menu.xhtml";
}
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<welcome-file-list>
<welcome-file>login.xhtml</welcome-file>
</welcome-file-list>
<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>*.jsf</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>