我有一个表单,其中有一个文本字段。用户在文本字段中输入电子邮件地址,当用户提交表单时,Action
该类从表中找到uid
用户的并在 JSP 页面上显示为带有 的消息。我得到一个.Users
uid
<s:actionmessage/>
IndexOutOfBoundsException
我的 JSP 表单是:
<s:form action="okadddqs" method="post" cssClass="text">
<s:textfield label="Your Email address " name="email"/>
<s:submit value="Find Uid"/>
</s:form>
该类的代码Action
是:
package com.rambo.action;
import beans.Users;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.hibernate.Session;
/**
*
* @author ROMO
*/
@ManagedBean
@SessionScoped
public class NewQuestion extends ActionSupport {
private String email;
private List<Users> ul = new ArrayList<Users>();
public List<Users> getUl() {
return ul;
}
public void setUl(List<Users> ul) {
this.ul = ul;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
/* i have used many where the getUid() and getEmail() even in the error and messages to find what the value it is getting on error. */
@Override
public String execute() throws Exception {
Session session = null;
int d;
try {
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
try {
ul = (List<Users>) session.createQuery("from Users c where c.email = '" + getEmail() + "'");
if (!ul.isEmpty()) {
d = ul.get(0).getUid();
} else {
this.addActionError("Oops. Sorry. You are Not Allowed now. Please Try Again Later." + getEmail() + ","+ ul.get(0).getUid());
session.close();
return ERROR;
}
} catch (Exception e) {
this.addActionError("Oops. There was an error." + getEmail() + ","+ ul.get(0).getUid());
session.close();
return ERROR;
}
session.getTransaction().commit();
} catch (Exception e) {
this.addActionError("Oops. An Error Encountered...! Email address " + getEmail() + ","+ ul.get(0).getUid()+" not registered. Try with your new email address.");
session.close();
return ERROR;
}
this.addActionMessage("Your Uid is : " + ul.get(0).getUid());
return SUCCESS;
}
@Override
public void validate() {
if ( "".equals(getEmail()) || getEmail() == null ) {
this.addActionError("Email is Compulsory to input..!" + getCname());
} else if (getEmail().indexOf("@") < 0 || getEmail().indexOf(",") > 0 || getEmail().indexOf(".") < 0 || lastEmailFieldTwoCharsOrMore(getEmail()) == false) {
this.addActionError("Please Input a valid email address." + getCname());
}
}
}
当我删除所有 try catch 块时,我得到异常:
java.lang.ClassCastException: org.hibernate.impl.QueryImpl cannot be cast to java.util.List
我已经用 POJO 正确地完成了 bean 和表的映射。有人可以指出错误吗?我正在使用带有 Struts 的 Hibernate。