404 when running project through eclipse on Tomcat

2019-09-17 13:51发布

问题:

I have a Struts 2 project with following directory structure. But when I try to run this project using Eclipse on Tomcat 7, it gives me 404 error.

Struts.xml:

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

<struts>
  <action name="login" class="com.actions.LoginAction" method="execute">
     <result name="success">/jsp/login.jsp</result>  
  </action>
</struts>

login.jsp:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
  <s:form action="login" method="post">
  Login name : <input type="text" value="name"/>
  </s:form>
</body>
</html>

On running at path http://localhost:8080/StrutsPro/jsp/login.jsp, it gives HTTP 404, why?

回答1:

examine web.xml instead.

else put your all .jsp files in WebContent folder



回答2:

As per your code structure, you are not having index.jsp/html in your project roof folder. While running the server it will first find out the index.html/jsp by default. If its not available it will returns the 404 error. To fix this you can add the following lines your web.xml file,

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

Save all your changes and run your server. If the problem again exists you have to check Struts configuration file and the build path.



回答3:

Your project is really strange.

By the way you must:

  • declare a <package> in your struts.xml; Actions don't go directly inside <struts> element;

  • give that package the "/StrutsPro" namespace;

  • declare a method for checking the user credentials in your LoginAction (then execute() will open the JSP page to be filled by the user, and tryToLogin(), for example, will receive the data coming from the POST and try to validate them against the database); alternatively, you can specify two different Actions instead of one Action with two methods, it's up to you;

  • put a <s:submit /> button inside your <s:form> to send data to the login method/Action;

  • call the url pointing to your Action, NOT TO YOUR JSP. JSP is the view, it is always returned by the controller, that in Struts2 is the Action. You run the Action, the Action gives you the evaluated JSP. You don't run the JSP.

For example you should call: http://localhost:8080/StrutsPro/login.action or http://localhost:8080/StrutsPro/login.do , according to the extension you give to Actions in web.xml



回答4:

Try this answer:

login.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
  <s:form action="login" method="post">
           Login name : <s:textfield name="name"/> 
           <s:submit value="Click Me" name="Click Me"></s:submit>
  </s:form>
</body>
</html>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="login"  class="com.actions.LoginAction">
<result name="success">/jsp/login.jsp</result>  
</action>
</package>
</struts> 

LoginAction.java

private String name;

//Create setter and getter methods for NAME.

public String execute(){
  return SUCCESS;
}

You can save all your changes and restart your server. To access the page you have enter the url http://localhost:8080/StrutsPro/login.action

Hope this will helps you.



回答5:

im new to struts and i encountered the same error. Together with 404, in console, it has UnableToLodaConfiguration error although i already put the correct configuration of struts.xml. Someone had advised me to relocate my workspace from Desktop to C:\ and suprisingly it worked.



回答6:

404 status code actually means that resource by the given url is not available.

To request resource from the server make sure it's valid and compilable having taglib definitions included to the document.

<%@ taglib prefix="s" uri="/struts-tags" %>

You should not access JSP pages directly in the URL. Use action configuration that returns a dispatcher result.

<action name="UnderConstruction">
    <result>/UnderConstruction.jsp</result>
</action>