2015-09-23 45 views
-1

不會進入成功頁面。請告訴我哪裏出錯了。 1.XML:struts2登錄使用jsp與mysql數據庫的應用程序

<?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: 我覺得問題就在這裏。 pelase詳細看,讓我知道

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> 
+0

請發佈更多詳情。堆棧跟蹤,預期結果和當前發生的情況。 – piechuckerr

+0

謝謝...如果我們輸入ID和密碼登錄頁面..它必須打開成功頁面,而是它轉到錯誤頁面....並驗證,如果我們沒有輸入數據在用戶ID,它沒有給任何驗證...我不知道我要去哪裏錯 – dpk12

+0

請檢查下面發佈的答案 – piechuckerr

回答

0

您的變量名稱應與標籤名稱屬性。 替換你的當前變量。

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

LoginDao: 在你查詢你忘記PWD場比較,其中PWD =?如果結果集不爲空,則不需要進行等式檢查返回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; 
+0

謝謝...但它仍然在錯誤頁面。 – dpk12

+0

做了一些更改的答案,請檢查它!併發布控制檯日誌如果它仍然無法正常工作 – piechuckerr

相關問題