找到NoT即使Tomcat没有显示任何错误

时间:2013-06-02 04:44:12

标签: struts

我试图在eclipse上运行这个简单的例子。当我运行服务器时它没有显示任何错误。但是在浏览器中它显示404错误页面未找到。我不明白错误是什么。我也使用了WEB-INF / Lib文件夹中的所有Jars。

代码链接如下:

CLASS HELLOWORLDACTION

     package com.action;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        import org.apache.struts.action.Action;
        import org.apache.struts.action.ActionForm;
        import org.apache.struts.action.ActionForward;
        import org.apache.struts.action.ActionMapping;

        import com.form.HelloWorldForm;
        public class HelloWorldAction extends Action{

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

                HelloWorldForm helloWorldForm = (HelloWorldForm) form;
                helloWorldForm.setMessage("Hello World! Struts");
                return mapping.findForward("success");}

        }

CLASS HelloWorldForm

package com.form;

     import org.apache.struts.action.ActionForm;

     public class HelloWorldForm extends ActionForm{

     String message;

   public String getMessage() {
   return message;
  }
     public void setMessage(String message) {
   this.message = message;
  }

 }

的struts-config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
    "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">

    <struts-config>

        <form-beans>
            <form-bean name="helloWorldForm" 
                type="com.form.HelloWorldForm"/>
        </form-beans>

        <action-mappings>
            <action path="/helloWorld"
                type="com.action.HelloWorldAction"
                name="helloWorldForm">
                <forward name="success" path="/HelloWorld.jsp"/>
                </action>
        </action-mappings>

    </struts-config>

Web.xml中

 <?xml version="1.0" encoding="UTF-8"?>
 <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
            <display-name>Maven Struts Examples</display-name> 
          <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>
            <load-on-startup>1</load-on-startup>
          </servlet>

          <servlet-mapping>
               <servlet-name>action</servlet-name>
               <url-pattern>*.do</url-pattern>
          </servlet-mapping>

        </web-app>

的helloWorld.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <h1><bean:write name="helloWorldForm" property="message" />
    </h1>
    </body>
    </html>

1 个答案:

答案 0 :(得分:0)

听起来你使用的是错误的网址。 Tomcat通常有一个名为webapps的文件夹,用于部署在该服务器上的应用程序。要弄清楚配置,请在Eclipse中打开Server选项卡,找到Tomcat服务器并双击它。

它应该会打开一个属性窗格,其底部有两个标签OverviewModules。点击Modules,您应该会看到应用的路径。对我而言,它的形式为:/webapps/<project name>

您的完整网址为localhost:8080/webapps/<project name>/anystringhere.do,因为您将应用程序中的任何内容(localhost:8080/webapps/<project name>/)以.do结尾映射到操作servlet。