gpt4 book ai didi

java - struts2 应用程序默认为 error.jsp

转载 作者:行者123 更新时间:2023-11-29 00:00:19 24 4
gpt4 key购买 nike

I am creating a struts2 application. I want users to login to their account. If the record is found, it should go to the success.jsp. If the account is not found, it should default to to error.jsp. They can then register. The problem I am having is that whether the records are in the database or not, I get redirected to my error.jsp. I would appreciate some help here with some like explanation to go with it

这是我的文件:

学生信息

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;
}

}

学生信息.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>

成功.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 block 中(ex 是你的异常名称)。

关于java - struts2 应用程序默认为 error.jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30125351/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com