如果我删除 struts2 .action 扩展名,为什么欢迎文件列表不起作用?

2022-01-16 00:00:00 java struts2 action-mapping

如果我在我的 Struts2 应用程序中删除 .action 扩展,我会遇到问题.我把它放在我的 struts.xml 中:

I have a problem if I remove the .action extension inside my Struts2 application. I put this in my struts.xml:

<constant
    name="struts.action.extension"
    value="" />

应用程序正常工作,但在索引页面中除外.我的 web.xml 中有这个:

The application works correctly except in the index page. I have in my web.xml this:

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

当我访问 http://localhost/myApp/ 时,我收到以下错误:

When I access to http://localhost/myApp/, I get the following error:

There is no Action mapped for namespace [/] and 
action name [index.jsp] associated with context path [/myApp]. 
- [unknown location]

但是,如果我访问 http://localhost/myApp/fooAction,我不会收到任何错误并且可以正常工作.

However if I access to http://localhost/myApp/fooAction, I'm not getting any errors and works perfectly.

如果我将扩展名更改为非空扩展名(如 "html"),如果我访问 http://localhost/myApp/.

If I change the extension for a non empty extension (like "html"), I see the index page perfectly if I access to http://localhost/myApp/.

那么,我在做什么有问题吗?为什么我在删除扩展程序时会收到此错误?有没有可能的方法不得到它?

So then, is there something wrong in what I'm doing? Why am I getting this error when I remove extension? Is there any posible way of not getting it?

如果我在 <welcome-page> 中添加操作,错误如下:

If I put an action in the <welcome-page> the error is the following:

There is no Action mapped for namespace [/] and action name [] 
associated with context path [/myApp].

推荐答案

我在其中一个应用程序中遇到了同样的问题,我需要在页面加载时调用 Action 来代替 index.jsp<welcome-page> 中的 welcom.jsp.我做了以下步骤

I was having same issue in one of the application where i need to call an Action on page load in place of index.jsp or welcom.jsp in <welcome-page>.I did the following steps

在我的 web.xml 中放置了以下条目.

Placed the following entry in my web.xml.

 <welcome-file-list>
            <welcome-file>index</welcome-file>
</welcome-file-list>

我在我的 web-app 文件夹中创建了一个名为 index 的空文件,最后将以下条目放在我的 struts.xml 文件中

I created an empty file with name index in my web-app folder and finally placed the following entry in my struts.xml file

<action name="index" class="welcomeAction">
     <result>/ab.jsp</result>
 </action>

所以在这种情况下,当我点击这个 URL www.myapp.com/myApp 时,它调用了 Struts2 的索引操作,我能够为我的欢迎页面完成所有初始化工作.

So in this case when i was hitting this URL www.myapp.com/myApp,its calling index action of Struts2 and i was able to do all init work for my welcome page.

相关文章