简体   繁体   English

在Tomcat上通过Eclipse运行项目时出现404

[英]404 when running project through eclipse on Tomcat

I have a Struts 2 project with following directory structure. 我有一个具有以下目录结构的Struts 2项目。 But when I try to run this project using Eclipse on Tomcat 7, it gives me 404 error. 但是,当我尝试在Tomcat 7上使用Eclipse运行该项目时,出现了404错误。

在此处输入图片说明

Struts.xml : 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 : 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? 在路径http://localhost:8080/StrutsPro/jsp/login.jsp ,它显示HTTP 404,为什么?

examine web.xml instead. 请改为检查web.xml。

else put your all .jsp files in WebContent folder 否则将所有.jsp文件放入WebContent文件夹

As per your code structure, you are not having index.jsp/html in your project roof folder. 根据您的代码结构,您的项目Roof文件夹中没有index.jsp / html。 While running the server it will first find out the index.html/jsp by default. 在运行服务器时,默认情况下它将首先查找index.html / jsp。 If its not available it will returns the 404 error. 如果不可用,它将返回404错误。 To fix this you can add the following lines your web.xml file, 要解决此问题,您可以在web.xml文件中添加以下几行,

<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. 如果问题仍然存在,则必须检查Struts配置文件和构建路径。

Your project is really strange. 您的项目真的很奇怪。

By the way you must: 顺便说一句,您必须:

  • declare a <package> in your struts.xml ; 在您的struts.xml中声明<package> Actions don't go directly inside <struts> element; 动作不能直接在<struts>元素内进行;

  • give that package the "/StrutsPro" namespace; 给那个包一个“ / StrutsPro”名字空间;

  • 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); 在您的LoginAction中声明一种检查用户凭证的方法(然后execute()将打开要由用户填充的JSP页面,而tryToLogin()例如将接收来自POST的数据并尝试针对它们进行验证数据库); 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; <s:form>内放置一个<s:submit />按钮,以将数据发送到登录方法/操作;

  • call the url pointing to your Action, NOT TO YOUR JSP. 调用指向您的操作的URL,而不是指向您的JSP的URL。 JSP is the view, it is always returned by the controller, that in Struts2 is the Action. JSP是视图,它总是由控制器返回,在Struts2中是Action。 You run the Action, the Action gives you the evaluated JSP. 您运行该Action,该Action为您提供了评估的JSP。 You don't run the JSP. 您不运行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 例如,您应该根据在web.xml中为“操作”提供的扩展名,致电: http://localhost:8080/StrutsPro/login.actionhttp://localhost:8080/StrutsPro/login.do

Try this answer: 试试这个答案:

login.jsp 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 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 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 要访问该页面,您输入了URL http://localhost:8080/StrutsPro/login.action

Hope this will helps you. 希望这对您有帮助。

404 status code actually means that resource by the given url is not available. 404状态代码实际上意味着给定URL的资源不可用。

To request resource from the server make sure it's valid and compilable having taglib definitions included to the document. 要从服务器请求资源,请确保其有效且可编译,并且在文档中包含taglib定义

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

You should not access JSP pages directly in the URL. 您不应直接在URL中访问JSP页面。 Use action configuration that returns a dispatcher result. 使用返回调度程序结果的操作配置

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

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. 与404一起,在控制台中,它具有UnableToLodaConfiguration错误,尽管我已经正确配置了struts.xml。 Someone had advised me to relocate my workspace from Desktop to C:\\ and suprisingly it worked. 有人建议我将工作区从桌面重定位到C:\\,但令人惊讶的是它可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM