我正在創建一個struts2應用程序。我希望用戶登錄到他們的 帳戶。如果找到記錄,則應該轉到success.jsp。如果 該帳戶未找到,它應該默認爲error.jsp。他們可以 然後註冊。我遇到的問題是,無論數據庫中的記錄是否爲 ,我都會重定向到我的error.jsp。 我將在這裏得到一些幫助,一些喜歡解釋去用它struts2應用程序默認爲error.jsp
這裏是我的文件:
StudentInfo
package org.comp.dto;
//import
public class StudentInfo implements Serializable{
private int studentId;
private String userName;
private String password;
private String password1;
private String firstName;
private String lastName;
private int age;
private String dateofBirth;
private Calendar dateCreated;
private GregorianCalendar lastLogin;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
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 getPassword1() {
return password1;
}
public void setPassword1(String password1) {
this.password1 = password1;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDateofBirth() {
return dateofBirth;
}
public void setDateofBirth(String dateofBirth) {
this.dateofBirth = dateofBirth;
}
public GregorianCalendar getLastLogin() {
return lastLogin;
}
public void setLastLogin(GregorianCalendar lastLogin) {
this.lastLogin = lastLogin;
}
public Calendar getDateCreated() {
return dateCreated;
}
public void setDateCreated(Calendar dateCreated) {
this.dateCreated = dateCreated;
}
}
StudentInfo.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class mutable="true" name="org.comp.dto.StudentInfo" table="studentinfo">
<id name="studentId" type="int">
<column name="studentId"/>
<generator class="native"/>
</id>
<property column="userName" name="userName" type="string" not-null="true"/>
<property column="password" name="password" type="string" not-null="true"/>
<property column="firstName" name="firstName" type="string" not-null="true"/>
<property column="lastName" name="lastName" type="string" not-null="true"/>
<property column="age" name="age" type="int" not-null="true"/>
<property column="dateofBirth" name="dateofBirth" type="string" not-null="true"/>
<property column="dateCreated" name="dateCreated" type="calendar" not-null="true"/>
<property column="lastLogin" name="lastLogin" type="timestamp" not-null="true"/>
</class>
</hibernate-mapping>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="loginpackage" namespace="/" extends="struts-default">
<action name="login" class="org.action.LoginAction" method="login">
<result name="success">success.jsp</result>
<result name="input">login.jsp</result>
<result name="error">error.jsp</result>
</action>
<action name="accountSetUpAction" class="org.action.LoginAction" method="accountSetUp">
<result name="success">success.jsp</result>
</action>
</package>
</struts>
的success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Success page</title>
</head>
<body>
Login successfully! <s:property value="firstName"/>
</body>
</html>
LoginAction類
package org.action;
//import
public class LoginAction extends ActionSupport implements ModelDriven{
private int studentId;
private String userName;
private String password;
private String firstName;
private StudentInfo studentUser = new StudentInfo();
private Session session;
private String message;
PreparedStatement sQry;
ResultSet rs;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
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 StudentInfo getStudentUser() {
return studentUser;
}
public void setStudentUser(StudentInfo studentUser) {
this.studentUser = studentUser;
}
public LoginAction() {
this.session = HibernateUtil.getSessionFactory().getCurrentSession();
}
public void validate() {
if (StringUtils.isEmpty(studentUser.getUserName())){
addFieldError("userName", "Username cannot be blank");
}
if (StringUtils.isEmpty(studentUser.getPassword())){
addFieldError("password", "Password cannot be blank");
}
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
public String login(){
String ret = ERROR;
Connection conn = null;
try {
SessionImplementor impl = (SessionImplementor)session;
conn = impl.getJdbcConnectionAccess().obtainConnection();
org.hibernate.Transaction tx = session.beginTransaction();
String sQry = "SELECT firstName FROM StudentInfo WHERE studentinfo.studentId=? AND studentinfo.userName=? AND studentinfo.password=?";
PreparedStatement q = conn.prepareStatement(sQry);
q.setInt(1, studentId);
q.setString(2, userName);
q.setString(3, password);
rs = q.executeQuery();
while (rs.next()){
rs.getString(2);
ret = SUCCESS;
}
}
catch(Exception ex){
ret = ERROR;
}
finally
{
if (conn !=null){
try {
session.getTransaction().rollback();
conn.close();
}
catch (Exception ex){
}
}
}
return ret;
}
public String accountSetUp() throws Exception{
Transaction tx = null;
try {
tx = session.beginTransaction();
Calendar dateCreated = new GregorianCalendar();
studentUser.setDateCreated(dateCreated);
session.save(studentUser);
session.getTransaction().commit();
return SUCCESS;
}
catch (HibernateException ex){
if(tx != null) tx.rollback();
ex.printStackTrace();
return null;
}
finally
{
session.close();
}
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Override
public Object getModel() {
return studentUser;
}
}
的login.jsp的
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<s:form action="login" method="get">
<s:textfield label="Please StudentID:" key="studentId"/>
<s:textfield label="Please Enter User Name:" key="userName"/>
<s:password label="Please Enter Password:" key="password"/>
<s:submit/>
</s:form>
</body>
</html>
你試過調試嗎? –
嘗試調試並檢查您是否有來自數據庫的數據。 – Ajit
現在在服務器上運行調試 – CODI