struts2 使用 jsp 和 mysql 数据库登录应用程序

struts2 Login Application using jsp with mysql database

未进入成功页面。请告诉我哪里出错了。 1.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<welcome-file-list>
    <welcome-file>/Login.jsp</welcome-file>
</welcome-file-list>

<context-param>
    <param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
    <param-value>/WEB-INF/tiles.xml</param-value>
</context-param>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>

2.Login.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="html" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<link href="css/style.css" rel='stylesheet' type='text/css' />
</head>
<body>  
<html:form action="loginaction" method="post">
<html:textfield name="emp_id" label="Employee ID"/>
<html:password name="Pwd" label="Password"/>
<html:checkbox label="Remember" name="checkboxField1" value="aBoolean"       fieldValue="true"/>
    <html:submit value="login"></html:submit>  
    </html:form>
</body>
</html>

3.LoginAction: 我觉得问题就出在这里。请详细查看并告诉我

package controller;

import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

public class LoginAction extends ActionSupport {
String Emp_id;
String Pwd;    

public String getEmp_id() {
    return Emp_id;
}

public void setEmp_id(String Emp_id) {
    this.Emp_id = Emp_id;
}

public String getPwd() {
    return Pwd;
}

public void setPwd(String Pwd) {
    this.Pwd = Pwd;
}

@Override
public String execute() throws ClassNotFoundException{
    HttpServletRequest req = ServletActionContext.getRequest();
    setEmp_id(req.getParameter("Emp_id"));
    setPwd(req.getParameter("Pwd"));
    LoginDao ld = new LoginDao();
    if (ld.checkLogin(getEmp_id(), getPwd())) {
    return SUCCESS;
    } else
    return ERROR;
}

@Override
public void validate() {
     if("".equals(getEmp_id())){
        addFieldError("Emp_id", "ID must be filled !");
     }
        if("".equals(getPwd())) {
        addFieldError("Pwd", "Password must be filled !");
     }
     }
   }

4.LoginDao:

package controller;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginDao {   
public boolean checkLogin(String Emp_id, String Pwd) {
boolean status = false;
Connection conn = null;
try {
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
        e.printStackTrace();
        }   
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/timesheetdb", "root", "lion");
String query = "select Emp_id,Pwd from employee where Emp_id=?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, Emp_id);
ps.setString(2, Pwd);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
     if (rs.getString("Emp_id").equals(Emp_id)&& (rs.getString("Pwd").equals(Pwd))) {
            status = true;
            } else {
status = false;
}
}
} catch (SQLException e) {
} finally {
if (conn != null)   
    try {
        conn.close();
    } catch (SQLException e) {
// TODO Auto-generated catch block
        e.printStackTrace();
        }
}      
return status;
}
}  

5.Struts.xml:

 <!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="controller" extends="struts-default">
    <action name="loginaction" class="controller.LoginAction">
        <result name="input">/Login.jsp</result>
        <result name="success">/success.jsp</result>
        <result name="error">/error.jsp</result>
    </action>
</package>
</struts>

您的变量名称应与标签名称属性相匹配。 替换您当前的变量。

private String emp_id; 

并为此变量生成新的 getter 和 setter。 你在这里不需要 request.getParameter(),struts2 会在内部完成。

    @Override
    public String execute() throws ClassNotFoundException
    {
      LoginDao ld = new LoginDao();
      if (ld.checkLogin(getEmp_id(), getPwd()))  // replace with new getters() of emp_id and Pwd
      {
          return SUCCESS;
      } 
      else
        return ERROR;
    }

登录道: 在您的查询中,您忘记了 pwd 字段比较 where pwd = ? 并且您不需要 equals 检查结果集是否为空 return true。检查发布的代码

String query = "select Emp_id,Pwd from employee where Emp_id=? and Pwd=?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, Emp_id);
ps.setString(2, Pwd);
ResultSet rs = ps.executeQuery();

if(rs.next())
   return true;
else 
   return false;