2016-03-10 82 views
0

我正在學習Struts2。我有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="default" namespace = "/" extends="struts-default" > 
     <action name="loginStudent" class = "org.dream.action.LoginAction"> 
      <result name="login">/login.jsp</result> 
      <result name="success">/index.jsp</result> 
      <result name="input">/login.jsp</result> 
     </action> 
    </package> 



</struts> 

我有我的LoginAction.class

package org.dream.action; 

import org.apache.commons.lang3.StringUtils; 

import com.opensymphony.xwork2.Action; 
import com.opensymphony.xwork2.ActionSupport; 

public class LoginAction implements Action { 

    private String studentId; 
    private String password; 

    public String execute(){ 
     if(getStudentId().equals("test")||getPassword().equals("test")){ 
      return SUCCESS; 
     }else return LOGIN; 
    } 


    public String getStudentId() { 
     return studentId; 
    } 

    public void setStudentId(String studentId) { 
     this.studentId = studentId; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

} 

而且我有jsp文件。

的Login.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<%@ taglib prefix="s" uri="/struts-tags" %> 
<!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="loginStudent"> 
     <s:textfield key="studentId" label="Student Id"/> 
     <s:textfield key="password" label="Password"/> 
     <s:submit/> 
    </s:form> 

</body> 
</html> 

當我按下按鈕,請跟隨我得到一個錯誤:

HTTP Status 404 - There is no Action mapped for namespace [/] and action name [loginStudent] associated with context path [/Struts2Demo] 

我找不到原因這個錯誤,請幫助我!

回答

0

指定<s:form>標記的名稱空間屬性,它應該與struts.xml配置文件中的namespace屬性匹配。

<body> 
    <s:form action="loginStudent" namespace="/"> 
     <s:textfield key="studentId" label="Student Id"/> 
     <s:textfield key="password" label="Password"/> 
     <s:submit/> 
    </s:form> 

</body> 
+0

沒有什麼變化.. – Yuriy

+0

檢查更新後的答案。不要忘記重新啓動你的容器/服務器。 – AsSiDe

+0

再次沒有任何變化 – Yuriy

相關問題