我正在写一个让我有登录用户的页面。
在数据库中,我有一个表管理员:
CREATE TABLE IF NOT EXISTS `ePrzychodnia`.`admins` (
`id` INT NOT NULL ,
`firstName` VARCHAR(45) NOT NULL ,
`lastName` VARCHAR(45) NOT NULL ,
`personalId` VARCHAR(11) NOT NULL ,
`password` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `personalId_UNIQUE` (`personalId` ASC) ,
UNIQUE INDEX `password_UNIQUE` (`password` ASC) )
Class Admin,用于映射表格。
@Entity
@Table(name = "admins")
@XmlRootElement
public class Admin implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
@NotNull
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "firstName")
private String firstName;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "lastName")
private String lastName;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 11)
@Column(name = "personalId")
private String personalId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "password")
private String password;
ManagedBean JSF:
@ManagedBean
@RequestScoped
public class LoginMB {
private static final String PERSISTENCE_UNIT_NAME = "ePrzychodnia-ejbPU";
private static EntityManagerFactory factory;
private String login;
private String password;
/**
* Creates a new instance of LoginMB
*/
public LoginMB() {
}
Getters and setters
public String validate() {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
Query q = em.createQuery("SELECT a FROM Admin a WHERE a.personalId = :login AND a.password = :password");
q.setParameter("login", login);
q.setParameter("password", password);
try {
Admin admin = (Admin) q.getSingleResult();
if (login.equals(admin.getPersonalId()) && password.equals(admin.getPersonalId())) {
return "success";
}
} catch (Exception e) {
System.out.println("Błąd: " + e.getMessage());
return null;
}
return "failed";
}
}
登录页面:
<?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:head>
<title>Logowanie</title>
</h:head>
<h:body>
<h1>Logowanie</h1>
<h:form>
<h:panelGrid columns="3">
<h:outputLabel value="Login"></h:outputLabel>
<h:inputText id="login" value="#{loginMB.login}" label="Login">
<f:validateRequired/>
</h:inputText>
<h:message for="login" style="color:red" />
<h:outputLabel value="Password"></h:outputLabel>
<h:inputSecret id="pass" value="#{loginMB.password}" label="Password">
<f:validateRequired/>
</h:inputSecret>
<h:message for="pass" style="color:red" />
<h:commandButton value="Logowanie" action="#{loginMB.validate()}"></h:commandButton>
</h:panelGrid>
</h:form>
</h:body>
</html>
数据库中的表我添加了以下条目:
INSERT INTO admins VALUES(1, 'admin', 'admin', '12345678901', 'admin');
我登录网站登录:12345678901传递:admin
但我收到错误pl.ePrzychodnia.model.Admin cannot be cast to pl.ePrzychodnia.model.Admin
为什么我会收到此错误,我做错了什么?
堆栈追踪:
SEVERE: at pl.ePrzychodnia.mb.LoginMB.validate(LoginMB.java:57)
SEVERE: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
SEVERE: at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
SEVERE: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
SEVERE: at java.lang.reflect.Method.invoke(Method.java:601)
SEVERE: at javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:779)
SEVERE: at javax.el.BeanELResolver.invoke(BeanELResolver.java:528)
SEVERE: at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:257)
SEVERE: at com.sun.el.parser.AstValue.invoke(AstValue.java:248)
SEVERE: at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302)
SEVERE: at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
SEVERE: at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
SEVERE: at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
SEVERE: at javax.faces.component.UICommand.broadcast(UICommand.java:315)
SEVERE: at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
SEVERE: at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
SEVERE: at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
SEVERE: at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
SEVERE: at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
SEVERE: at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
SEVERE: at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
SEVERE: at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
SEVERE: at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
SEVERE: at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
SEVERE: at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
SEVERE: at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
SEVERE: at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
SEVERE: at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
SEVERE: at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
SEVERE: at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
SEVERE: at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
SEVERE: at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
SEVERE: at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
SEVERE: at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
SEVERE: at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
SEVERE: at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
SEVERE: at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
SEVERE: at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
SEVERE: at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
SEVERE: at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
SEVERE: at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
SEVERE: at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
SEVERE: at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
SEVERE: at java.lang.Thread.run(Thread.java:722)