Struts2 如何将 URL 映射到操作?

How does Struts2 map URLs to actions?

我想了解我很久以前用过的东西,但不知道它是如何工作的。

当我点击这样的超链接时:

<a href="name_action.action">Link</a>

Struts2 如何激活映射到 struts.xml 文件中的操作?

<action name="name_action">
    <result type="redirect">web/page.jsp</result>
</action>

Struts2 将 URL 映射到具有 ActionMapper 的操作。

The ActionMapper interface provides a mapping between HTTP requests and action invocation requests and vice-versa.

When given an HttpServletRequest, the ActionMapper may return null if no action invocation request matches, or it may return an ActionMapping that describes an action invocation for the framework to try.

The ActionMapper is not required to guarantee that the ActionMapping returned be a real action or otherwise ensure a valid request. Accordingly, most ActionMappers do not need to consult the Struts configuration just to determine if a request should be mapped.

Just as requests can be mapped from HTTP to an action invocation, the opposite is true as well. However, because HTTP requests (when shown in HTTP responses) must be in String form, a String is returned rather than an actual request object.


By default, the DefaultActionMapper is used:

Default action mapper implementation, using the standard *.[ext] (where ext usually "action") pattern. The extension is looked up from the Struts configuration key struts.action.extension.

注意:你不应该直接登陆 JSP,而是在通过一个动作后调度它们。