如何在Struts 2中使用没有后缀的URL(例如.action)? [英] How to have URLs without a suffix (e.g .action) in Struts 2?

查看:138
本文介绍了如何在Struts 2中使用没有后缀的URL(例如.action)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我需要的东西,我不仅需要使用我的.war来提供java,还需要一些javascript文件。所以e,g。如果有人转到该网址:

Here's the thing, I need to not only serve java, but also some javascript files, with my .war. So e,g. if someone goes to the URL:

example.com/js/foo.jar

然后我需要将其正确地作为javascript文件提供。同时,如果有人去:

Then I need that to be properly served as a javascript file. At the same time, if someone goes to:

example.com/bar

我需要将Struts2作为可能的控制器提供服务。

I need that to be served by Struts2 as a possible controller.

我的方法在网上发现从网址中删除后缀,会导致两个URls由struts 2提供(因此第一个 foo.js 文件会出错,即使它存在)。是否有一种方法(例如拦截器)在给出错误之前首先检查给定的.js文件是否存在?

The methods I've found online of removing the suffix from the url, would cause both URls to be served by struts 2 (and hence give an error for the first foo.js file even though it exists). Is there a way (such as an interceptor) which will first check if the given .js file exists, before giving an error?

推荐答案

DarkHorse的答案并不完全准确。有两个值非常相似但工作方式不同:

DarkHorse's answer is not entirely accurate. There are two values that are very similar but work very differently:

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

您几乎肯定想要第一个选项,其中包括一个逗号。这告诉Struts动作扩展名为空。这也意味着Struts不会尝试匹配具有扩展名的任何请求,例如* .css,* .js等。

You almost certainly want the first option, which includes a comma. This tells Struts that the action extension is empty. It also means that Struts will not attempt to match any requests that do have an extension, such as *.css, *.js, etc.

第二个选项告诉Struts不应该有一个动作扩展,但它也说你希望Struts匹配每个请求。我能想到的唯一可能的好处是,如果您想在动作映射中包含文件扩展名,例如:

The second option tells Struts that there shouldn't be an action extension, but it also says that you want Struts to match every request. The only possible benefit I can think of to this is if you wanted to include file extensions in your action mapping, such as the following:

<action name="robots.txt" class="...">
    ...
</action>

另外,要从Struts过滤器中排除请求,请使用 struts。 action.excludePattern 属性,如 here 。不要依赖拦截器,因为拦截器只在框架找到合适的映射动作后运行。

Also, to exclude requests from the Struts filter is to use the struts.action.excludePattern property, as described here. Do not rely on an interceptor for this, since interceptors only run once the framework has located a suitable action to map to.

这篇关于如何在Struts 2中使用没有后缀的URL(例如.action)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