1

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

代码片段如下:

班级问候语

     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");}

        }

类 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>
4

1 回答 1

0

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

它应该会弹出一个属性窗格,底部有两个选项卡OverviewModules. 单击Modules,您应该会看到应用程序的路径。对我来说,它的形式是:/webapps/<project name>

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

于 2013-06-15T14:12:41.100 回答