<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config>  <form-beans>
 <form-bean name="addform" type="ch02.AddForm" />
  </form-beans>
<action-mappings>
<action name="addform" path="/add" scope="request" 
type="ch02.AddAction" >
<forward name="input" path="/ch02/add.jsp" />
<forward name="result" path="/ch02/jieguo.jsp" />
</action>
</action-mappings></struts-config>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  <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>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>package ch02;import javax.servlet.http.*;import org.apache.struts.action.*;public class AddAction extends ActionForm {
   private Calculator biz=new Calculator();
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
   ActionForward af=null;
   AddForm addform=(AddForm)form;
   double resultData=biz.add(addform.getNum1(), addform.getNum2());
   request.setAttribute("result", resultData);
   af=mapping.findForward("result");
   return af;
   }
}
package ch02;import org.apache.struts.action.ActionForm;public class AddForm extends ActionForm {
   private double num1;
   private double num2;
public double getNum1() {
return num1;
}
public void setNum1(double num1) {
this.num1 = num1;
}
public double getNum2() {
return num2;
}
public void setNum2(double num2) {
this.num2 = num2;
}
   
}package ch02;public class Calculator {
   public double add(double a,double b){
   return a+b;
   }
   public double multiply(double a,double b){
   return a*b;
   }
}
<%@ page language="java" import="java.util.*" pageEncoding="GBK" contentType="text/html; charset=GBK" %><html>
  <head>  <title>加法器</title>
  <style type="text/css">
<!--
.STYLE1 {font-size: 36px}
-->
  </style>
  </head>
  
  <body>   <p class="STYLE1">加法器</p>
  <form name="form1" method="post" action="/add.do">
    <table width="60%" border="0">
      <tr>
        <td width="22%">第一个</td>
        <td width="78%"><label>
          <input type="text" name="num1">
        </label></td>
      </tr>
      <tr>
        <td>第二个</td>
        <td><input type="text" name="num2"></td>
      </tr>
      <tr>
        <td><label>
          <input type="submit" name="Submit" value="加">
        </label></td>
        <td></td>
      </tr>
    </table>
    </form>
  <p>&nbsp; </p>
  </body>
</html>
提示错误
2010-5-12 23:07:09 org.apache.struts.action.RequestProcessor processMapping
严重: Invalid path was requested /ch02/addHTTP Status 404 - Invalid path was requested--------------------------------------------------------------------------------type Status reportmessage Invalid path was requesteddescription The requested resource (Invalid path was requested) is not available.
希望高手指点 谢谢

解决方案 »

  1.   

     path="/ch02/add.jsp"  
    似乎是该路径不存在
      

  2.   

    看不出。你的jsp是否放在WebContent下面?
      

  3.   

    public class AddAction extends ActionForm 
    继承错了public class AddAction extends Action
      

  4.   

    struts-config.xml的配置
    <struts-config>
      <data-sources />
      <form-beans >
        <form-bean name="addForm" type="com.yourcompany.struts.form.AddForm" />  </form-beans>  <global-exceptions />
      <global-forwards />
      <action-mappings >
        <action
          attribute="addForm"
          input="/add.jsp"
          name="addForm"
          path="/add"
          scope="request"
          type="com.yourcompany.struts.action.AddAction">
          <forward name="result" path="/result.jsp" />
        </action>  </action-mappings>  <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
    </struts-config>
    web.xml的配置
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      <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>
        <init-param>
          <param-name>debug</param-name>
          <param-value>3</param-value>
        </init-param>
        <init-param>
          <param-name>detail</param-name>
          <param-value>3</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    action的代码
    /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     */
    package com.yourcompany.struts.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.yourcompany.struts.DAO.Dao;
    import com.yourcompany.struts.form.AddForm;/** 
     * MyEclipse Struts
     * Creation date: 05-13-2010
     * 
     * XDoclet definition:
     * @struts.action path="/add" name="addForm" input="/add.jsp" scope="request" validate="true"
     */
    public class AddAction extends Action {
    /*
     * Generated Methods
     */ Dao dao=new Dao();
    /** 
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    AddForm addForm = (AddForm) form;// TODO Auto-generated method stub
     double result=0;
     
     result=dao.add(addForm.getFirst(), addForm.getSecound());
     if (result!=0) {
    request.setAttribute("result",result);
    return mapping.findForward("result");
    }
    return null;
    }
    }
    Dao类的代码
    package com.yourcompany.struts.DAO;/***
     * 数据访问层
     * 方法
     * @author Administrator
     *
     */
    public class Dao {  public double add(double a ,double b)
     {
      return a+b;
     }

    }
    add.jsp 的页面代码
    <%@ page language="java" pageEncoding="GBK"%>
    <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
    <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%><html>
    <head>
    <title>JSP for AddForm form</title>
    </head>
    <body>
    <html:form action="/add">
    第一个数 : <html:text property="first" />
    <html:errors property="first" />
    <br />
    第二个数 : <html:text property="secound" />
    <html:errors property="secound" />
    <br />
    <html:submit  value="加"/>
    </html:form>
    </body>
    </html>result.jsp返回的结果页面代码
    <%@ page language="java" import="java.util.*" pageEncoding="GBK"%><html>
      <head>
        
        <title>结果页面</title>  </head>
      
      <body>
        您得到的结果是:${requestScope.result }
      </body>
    </html>
      

  5.   

    给楼主的建议:
    多看使用struts框架开发的成功实例项目!比较自己做的项目你会有丰富的收获的!
      

  6.   

    已经搞定了 谢谢大家啊 <form name="form1" method="post" action="add.do">
    改为
     <form name="form1" method="post" action="../add.do">
    就可以了谢谢大家对我的关注!