Java web应用程序为什么使用.do扩展名?它的起源是什么?

123

我一直想知道为什么许多Java开发人员将 ".do" 作为其Web控制器(MVC)资源的扩展名。例如:http://example.com/register.do

这似乎甚至不是特定于框架的,因为我在Spring MVC和Struts项目中都看到过它。 这个 ".do" 扩展名惯例从哪里来?为什么要这样做而不是没有扩展名? 我感觉好像错过了Java世界的备忘录。

就我个人而言,我更喜欢没有扩展名。


5
对于想从“.do”迁移并使用友好的URL的人,友情提示:请使用servlet路径而不是扩展名,例如/do/login,然后使用Tuckey Filter URL重写将/do/login改写为/login。 - Adam Gent
True,URL重写(无论是通过mod_rewrite还是Tuckey的过滤器)都可以解决问题。 - Pascal Thivent
1
这很愚蠢,没有任何借口。 - irreputable
在Struts2中,对于.actionURL末尾的.action/.event是什么意思? - li ki
2个回答

81
据我所知,这种约定是由Struts1推广的。用户指南如此描述:

5.4.2 Configure the ActionServlet Mapping

Note: The material in this section is not specific to Struts. The configuration of servlet mappings is defined in the Java Servlet Specification. This section describes the most common means of configuring a application.

There are two common approaches to defining the URLs that will be processed by the controller servlet -- prefix matching and extension matching. An appropriate mapping entry for each approach will be described below.

Prefix matching means that you want all URLs that start (after the context path part) with a particular value to be passed to this servlet. Such an entry might look like this:

<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>/do/*</url-pattern>
</servlet-mapping>

which means that a request URI to match the /logon path described earlier might look like this:

http://www.mycompany.com/myapplication/do/logon

where /myapplication is the context path under which your application is deployed.

Extension mapping, on the other hand, matches request URIs to the action servlet based on the fact that the URI ends with a period followed by a defined set of characters. For example, the JSP processing servlet is mapped to the *.jsp pattern so that it is called to process every JSP page that is requested. To use the *.do extension (which implies "do something"), the mapping entry would look like this:

<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

and a request URI to match the /logon path described earlier might look like this:

http://www.mycompany.com/myapplication/logon.do

WARNING - The framework will not operate correctly if you define more than one <servlet-mapping> element for the controller servlet.

WARNING - If you are using the new module support since version 1.1, you should be aware that only extension mapping is supported.

我认为这个惯例已经被保留了(有时候甚至不改变URL,即使替换了Struts1,有时候只是因为人们对此感到满意)。


2
我想这应该是Struts 1。那是很久以前的事了,我可能已经忘记了。那些对于Java Web开发来说并不是那么美好的日子。 - Adam Gent
4
@Adam 那个时候(约2001年),我对Struts1感到很满意。今天使用它会让我哭泣。 - Pascal Thivent
5
2001年,我们很幸运能够拥有Struts 1! - Thorbjørn Ravn Andersen
2
有了Struts 2,我们都可以感到高兴! - Alireza Fattahi

10

在web.xml中将struts servlet映射到*.do是一种常见的做法,以便将URL传递给struts servlet。例如:

<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

没有其他原因,只是惯例而已。如果不使用扩展名,您需要做一些特殊处理,以便以不会将图像和其他静态内容发送到您的servlet的方式处理它们。通常在负载均衡器或前端Web服务器上完成此操作。


2
我不认为这是魔法。它只是有一个主调度servlet,可能进行一些URL重写,以避免使用/myservlet/前缀。请参阅Tuckey URL重写。 - Adam Gent

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接