我正在实现一个JSF应用程序,其中用户提供的凭据通过查看MySQL数据库表进行验证,如果找到它们,则客户端和服务器之间的HTTP会话启动并且“欢迎” “页面是根据他/她的请求返回的。 “欢迎”页面包含一个注销按钮,当我点击它时,什么都不做(我检查了HTTP流量以确保它)。
<h:commandButton value="Logout" type="submit" action="#{logoutBean.terminate}"/>
注销按钮与处理注销过程的托管bean相关(即终止会话并返回起始页)。终止会话并返回起始页面的功能如下:
public String terminate() {
System.out.println("Here!"); //for debug
FacesContext fc = FacesContext.getCurrentInstance();
HttpSession session =
(HttpSession) fc.getExternalContext().getSession(false);
try {
session.invalidate();
} catch(IllegalStateException isEx) {
isEx.printStackTrace();
}
return "index";
}
即使消息Here!
也未在控制台中查看。
更具体地说,让我发布下面的完整代码。
我的welcome.xhtml页面:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Admin's page</title>
</head>
<body>
<h2>Welcome admin!</h2>
<p>You are successfully logged in</p> <br/>
<h:commandButton value="Logout" type="submit" action="#{logoutBean.terminate}"/>
</body>
</html>
我的LogoutBean.java文件(处理退出请求):
package loginbean;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
@ManagedBean(name="logoutBean")
@RequestScoped
public class LogoutBean {
private String errorMsg = null;
public LogoutBean() { }
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public String terminate() {
System.out.println("Here!");
FacesContext fc = FacesContext.getCurrentInstance();
HttpSession session =
(HttpSession) fc.getExternalContext().getSession(false);
try {
session.invalidate();
} catch(IllegalStateException isEx) {
isEx.printStackTrace();
}
return "index";
}
}
我的首页(index.xhtml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<h:outputText value="#{loginBean.errorMsg}"
rendered="#{loginBean.errorMsg != null}"
style="color:red;"/>
<h:form>
User Name: <h:inputText id="userName" value="# {loginBean.userName}"/>
<br/>
Password: <h:inputSecret id="password" value="#{loginBean.password}"/>
<br/>
<h:commandButton value="Submit" type="submit" action="#{loginBean.validate}"/>
<h:commandButton value="Reset" type="reset"/>
</h:form>
</body>
</html>
和我的LoginBean.java(处理登录请求)
package loginbean;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import java.sql.*;
@ManagedBean(name="loginBean")
@RequestScoped
public class LoginBean {
private String userName = null;
private String password = null;
private String errorMsg = null;
public LoginBean() {}
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 getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public String validate() {
try {
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/dbname?useSSL=false",
"rootName", "aPassword");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select name, password from admins;");
// Look for the credentials into the table.
while (rs.next()) {
if (rs.getString("name").equals(userName)
&& rs.getString("password").equals(password)) {
errorMsg = "null";
// Start a session with the client.
FacesContext fc = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) fc.getExternalContext().getSession(true);
System.out.println("Session ID = " + session.getId());
// Return the "welcome.xhtml" page.
return "welcome";
}
}
} catch(SQLException sqlEx) {
System.out.println("Error code: " + sqlEx.getErrorCode());
}
errorMsg = "Invalid user. Please try again";
return null;
}
}
我认为没有必要发布我的web.xml
,因为这对于这样的应用来说非常明显。
有人可以帮我正确实施这样的退出流程吗?