[英]struts2 application defaulting to error.jsp
我正在創建一個struts2應用程序。 我希望用戶登錄到他們的帳戶。 如果找到該記錄,則應轉到success.jsp。 如果找不到該帳戶,則應默認為error.jsp。 然后他們可以注冊。 我遇到的問題是,無論記錄是否在數據庫中,我都會重定向到我的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>
在您的LoginAction類中:
String sQry = "SELECT firstName FROM StudentInfo WHERE studentinfo.userName=? AND studentinfo.password=?";
PreparedStatement q = conn.prepareStatement(sQry);
q.setInt(1, studentId);
q.setString(2, userName);
q.setString(3, password);
在查詢中,您有兩個參數(用戶名,密碼)。 但是,您嘗試設置三個(studentId,userName,password)。
要查看控制台中的錯誤,請編寫ex.printStackTrace();。 在您的catch塊中(例如,您的例外名稱)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.