如何解决JSF 2.0登录+会话范围处理问题
| 我在处理JSF 2.0中的登录时遇到问题。我真的不知道我是否朝着正确的方向前进。我想具体一点,所以我会指出我的应用程序如何工作 会话范围内的登录页面 豆角,扁豆。 正确登录后,用户 重定向到下一页 另一个会话范围的bean 的 第二个bean获得登录名并 来自firs 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>
板豆(第二豆)
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>
解决方法
这不会创建两个会话,而是创建两个驻留在同一会话中的会话范围的Bean。
但是,如果使管理登录视图范围的Bean可能更好,因为实际上没有理由将其限制为会话范围(始终建议保持较小的会话状态)。
loginBean
然后将简单地设置the5ѭ的状态。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。