javax.servlet.UnavailableException:缺少路径 /WEB-INF/struts-config.xml 的配置资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19875210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:58:35  来源:igfitidea点击:

javax.servlet.UnavailableException: Missing configuration resource for path /WEB-INF/struts-config.xml

javajspweb-applicationsstruts

提问by user2935193

I am new to Struts and I am trying to run a sample login web-application using Struts but I am unable to get the result due to (javax.servlet.UnavailableException: Missing configuration resource for path /WEB-INF/struts-config.xml)the error. I had done everything in configuring struts in eclipse. I even don't no what the exception is? please anyone help me

我是 Struts 的新手,我正在尝试使用 Struts 运行示例登录 Web 应用程序,但由于(javax.servlet.UnavailableException: Missing configuration resource for path /WEB-INF/struts-config.xml)错误,我无法获得结果。我已经完成了在 Eclipse 中配置 struts 的所有工作。我什至不知道有什么例外?请任何人帮助我

the errors I am getting are:

我得到的错误是:

javax.servlet.UnavailableException: Missing configuration resource for path /WEB-INF/struts-config.xml
at org.apache.struts.action.ActionServlet.splitAndResolvePaths(ActionServlet.java:1872)
at org.apache.struts.action.ActionServlet.initModuleConfig(ActionServlet.java:683)
at org.apache.struts.action.ActionServlet.init(ActionServlet.java:356)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1213)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1026)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4421)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4734)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
at org.apache.catalina.core.StandardService.start(StandardService.java:525)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414) 

index.jsp:

index.jsp

<html> 
    <body>


        <form action="login.do">
       <tr>
       <td>Username</td> 
       <td><input type = "text" name = "username" maxlength = "25"></td>
       </tr>
       <tr>
       <td>Password</td>
       <td><input type = "password" name = "password" minlength = "8" maxlength = "16"></td>
       </tr>

       <tr>
       <td  colspan = "2" align = "center"><button type = "submit" name = "submit">Submit</button>
       <button type = "reset"   name = "reset">Reset</button></td>

       </tr>


        </form>
    </body>
</html>

LoginForm.java:

LoginForm.java

package com.sample.com;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionForward;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class LoginAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res)
    {
        System.out.println("test");

    LoginForm login = (LoginForm)form;

    String uname=login.getUname();

    String pass=login.getPass();

    if(uname=="admin" && pass =="test")
    {
    return mapping.findForward("success");

    }
    else
    {
    return mapping.findForward("failure");

    }
  }
}

LoginAction.java:

LoginAction.java

package com.sample.com;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionForward;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;



public class LoginAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res)
    {

        System.out.println("test");

    LoginForm login = (LoginForm)form;

    String uname=login.getUname();

    String pass=login.getPass();

    if(uname=="admin" && pass =="test")
    {
    return mapping.findForward("success");
    }
    else
    {
    return mapping.findForward("failure");

    }

  }


}

config-struts.xml:

config-struts.xml

<?xml version="1.0" encoding="UTF-8" ?>



<struts-config>

    <form-beans>
        <form-bean name="loginRequest" type="com.sample.com.LoginForm"/>
    </form-beans> 

    <global-forwards>
        <forward name="success"  path="/success.jsp"/>
    </global-forwards>

    <action-mappings>
        <action path="/login" type="com.sample.com.LoginAction" name="loginRequest">
            <forward name="success" path="/success.jsp"/>
            <forward name="failure" path="/failure.jsp"/>        
        </action>
    </action-mappings>

</struts-config>

web.xml:

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.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>2</param-value>
        </init-param>
        <init-param>
            <param-name>detail</param-name>
            <param-value>2</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

回答by Dark Knight

Make sure struts-config.xmlresides in WEB-INF folder. Similarly, hibernate.cfg.xmlshould also be at the appropriate place(if you are using it).

确保struts-config.xml位于 WEB-INF 文件夹中。同样,hibernate.cfg.xml也应该在适当的地方(如果你正在使用它)。

回答by Roman C

You are configured the web application to use struts-config.xml

您已配置要使用的 Web 应用程序 struts-config.xml

<init-param>
    <param-name>config</param-name>
    <param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>

but there's not the file with such name in the specified folder. Make sure the file exist there or if you use another name for the struts configuration file you could change this init param value to reflect the file availability. If you have several configuration files each for the module, you could use a comma to separate names.

但是在指定的文件夹中没有这样名称的文件。确保该文件存在于那里,或者如果您为 struts 配置文件使用另一个名称,您可以更改此 init 参数值以反映文件可用性。如果模块有多个配置文件,则可以使用逗号分隔名称。

回答by Rama

You need to add the servlet-api.jarfrom the apache libfolder. You need to include the struts.xmlfolder in srcfolder under java resources.

您需要从 apache lib文件夹中添加servlet-api.jar。您需要在 java 资源下的src文件夹中包含struts.xml文件夹。

回答by Yithirash

1) You have mentioned your struts.xml filename as config-struts.xmlbut in the init-param you wrote as struts-config.xml. Please check the names properly.

1) 您已经将 struts.xml 文件名提到为config-struts.xml但在 init-param 中您写为struts-config.xml。请正确检查名称。

2) Or maybe (common issue) because you are missing the DTD tag in your struts xml file. Try to add this code above the <struts-config>tag in your struts-config.xml.

2)或者可能(常见问题)因为您在 struts xml 文件中缺少 DTD 标记。尝试将此代码添加到struts-config.xml 中<struts-config>标记上方。

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">