0

I am studying a Struts2. And I have struts.xml file:

<?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>

I have my 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;
    }

}

And I have jsp files.

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>

When I press sumbit button I get an error:

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

I can't find a reason of this error, please help me!!

Roman C
  • 49,761
  • 33
  • 66
  • 176
Yuriy
  • 1,370
  • 4
  • 14
  • 30

1 Answers1

0

specify namespace attribute of <s:form> tag which should be match with namespace attribute in your struts.xml config file.

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

</body>
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
AsSiDe
  • 1,826
  • 2
  • 15
  • 24
  • check updated answer. Don't forget to restart your container/server. – AsSiDe Mar 10 '16 at 20:42
  • And again nothing changed – Yuriy Mar 10 '16 at 20:51
  • I nothing understand. I restarted eclipse, and program earned without error. And I start my initial code, without changes – Yuriy Mar 10 '16 at 20:56
  • 1
    you almost always need to restart your tomcat/application server after changing configurations. – AsSiDe Mar 10 '16 at 21:00
  • I did this every time. I assume that some bug in eclipse caused this error – Yuriy Mar 10 '16 at 21:04
  • 1
    @Yuriy Eclipse might has bugs but it doesn't matter, the matter is when you run your application it is missing configuration settings that is available in `struts.xml`. If you deployed it locally Eclipse store files in metadata folder, you should check this folder that has all required files. and because you are new here you should read [how to accept/upvote answers](http://stackoverflow.com/tour). – Roman C Mar 11 '16 at 10:06