【发布时间】:2021-09-03 12:16:16
【问题描述】:
我正在尝试使用 hibernate、jsf、mysql 和 netbeans 构建登录页面。我创建了包含表单和信息的索引页面。创建一个 Login JSF Managed bean 页面并设置必要的东西,如 try..catch 等。这是我的 Login.java
package common;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
@ManagedBean(name = "login_bean")
@SessionScoped
@Entity
public class Login implements Serializable {
@Id
private int sl_no;
private String user_name;
private String password;
public String getUser_name() {
return user_name;
}
public String getPassword() {
return password;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public void setPassword(String password) {
this.password = password;
}
public boolean checkuser(){
try {
System.out.println("user name" +user_name);
System.out.println("password" +password);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Query query= session.createQuery("from Login where user_name=:user_name and password=:password");
query.setString("user_name", user_name);
query.setString("password", password);
List list= query.list();
System.out.println("list size" +list.size());
if(list.size()==1){
return true;
}else{
return false;
}
} catch (Exception e) {
System.out.println(e);
}
return false;
}
public Login() {
}
}
还将我的数据库与 netbeans 连接起来,以便可以从我的数据库中获取用户。它工作正常。我想添加一个在 id/pw 错误时给出错误消息的功能。我想在我的 faces-config.xml 中执行此操作,因为我正在从该页面导航登录。尝试这样做,但无法将任何代码放入我的 xml 页面。有一些例子和类似的问题,但它们都是用 php 或 java 编写的。这是我的 faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<navigation-rule>
<from-view-id>index.xhtml</from-view-id>
<navigation-case>
<from-action>#{login_bean.checkuser()}</from-action>
<from-outcome>true</from-outcome>
<to-view-id>loginSucc.xhtml</to-view-id>
<redirect></redirect>
</navigation-case>
<navigation-case>
<from-action>#{login_bean.checkuser()}</from-action>
<from-outcome>false</from-outcome>
<to-view-id>index.xhtml</to-view-id>
<redirect></redirect>
</navigation-case>
</navigation-rule>
</faces-config>
重定向后,我想显示一条消息,就像“失败”一样,并让 index.xhtml 继续工作。我愿意接受任何建议。我的 xml 知识还不够,在 xml 上找不到任何示例。
【问题讨论】:
标签: jsf