我是 Struts 和 Hibernates 的新手。我的作业是创建登录、注册和更改密码页面。我已经能够创建登录页面,但我不断收到 java.lang.ClassCastException。我不知道我做错了什么,我已经有一段时间了,请帮助我。谢谢你。以下是我的详细信息(对不起长度):
**Login.jsp**
......
<html:form action="login">
<bean:message key="label.username" />
<html:text property="userName"></html:text>
<html:errors property="userName" />
<br/>
<bean:message key="label.password"/>
<html:password property="password"></html:password>
<html:errors property="password"/>
<html:submit/>
<html:reset/>
</html:form>
<%
if(request.getAttribute("msg")!=null){
%>
<%= request.getAttribute("msg") %>
<%} request.setAttribute("msg","null"); %>
<%
if(request.getAttribute("msg10")!=null){
%>
<%= request.getAttribute("msg10") %>
<%} request.setAttribute("msg10","null"); %>
</body>
</html>
**struts-config.xml**
.....
<form-beans>
<form-bean name="LoginForm"
type="com.company.strutsuser.LoginForm" />
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards></global-forwards>
<action-mappings>
<action path="/login" name="LoginForm" validate="true" input="/login.jsp"
type="com.company.strutsuser.LoginForm">
<forward name="success" path="/success.jsp" />
<forward name="failure" path="/login.jsp" />
<forward name="failure1" path="/ChangePassword.jsp" />
<forward name="failure2" path="/login.jsp" />
</action>-->
</action>
</action-mappings>
<message-resources parameter="resource.MessageResource"></message-resources>
.....
**MessageResource.properties**
label.username = Login Detail
label.password = Password
label.welcome = Welcome
error.username =Username is not entered.
error.password = Password is not entered.
**success.jsp**
.....
<H1>Welcome!</H1>
<H2>Below are the necessary details</H2>
<table class="successtable" border="1" cellspacing="10">
<c:forEach var="ulogin" items="${ulogin}" varStatus = "varCounter">
<tr>
<td>${ulogin.phone_number}</td>
<td>${ulogin.name}</td>
</tr>
</c:forEach>
</table>
<button type="button" onclick="myfunction();">Logout</button><BR />
.....
**LoginForm**
public class LoginForm extends ActionForm{
private String userName;
private String password;
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors actionErrors = new ActionErrors();
if(userName == null || userName.trim().equals("")) {
actionErrors.add("userName", new ActionMessage("error.username"));
}
try {
if(password == null || password.trim().equals("")) {
actionErrors.add("password", new ActionMessage("error.password"));
}
}catch(Exception e) {
e.printStackTrace();
}
return actionErrors ;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPhone_number() {
return phone_number;
}
public void setPhone_number(int phone_number) {
this.phone_number = phone_number;
}
private String name;
private int phone_number;
public boolean isNew_user() {
return new_user;
}
public void setNew_user(boolean new_user) {
this.new_user = new_user;
}
boolean new_user;
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
**LoginAction**
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction trans = null;
LoginForm loginForm = (LoginForm) form;
String name = loginForm.getUserName();
String password =loginForm.getPassword();
boolean new_user = false;
LoginForm lform = null;
String result ="failure";
try{
trans = session.beginTransaction();
List ulogin = session.createQuery("from LoginForm").list();
for (Iterator iterator = ulogin.iterator(); iterator.hasNext();){
lform = (LoginForm) iterator.next();
if (lform.getUserName().equals(name)&& lform.getPassword().equals(password)&& lform.isNew_user()==new_user){
result ="success";
break;
}
if (lform.getUserName().equals(name)&& lform.getPassword().equals(password)&& lform.isNew_user()!=new_user){
result="failure1";
}
if(lform.getUserName().equals(name)&& !lform.getPassword().equals(password)){
result = "failure2";
break;
}
}
if(result.equals("success")){
LoginForm forml = new LoginForm();
forml.setUserName(lform.getUserName());
forml.setName(lform.getName());
forml.setPhone_number(lform.getPhone_number());
forml.setPassword(lform.getPassword());
ulogin.add(forml);
request.setAttribute("ulogin", ulogin);
}
if(result.equals("failure")){
request.setAttribute("msg", "Email and Password Entered are Wrong");
}
if(result.equals("failure2")){
request.setAttribute("msg10", "Password Entered is Wrong");
}
}
catch (HibernateException e){
if (trans!=null)
trans.rollback();
e.printStackTrace(); }
return mapping.findForward(result);
**user.hbm.xml**
<hibernate-mapping>
<class name="com.company.strutsuser.LoginForm" table="user">
<meta attribute="class-description">
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<property name="userName" column="email" type="string"/>
<property name="password" column="password" type="string"/>
<property name="phone_number" column="phone_number" type="string"/>
<property name="new_user" column="new_user" type="string"/>
</class>
</hibernate-mapping>
**Error message the compiler gives**
java.lang.ClassCastException: com.company.strutsuser.LoginForm cannot be cast to org.apache.struts.action.Action