各位高手大家好。我最近想搭建一个SSH的开发框架出来,由于没有用过spring,搭建过程中遇到了种种问题。目前最棘手的就是页面报错说: HTTP Status 404 - Servlet action is not available--------------------------------------------------------------------------------type Status reportmessage Servlet action is not availabledescription The requested resource (Servlet action is not available) is not available.
上网查了很多资料,但是最终都没有解决,没办法只能发帖上来,希望各路高手能针对我的情况,给与指点。先谢谢了。
下面是我的部分代码,请大家看看:
环境: jdk1.5 eclipse3.2 MyEclipse5.0  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>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<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></web-app>struts-config.xml<?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>
<data-sources />
<form-beans>
<form-bean name="loginForm"
type="com.tepia.form.LoginForm" />
<form-bean name="registerForm"
type="com.tepia.form.RegisterForm" />
</form-beans> <global-exceptions /> <global-forwards /> <action-mappings> <action attribute="loginForm"
input="/jsp/user/login.jsp" name="loginForm"
path="/login" scope="request"
type="org.springframework.web.struts.DelegatingActionProxy"> <forward name="login" path="/jsp/user/login.jsp" /> <forward name="success" path="/jsp/user/login.jsp" /> <forward name="error" path="/jsp/user/login.jsp" /> <forward name="register"
path="/jsp/user/register.do?method=toRegister" /> </action> <action attribute="registerForm"
input="/jsp/user/register.jsp" name="registerForm"
path="/register" scope="request"
type="org.springframework.web.struts.DelegatingActionProxy"> <forward name="success"
path="/jsp/user/register.jsp" /> <forward name="error" path="/jsp/user/register.jsp" /> <forward name="register"
path="/jsp/user/register.jsp" /> </action> </action-mappings>
<controller >
<set-property property="processorClass" 
value="org.springframework.web.struts.DelegatingRequestProcessor" />
</controller>
<message-resources parameter="com.tepia.struts.ApplicationResources" />
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" 
value="/WEB-INF/classes/applicationContext.xml" />
</plug-in>
</struts-config>applicationContext.xml , 建立文件时在src下,编译发布后在/WEB-INF/classes/下<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--注册工具自动生成的Bean,比如Hibernate的dataSource Bean, SessionFactory Bean和各个POJO对象对应的DAO操作类Bean  -->
<beans>
<bean id="testBeanID"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</property>
<property name="url">
<value>jdbc:microsoft:sqlserver://localhost:1433</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value>sa</value>
</property>
</bean>
<bean id="testSFID"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="testBeanID" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/tepia/pojo/TUser.hbm.xml</value>
<value>com/tepia/pojo/TCourse.hbm.xml</value>
</list>
</property>
</bean>
<bean id="userService"
class="com.tepia.service.impl.UserServiceImpl"> <property name="userDAO"> <ref bean="TUserDAO" /><!-- 在applicationContext.xml中注册过的bean的id --> </property> </bean>
<bean id="TUserDAO" class="com.tepia.pojo.TUserDAO">
<property name="sessionFactory">
<ref bean="testSFID" />
</property>
</bean>
<bean id="TCourseDAO" class="com.tepia.pojo.TCourseDAO">
<property name="sessionFactory">
<ref bean="testSFID" />
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="testSFID" /> </property> </bean> <bean id="baseServiceProxy" abstract="true" lazy-init="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <!-- 
注释了的property标签,里面定义的是所有要求进行事务管理对象中,需要进行事务管理的公共方法的声明.
如果项目中需要进行事务管理的对象中,有很多要进行事务管理的方法的名称一样或差不多,
我们就可以在这里进行事务配置,这样就省出了在每个事务对象中都要做重复配置的麻烦.
我们这里没有进行共性方法的配置,所以就隐去了该段的配置

<property name="transactionAttributes">

<props>

<prop key="add*">PROPAGATION_REQUIRED</prop>

<prop key="get*">PROPAGATION_REQUIRED</prop>

<prop key="update*">PROPAGATION_REQUIRED</prop>

<prop key="validate*">PROPAGATION_REQUIRED</prop>

<prop key="do*">PROPAGATION_REQUIRED</prop>

<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>

</props>

</property>

-->
</bean>
<!-- 代理的id是userServiceProxy,注意,这名字组成部分是有代理目标的bean的名字,加上Proxy组成.

