调用 action 名称的页面应该放在 namespace 的名称里面(文件夹,路径)
<package name="example" namespace="/example" extends="struts-default">
<action name="HelloWorld" class="example.HelloWorld">
<result>/example/HelloWorld.jsp</result>
</action>
HelloWorld.jsp 文件应该放在 namespace="/example" example 文件夹里面. 否则调用 action 会出错.
关于 namespace :
struts.xml 文件中 package 元素下 namespace 属性的作用
说在前面的话:
namespace的作用是控制相应package下的action的url地址,url地址在web编程中是基础中的基础. 我们的程序不同的功能实际上就是对相应url地址的访问来触发的,这个要牢牢掌握,有点象java的classpath
原文: http://www.blogjava.net/Unmi/archive/2008/05/26/203014.html
Struts2 的 struts.xml 中是分 package 配置的,可以为 package 设置 namespace 属性,如
<package namespace="/secure" ....>
......
</package>
如果没有指定 namespace 属性,默认 namespace 是 ""。使用 namespace 可以方便于按不同目的规划对应用的访问规则。比如不同 namespace 下配置了不同的拦截器就可以实现权限的控制,如 "/secure" 下已登陆用户才能访问,"/public" 下可公开访问的。
配置了 namespace 直接就是反应在访问 URL 上,例如 namespace="/secure" name="test" 的 action
<package namespace="/secure" ....>
<action name="test" ....
</package>
访问它的 URL 就是 http://ip:port/context/secure/test.action,那如果在 namespace "/secure" 下没有 test action 会出现什么情况呢?Struts 还会尝试在默认 namespace,即 "" 下找 test。
再举个例子,URL 是 http://ip:port/context/some/path/test.action 时,如果在 "/some/path" namespace 下找不到 test action,也是到 "" (default namespace) 下找 test action,但不会去 "/some" 下找的。
用标签 <s:url value="/secure/test.action"/> 对应页面源文件是 /context/secure/test.action
稍有麻的就是 <s:form action="/secure/test.action" .... 对应的源文件是 <form action="/context/secure/test.action" ...
但是后台会有警告:
警告: No configuration found for the specified action: '/secure/test.action' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
Struts2 把 action 属性值当整一个 Action Name 了,但这也不影响使用,这个 URL 正好能与 (package namespace) + (action name) 合上拍。
但是对于使用了动态方法调用(struts.enable.DynamicMethodInvocation = true)就没这么幸运了。很容易想当然的
<s:form action="/secure/test!update.action" .... 生成的 HTML 源文件却是 action="/TestStruts2/om/test"
同时后台的警告信息是:
警告: No configuration found for the specified action: '/secure/test' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
很显然对于这个 action="/TestStruts2/om/test",提交时是会得到 HTTP Status 404 - /context/secure/test 错误。
正确的用法是 <s:action...> 也有一个 namespace 属性,对了,就是
<s:form namespace="/secure" action="test!login"> 生成的 HTML 源文件是:<form action="/TestStruts2/om/test!login.action" ....>
我们要的就是这个。
如果不配置 namespace 属性,我们能不能在访问 action 时也用上目录层次呢?可以,那是在 struts1 习惯的做法,配置 <action name="secure/test" ....> name 中使用斜杠,但在 Struts2 中 Action Name 中使用斜杠需要设置
struts.enable.SlashesInActionNames=true 默认为 false
可是 Struts2 大概不赞同这种做法,力挺 namespace 的作用。
对于上面使用了斜框的 Action Name,<s:form 中的写法要用
<s:form action="secure/test"> 生成 HTML 源文件:<form action="/context/secure/test.action" .....
<s:form action="secure/test!update"> 生成 HTML 源文件:<form action="/context/secure/test!login.action" .....
上面的 action 后加不加 .action 无所谓,只是要保证 <s:form> 的 action 属性一定要与 struts.xml 中的 <action> 的 name 匹配上,如果你自作多情的在前面加个斜杠,如写成了
<s:form action="/secure/test!update"> 、 <s:form action="/secure/test"> 或者 <s:form action="/secure/test!update.action"> 生成的 HTML 源文件就都成了:<form action="/context/secure/test" .....
这也是从 Struts1 带来的弊病,因为 Struts1 中 <html:form> action 属性对应的是 <action> 的 path,而 Struts2 中 <s:form> 的 action 属性对应的是 <action> 的 name;name 要完全匹配,path 可以加些层次。
我为何以费这么些功夫来搞清楚这个呢,也就是因为使用 <s:form action=""> 是时而行,时而不行,很似迷惑,一度怀疑是应用服务器在从中作梗。坦白的说,就是写本篇日志完成红线以上的内容时仍未能知晓就理,这也正好映证了写下来的意义。因为在这过程中你在试图让别人更好的理解,倘若自己都说不清,他人只能是云里雾里了
分享到:
相关推荐
前几天在网上下载一个struts2的helloword的例子,那个作者也真够缺德的,搞个错误的程序,害得我查了一天的程序错误。 最后发现竟然是struts.xml被写成啦sturts.xml。 碰见这样的问题先鄙视下提供例子的作者, 再...
HTTP Status 404 - There is no Action mapped for namespace and action name BackMemberGroupAudit..doc
http://localhost:8080/ struts2会拦截,出现异常信息 There is no Action mapped for namespace / and action name.默认配置的welcomefile没有起作用。 按附件配置即可解决此问题
该文档对weblogic使用过程中,包括下载,安装,部署...6. weblogic部署war包action不能访问问题解决方法 [There is no Action mapped for namespace / and action name]. 5 7. java.lang.StackOverflowError. 5 等等
在网上找了好个struts2的例子结果都不好使报There is no Action mapped for namespace / and action name这个错,没办法自己搞了个好用的,myeclipse 6.0 +tomcat5.5 + jdk 1.5 引入项目后直接发布就可以了!
There is no flow logic, no business logic, and no model information -- just tags. Tags are one of the things that make Struts unique compared to other frameworks like Velocity. Note: "Think thin" ...
MDB: A Memory-Mapped Database and Backend for OpenLDAP
To reserve or commit memory and unintentionally not release it when it is no longer being used. A process can leak resources such as process memory, pool memory, user and GDI objects, handles, threads...
--------------------------------------------------------------------------------...Map with errors mapped from fieldname (String) to Collection of String error messages ---------------------------------...
is larger than the largest known FADT version, and 2) if there is a mismatch between a 32-bit block address and the 64-bit X counterpart (when both are non-zero.) Example Code and Data Size: These ...
Memory-Mapped Files for Qualcomm Brew By Ray Rischpater
This document derives useful formulae for working with the Lie groups that represent transformations in 2D and 3D space. A Lie group is a topological group that is also a smooth manifold, with some ...
开发中,我们常常会遇到很多异常报错,现在就我工作中经常遇到的报错做记录和总结,首先对自己会有很大的帮助,同时希望对读者也起到一定的帮助。废话不多说,先上报错。 1. Ambiguous mapping. Cannot map ‘***’ ...
When mapping from an ER model to a relational model, a strong entity is mapped into a (a) table (b) row (c) column (d) key Correct answer is (a) 10. Which of the following is true about ...
Fixed comparing against disconnected mapped network drives to prompt for username/password and reconnect.
MemoryMappedFile 使用 C# 内存中分配一大块地址. 前边分配一个用与共同配置用 后边N个结构体 for循环建 一般用到此问题,两本程序 一个读一个写 循环读写 ----2015/09/24 Lyndon 上海----
3. There is a point light and a single directional light in the scene. 4. There is one rotating triangle floating in the air, casting a shadow on the table, walls and other objects. 5. The program ...
For a node's Mapped Rx signal, the assigned value table is displayed in the dialog box in addition to the overview. For user-defined attributes with the type "hex" it is now possible to define the min...
The behaviour is changed: the DATETIME2 data type is now used for columns, corresponding to System.DateTime properties, for Microsoft SQL Server 2008 and later The behaviour is changed: the 'Real' ...
Indian Subcontinent, India, and there is a strong promotion by the Asian-Pacific Development Information Programme (APDIP) for the use of FOSS in the countries in South East Asia. On the contrary, ...