2013-08-16 26 views
0

我是Struts新手,試圖執行登錄表單。但是它沒有在ACTION類中的默認構造函數之後執行。Struts 1登錄應用程序示例錯誤


LoginForm.java

package struts.login.action; 

import org.apache.struts.action.ActionForm; 

@SuppressWarnings("serial") 
public class LoginForm extends ActionForm { 

public LoginForm() { 

} 

private String username; 
private String password; 
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; 
} 


} 

LoginAction.java

package struts.login.action; 

import javax.servlet.ServletRequest; 
import javax.servlet.ServletResponse; 

import org.apache.struts.action.Action; 
import org.apache.struts.action.ActionForm; 
import org.apache.struts.action.ActionForward; 
import org.apache.struts.action.ActionMapping; 

public class LoginAction extends Action { 

private final static String SUCCESS = "success"; 
private final static String FAILURE = "failure"; 
//private final static String FAILURE = "failure"; 

public LoginAction() { 
    System.out.println("default constructor of Login Action"); 
} 

@Override 
public ActionForward execute(ActionMapping mapping, ActionForm form, 
     ServletRequest request, ServletResponse response) throws Exception  { 

    LoginForm loginForm = (LoginForm) form; 
    if(loginForm.getUsername().equals(loginForm.getPassword())){ 
     return mapping.findForward(SUCCESS);  
    } 
    else{ 
     return mapping.findForward(FAILURE);  
    } 

} 
} 

的Login.jsp

<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%> 
<html> 
<head> 
<title>Login Page</title> 
</head> 
<body> 
<div style="color:red"> 

</div> 
<html:form action="/Login" method="get"> 
    User Name :<html:text property="username"/><br> 
    Password :<html:password property="password"/> 
    <html:submit value="Login Here" /> 
</html:form> 
</body> 
</html> 

的success.jsp

<html> 
<head> 
<title>Insert title here</title> 
</head> 
<body> 
Successfully logged in! 
</body> 
</html> 

failure.jsp

<html> 
<head> 
<title>Insert title here</title> 
</head> 
<body> 
login failed please try again! 
</body> 
</html> 

struts-config.xml中

<?xml version="1.0" encoding="UTF-8"?> 

<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://struts.apache.org/dtds/struts-config_1_3.dtd"> 

<struts-config> 
<form-beans> 
    <form-bean name="loginForm" type="struts.login.action.LoginForm"> 
    </form-bean> 
</form-beans> 

<action-mappings> 
    <action path="/Login" name="loginForm"    type="struts.login.action.LoginAction" > 
    <forward name="success" path="/success.jsp"></forward> 
    <forward name="failure" path= "/failure.jsp"></forward> 
    </action> 


</action-mappings> 

</struts-config> 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app id="WebApp_ID" version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

<welcome-file-list> 
    <welcome-file>login.jsp</welcome-file> 
</welcome-file-list> 
<!-- Standard ActionServlet Configuration --> 
<servlet> 
    <servlet-name>action</servlet-name> 
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> 
    <init-param> 
     <param-name>config</param-name> 
     <param-value>/WEB-INF/struts-config.xml</param-value> 
    </init-param> 
    <init-param> 
     <param-name>application</param-name> 
     <param-value>ApplicationResources</param-value> 
    </init-param> 
    <init-param> 
     <param-name>debug</param-name> 
     <param-value>3</param-value> 
    </init-param> 
    <init-param> 
     <param-name>detail</param-name> 
     <param-value>3</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<!-- Standard ActionServlet Mapping --> 
<servlet-mapping> 
    <servlet-name>action</servlet-name> 
    <url-pattern>*.do</url-pattern> 
</servlet-mapping> 

<!-- Struts Tag Library Descriptors --> 
<jsp-config> 
<taglib> 
    <taglib-uri>/WEB-INF/tld/struts-html.tld</taglib-uri> 
    <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location> 
</taglib> 
</jsp-config> 
</web-app> 

我已經包含在struts-html.tld文件中的WEB-INF/tld文件夾。 用戶名和密碼登錄後它不重定向到任何的success.jsp或failure.jsp頁

+0

歡迎來到Stackoverflow,你得到了什麼類型的異常? –

+1

如果你是struts的新手,並且爲了自己的知識閱讀它,那麼比我的建議是從Struts2開始,而不是struts,因爲它已經宣佈它已經結束了。 –

+0

我沒有在我的控制檯中發現任何異常。所以我認爲它可能與罐子有關。 – Roikka

回答

0

那是因爲你已經通過的ServletRequest和ServletResponse的行動類而不是HttpServletRequest和HttpServlet的響應在執行方法.. 使用像繼

public ActionForward execute(ActionMapping mapping, ActionForm form, 
    HttpServletRequest request, HttpServletResponse response) throws Exception  { 
} 

並導入這些東西:

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

希望它會運行

相關問題