0
我在JSF 2.0中处理登录问题。我真的不知道我是否正朝着正确的方向前进。我想要具体所以我会指出我的应用程序如何工作JSF 2.0登录+会话范围处理问题
- 登录页面的会话范围为 bean。
- 一个正确登录之后,用户 被重定向到下一个页面 另一个会话范围的bean
- 的 第二豆获取登录和从冷杉豆 密码字符串进一步使用同一个数据库
我的问题是我为用户创建两个会话?如果是这样,我怎么能杀死第一届会议。如果它不是管理登录的最佳方式,请纠正我。谢谢你在前进
这里是一个simplyfied例如
LOGIN BEAN
package main;
import java.io.Serializable;
import java.sql.SQLException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@SessionScoped
@ManagedBean(name="loginBean")
public class loginBean implements Serializable
{
private String login;
private String password;
public loginBean()
{
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String login() throws ClassNotFoundException, SQLException
{
SQL sql = new SQL(); //class connecting do database
if(sql.login(this.login, this.password) == true)
{
return "yes";
}
else
{
return "no";
}
}
}
登录页面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:h="http://java.sun.com/jsf/html">
<h:head>
</h:head>
<h:body>
<h:form>
<div id="logowanie" style="margin: auto; max-width: 180px; margin-top: 50px; ">
<table border="0" cellspacing="4" cellpadding="4">
<thead></thead>
<tbody>
<tr>
<td><h:outputText value="login:" /></td>
<td><h:inputText value="#{loginBean.login}" size="10"/></td>
</tr>
<tr>
<td><h:outputText value="password:" /> </td>
<td><h:inputSecret value="#{login.password}" size="10"/></td>
</tr>
<tr>
<td colspan="2"><h:commandButton value="zaloguj" action="#{loginBean.login}" /></td>
</tr>
</tbody>
</table>
</div>
</h:form>
</h:body>
</html>
PANEL BEAN(第二豆)
package main;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@SessionScoped
@ManagedBean(name="panelBean")
public class panelBean implements Serializable{
private String login;
private String password;
FacesContext context = FacesContext.getCurrentInstance();
public panelBean()
{
loginBean Bean = (loginBean) context.getApplication().getVariableResolver().resolveVariable(context, "loginBean");
this.password = Bean.getPassword();
this.login = Bean.getLogin();
Bean.setPassword("");
Bean.setLogin("");
ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
HttpServletResponse response = (HttpServletResponse)ectx.getResponse();
HttpSession session = (HttpSession)ectx.getSession(false);
session.invalidate();
}
}
面部配置
<?xml version='1.0' encoding='UTF-8'?>
<!-- =========== FULL CONFIGURATION FILE ================================== -->
<faces-config version="2.0"
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">
<navigation-rule>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>yes</from-outcome>
<to-view-id>/panel.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>no</from-outcome>
<to-view-id>/login.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
谢谢。因为我使用CGI,所以我选择了请求范围,因为viev scope不受支持 – Szajba