Java 在struts.xml中配置action类时,请求的资源在struts2中不可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19860795/
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:47:05  来源:igfitidea点击:

The requested resource is not available in struts2 when action class is configured in struts.xml

javajsptomcatstruts2

提问by Pravin

I've tried to create a new action class and add it to struts.xml, the action class successfully get compiled but when I run tomcat it shows The requested resource is not available 404 error on index page itself.

我尝试创建一个新的操作类并将其添加到struts.xml,该操作类已成功编译,但是当我运行 tomcat 时,它显示请求的资源不可用 404 错误在索引页本身上。

CreateUser.jsp

创建用户.jsp

<s:form action = "CreateUserAction" >
    <s:textfield name = "name" label = "Name" />
    <s:textfield name = "userName" label = "User Name" />
    <s:submit value = "Register" />
</s:form>

CreateUserAction.java

创建用户操作.java

public String execute() {
    setMessage("Hello " + getUserName());
    return "SUCCESS";
}

Struts.xml

Struts.xml

<package name="default" extends="struts-default">
    <action name="CreateUserAction" class="com.ecommerce.action.CreateUserAction">
    <result name="SUCCESS">/success.jsp</result>
    </action>
</package>

采纳答案by Roman C

Actions in Struts2 are mapped to the URLs that are built from the configuration. When the url of the request match the action name and namespace then it is executed, otherwise error code 404 is returned by the dispatcher. Struts2 is implemented as a filter and it's looking for the requests that are mapped in it's configuration. For example if I want it to filter all URLs I will write in web.xml

Struts2 中的操作被映射到从配置构建的 URL。当请求的 url 与操作名称和命名空间匹配时,它就会被执行,否则调度程序将返回错误代码 404。Struts2 被实现为一个过滤器,它正在寻找映射到它的配置中的请求。例如,如果我希望它过滤我将写入的所有 URLweb.xml

<filter>
  <filter-name>struts2</filter-name>
  <filter-class>
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  </filter-class>
</filter>
 <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

If you say the error 404 on index page itself, then you should ask yourself which URL corresponds to it and what action is mapped via configuration to that URL. If you need more detailed picture of what configuration is and what URLs you could enter to execute an action install the config-browser plugin.

如果您在索引页面本身上说错误 404,那么您应该问问自己哪个 URL 对应于它,以及通过配置将哪些操作映射到该 URL。如果您需要更详细地了解什么是配置以及可以输入哪些 URL 来执行操作,请安装 config-browser 插件。

回答by Mithun Mathew

for the above issue:"The requested resource is not available in struts2 when action class is configured in struts.xml" I have a suggestion.

对于上述问题:“当在 struts.xml 中配置动作类时,请求的资源在 struts2 中不可用” 我有一个建议。

Copy and paste all the external jars in in the folder lib(WebContent-->WEB-INF-->lib) remove all external jars from Build path (Project -->buildpath-->Configure Build path)

将所有外部 jar 复制并粘贴到文件夹 lib(WebContent-->WEB-INF-->lib) 中,从 Build path (Project -->buildpath-->Configure Build path) 中删除所有外部 jar

I found this solution is working fine for eclipse Luna with Tomacat 7

我发现这个解决方案适用于使用 Tomacat 7 的 eclipse Luna