【问题标题】:struts2 application defaulting to error.jspstruts2 应用程序默认为 error.jsp
【发布时间】:2015-07-19 11:01:38
【问题描述】:

我正在创建一个 struts2 应用程序。我希望用户登录到他们的 帐户。如果找到记录,它应该转到success.jsp。如果 找不到帐户,它应该默认为error.jsp。他们能 然后注册。我遇到的问题是记录是否 在数据库中与否,我被重定向到我的error.jsp。 我会很感激这里的一些帮助和一些类似的解释

这是我的文件:

学生信息

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>

【问题讨论】:

  • 你调试了吗?
  • 尝试调试并检查是否有来自数据库的数据。
  • 现在在服务器上运行调试

标签: java mysql eclipse hibernate struts2


【解决方案1】:

在您的 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 块中(ex 是你的异常名称)。

【讨论】:

  • 客人,我对那个查询表示歉意,我已经编辑了它。但问题仍然存在。
  • @CODI 1) 尝试调试应用程序。 2)尝试在可能抛出错误之前编写 sysout 语句 3)我更喜欢 - 如果您可以 printStackTrace() 异常并向我们显示错误消息,那么它会有所帮助
  • 我无法粘贴所有的 stace,但这是相关的:org.hibernate.HibernateException: getJdbcConnectionAccess is not valid without active transaction
  • org.hibernate.HibernateException: getJdbcConnectionAccess 在 org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352) 在 com.sun.proxy.$ 没有活动事务的情况下无效Proxy6.getJdbcConnectionAccess(Unknown Source) at org.action.LoginAction.login(LoginAction.java:103) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
猜你喜欢
  • 1970-01-01
  • 2011-11-14
  • 2010-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-14
  • 2011-04-12
  • 1970-01-01
相关资源
最近更新 更多