Bean的parent属性值,是我们在上面配置的事务代理bean的id值
-->
<bean id="userServiceProxy" parent="baseServiceProxy"> <property name="target"> <ref bean="userService" /> </property> <property name="transactionAttributes"> <props> <prop key="register">PROPAGATION_REQUIRED</prop> <prop key="verifyUser">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean>
<bean name="/register" class="com.tepia.action.UserAction"> <property name="userService"> <ref bean="userServiceProxy" /> </property> </bean> <bean name="/login" class="com.tepia.action.UserAction"> <property name="userService"> <ref bean="userServiceProxy" /> </property> </bean>
</beans>

解决方案 »

  1.   

    UserAction.javapackage com.tepia.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 org.apache.struts.actions.DispatchAction;
    import org.springframework.web.struts.DispatchActionSupport;import com.tepia.form.LoginForm;
    import com.tepia.form.RegisterForm;
    import com.tepia.service.UserService;
    import com.tepia.service.impl.UserServiceImpl;
    /**
     * @struts.action path="/login" name="loginForm"
     * 
     * input="/jsp/user/login.jsp" parameter="method"
     * 
     * scope="request" validate="true"
     * 
     * @struts.action-forward name="success" path="/WEB-INF/jsp/user/login.jsp"
     * 
     * @struts.action-forward name="error" path="/WEB-INF/jsp/user/login.jsp"
     * 
     * @struts.action-forward name="register" path="/WEB-INF/jsp/user/register.jsp"
     * 
     */
    //public class UserAction extends DispatchActionSupport {
    //public class UserAction extends DispatchAction{
    public class UserAction extends Action{
    private UserService userService = null; /*
     * 
     * Generated Methods
     * 
     */ public void setUserService(UserService userService) { this.userService = userService; }

    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    LoginForm loginForm = (LoginForm) form;
    System.out.println("##########");
    String username = (String) loginForm.getUserName();
    this.toLogin(mapping, form, request, response);
    return mapping.findForward("error");
    }


    /**
     * 
     * Method login
     * 
     * 
     * 
     * @param mapping
     * 
     * @param form
     * 
     * @param request
     * 
     * @param response
     * 
     * @return ActionForward
     * 
     */ public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { String path = "error"; String message = "操作成功"; this.initServices(); LoginForm loginForm = (LoginForm) form; String userName = loginForm.getUserName(); String password = loginForm.getPassword(); int result = this.userService.verifyUser(userName, password); if (result == 0) { path = "success"; } else { if (result == 1) { // 用户名不存在 message = "用户名不存在"; } else if (result == 2) {// 用户名密码错误 message = "用户名密码错误"; } else { // 用户名或密码为空 message = "用户名或密码为空"; } } loginForm.setMessage(message); return mapping.findForward(path); } public ActionForward toRegister(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { return mapping.findForward("register"); } public ActionForward toLogin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
    System.out.println("in action UserAction.toLogin(...)................");
    return mapping.findForward("login"); } public ActionForward register(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { this.initServices(); String path = "error"; String message = "操作成功"; RegisterForm registerForm = (RegisterForm) form; String userName = registerForm.getUserName(); String password = registerForm.getPassword(); String name = registerForm.getName(); String email = registerForm.getEmail(); String phone = registerForm.getPhone(); int result = this.userService.register(userName, password, name, email, phone); if (result == 0) { path = "success"; } else { if (result == 1) {// 用户名已经存在 message = "用户名已经存在"; } else if (result == 2) {// 用户名为空 message = "用户名为空"; } else if (result == 3) {// 密码为空 message = "密码为空"; } else if (result == 4) {// 姓名为空 message = "姓名为空"; } else {// 其它错误 message = "其它错误"; } } registerForm.setMessage(message); return mapping.findForward(path); } private void initServices() { if (this.userService == null) { this.userService = new UserServiceImpl(); } }}写了测试类
    public static void main(String[] args) {
    String[] path = { "/WebRoot/WEB-INF/classes/applicationContext.xml" };
    ApplicationContext ctx = new FileSystemXmlApplicationContext(path);
    TUserDAO dao = (TUserDAO) ctx.getBean("TUserDAO");
    UserServiceImpl userService = (UserServiceImpl)ctx.getBean("userService");
    UserAction ua = (UserAction)ctx.getBean("/login");

    System.out.println(dao.findByUmane("u1"));
    int recode = userService.verifyUser("u1", "qweasd");//0为成功
    ActionMapping mapping = new ActionMapping() ;
    ActionForm form = new LoginForm();
    HttpServletRequest request = null; 
    HttpServletResponse response = null;

    ua.toLogin(mapping, form, request, response);

    System.out.println("recode="+recode);
    }都可以成功进入到 TUserDAO ,UserServiceImpl ,ua 中。
      

  2.   

    index.jsp ,在webroot目录下 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title> </title>
        
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->  </head>
      
      <body>
      <form name="frm" action="login.do">
       <input type='hidden' name="method" id="method" value="toLogin" >
        <input type='submit' value='登陆'> <br>
      </body>
    </html>classPath内容;
    <?xml version="1.0" encoding="UTF-8"?>
    <classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="con" path="com.genuitec.eclipse.j2eedt.core.J2EE14_CONTAINER"/>
    <classpathentry kind="con" path="melibrary.com.genuitec.eclipse.hibernate.MYECLIPSE_HIBERNATE3_CORE"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/antlr.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-beanutils.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-digester.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-fileupload.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-logging.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-validator.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jakarta-oro.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/struts.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-attributes-api.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-attributes-compiler.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-collections.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-lang.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jaxen-1.1-beta-4.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/log4j-1.2.13.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-logging-1.0.4.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/dom4j-1.6.1.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/ehcache-1.1.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/hibernate3.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jaas.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jaxen-1.1-beta-7.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jdbc2_0-stdext.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jta.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/msbase.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/mssqlserver.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/msutil.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/xml-apis.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/JAMon.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/burlap-2.1.12.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/c3p0-0.9.0.2.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/cglib-2.1.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/cglib-nodep-2.1_3.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-dbcp.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-httpclient.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-pool.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/cos.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/db-ojb-1.0.3.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/easymock.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/easymockclassextension.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/freeer.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/hessian-2.1.12.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/ibatis-common-2.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/ibatis-sqlmap.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/itext-1.3.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jasperreports-1.0.3.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jdo2.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jmxremote.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jmxremote_optional.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jmxri.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jotm.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/junit.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jxl.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/mysql-connector-java-3.1.12-bin.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/mysql-connector-java-3.1.8-bin.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/poi-2.5.1.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/quartz-1.5.1.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/toplink-api.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/velocity-1.4.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/velocity-tools-generic-1.1.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/velocity-tools-view-1.1.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/xapool.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring.jar"/>
    <classpathentry kind="output" path="WebRoot/WEB-INF/classes"/>
    </classpath>以上是我觉得有关系的主要主要代码,请各位高手指点阿,如果还需要什么部分的代码,告诉我,我给你贴。
      

  3.   

    当我打开index.jsp页面,点登陆按钮,页面就会报错说Servlet action is not available。
      

  4.   

    struts: <action ... parameter="method">
    可能是这个吧
      

  5.   

    struts: <action ... parameter="method"> 
    这个本来是写了的,而且action是继承的DispatchAction ,可是不行,我就把action改成继承 Action 了,并且把struts-config.xml 中的 parameter="method" 也去掉了,可是还是不行。
    还有什么别的地方么?
      

  6.   

    在index.jsp中
    把<form name="frm" action="login.do"> 改为<form name="frm" action="${pageContext.request.contextPath}/login.do?method=toLogin"> 
    把 <input type='hidden' name="method" id="method" value="toLogin" > 去掉
      

  7.   

    改为 <form name="frm" action="${pageContext.request.contextPath}/login.do?method=toLogin"> 
    也不行,我直接在地址栏打http://localhost:8080/sshdemo/login.do?method=toLogin 也不行
      

  8.   

    spring让我很受伤,希奇古怪问题一堆,
      

  9.   

    动态表单提交的话必须得继承DispatchAction 这个类,改成继承Action的话那肯定是找不到了!
      

  10.   


    本来是继承的DispatchAction ,可是总也没有反应,就改称action试试。现在你这样说了,我又改回来了,但是还是没有任何反应。
    在帮帮忙吧。。各位高手们,你们看看我的代码或配置哪里有问题啊?
      

  11.   

    可能是spring包导的有问题。
    另外,像你的这种写法应该继承DispatchAction。
      

  12.   

    <action attribute="registerForm" 
    input="/jsp/user/register.jsp" name="registerForm" 
    path="/register"[/color] scope="request"<bean [color=#FFFF00]name="/login" class="com.tepia.action.UserAction"> 
    <property name="userService"> 
    <form name="frm" action="login.do"> 问题应该就出在这三个标记的地方,我首先这三个路径要一致吧,而且把路径加到前面比较好
      

  13.   

    可能是导包有问题,把Hibrenate的包重导一下看,我也遇到过,由Sring代理生成ACtion<servlet> 
    <servlet-name>context </servlet-name> 
    <servlet-class> 
    org.springframework.web.context.ContextLoaderServlet 
    </servlet-class> 
    <load-on-startup>1 </load-on-startup> 
    </servlet> 这一段可以不要吧