我正在使用 JSF 2。这是我的控制器。
package com.acc.validations;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="user")
@SessionScoped
public class Controller implements Serializable{
private static final long serialVersionUID = 1L;
String name;
String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String checkUser(String name,String password) {
if(("admin".equalsIgnoreCase(name)) && ("admin".equalsIgnoreCase(password))){
return "result";
}else{
/*System.out.println("Enter Valid details");*/
FacesMessage msg = new FacesMessage("Authentication Failed.","Authentication Failed");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
return null;
}
}
public void execute(){
checkUser(name, password);
}
}
这是我的 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:f="http://java.sun.com/jsf/core" >
<h:body>
<h1>Custom validator in JSF 2.0</h1>
<h:form >
<h:panelGrid columns="3">
User Name:
<h:inputText id="username" value="#{user.name}"
required="true" >
<f:validateLength minimum="4" maximum="15" />
</h:inputText>
<h:message for="username" style="color:red" />
Password :
<h:inputSecret id="password" value="#{user.password}"
required="true" >
<f:validator validatorId="com.acc.validations.PasswordValidator" />
</h:inputSecret>
</h:panelGrid>
<h:commandButton value="Submit" action="#{user.execute}" />
</h:form>
</h:body>
</html>
我的要求是,如果用户以“admin”的身份输入用户名和密码,则控件应重定向到 result.xhtml,否则应在 facesContext 中显示我在控制器中编写的消息。这是我的结果.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">
<h:body>
<h1>Custom validator in JSF 2.0</h1>
<h:panelGrid columns="2">
Welcome to User :
<h:outputText value="#{user.name}" />
</h:panelGrid>
</h:body>
</html>
即使我输入 admin 作为用户名和密码,我也无法呈现 result.xhtml,而且我在输入错误凭据时也没有收到任何消息。而且我也没有收到任何错误。你能在这方面帮助我吗为了按照我的要求工作。