`
jzinfo
  • 浏览: 116506 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

彻底解决 警告: No configuration found for the specified action

阅读更多

最近学习struts2 中时候控制台老是出现该警告错误。

我的客户端代码如下 (代码1):

 

 

<s:form action="/admin/login" method="post">
 <s:textfield name="username" id="usenrame" label="用户名" />
 <s:password name="password" id="password" label="密码" /> 
 <s:submit type="input" value="登录" id="btnsubmit" cssClass="btnsubmit"></s:submit> 
</s:form>

 

 

struts.xml中的配置代码如下 (代码2):

<package name="admin" namespace="/admin" extends="struts-default">
 <action name="login" class="com.longweir.struts2.action.LoginAction"> 
   <result name="success">/actionResult.jsp</result> 
   <result name="login" type="redirect">/admin/login.jsp</result>    
   <result name="input">/admin/login.jsp</result>
 </action>
 </package>

 

 

在网上搜索了下,搜索网络给出的答案几乎都说把<s:form  action="/admin/login method="post"  >中的login后加上.action后缀了,做了测试,问题仍然没有解决。

 

通过我观察发现有两个地方需要修改,首先<s: form>表单中需要加入命名空间参数namespace,和struts.xml中配置一致。其次,<s:form action=xxx> 中直接写action映射的名字,比如我的xml中是login,则这里直接写login即可,不需要画蛇添足加上.action后缀或者其他的前置路路径,所以修改后的客户端文件配置为 代码(3):

 

<s:form action="login" method="post" namespace="/admin"> 
<s:textfield name="username" id="usenrame" label="用户名" /> 
<s:password name="password" id="password" label="密码" /> <s:submit type="input" value="登录" id="btnsubmit" 
cssClass="btnsubmit">
</s:submit> 
</s:form>

 

 
当然,如果你不是使用的struts2的标签,而是使用的传统的html代码,则表单的action属性必须加上完整的路径和后缀

也就是说,必须改成如下代码:

<form name="form" method="post" action="/admin/login.action">

 

你如上指定后,当浏览器给出如上的请求/admin/login.action ,struts2框架将首先在sturts.xml中查找 /admin 名称空间,如果找到了(比如我这里显然有这个名称空间),则执行login.action,如果没有找到,会到默认的名称空间中查找,如果默认的也没有,则提示错误,没有映射该action.

 

分析下我出现的错误,在我的原始的代码 (代码1)中因为没有指定namespace,那么框架认为输出表单的请求和提交表单的请求都是在一个名称空间中,即使在/admin名称空间中,此事虽然可以找到,可以运行,但是会提示这个警告.

 

 

如果在你的struts.xml文件中指定了名称空间,那么,如果在客户端使用struts2的标签,则要同样指定名称空间,但action= 这后面,直接写action名即可,不需要加上名称空间的前缀在前面。

因为,struts2框架会根据你所指定的名称空间以及你系统当前应用所映射的路径,构造一个完整的请求路径。比如如上正确的 代码3的 struts2代码,在浏览器中查看源码,解析的结果是:

 

<form id="login" name="/admin" action="/guestbook/admin/login.action" method="post"> 
<table class="wwFormTable">
 <tr> 
    <td class="tdLabel">
    <label for="usenrame" class="label">用户名:</label> </td> 
    <td><input type="text" name="username" value="" id="usenrame"/></td> 
   </tr>
   <tr>
    <td class="tdLabel"><label for="password" class="label">密码:</label></td> 
   <td><input type="password" name="password" id="password"/></td>
 </tr> 
  <tr> <td colspan="2"><div align="right"><input type="submit" id="btnsubmit" value="&#30331;&#24405;" class="btnsubmit"/> </div></td> 
   </tr> 
</table>
</form> 

 

 

可以看到,输出的action 请求地址非常正确,自动加入了当前应用的名称和根据namespace所整理成的完整的请求路径:"/guestbook/admin/login.action

 

但是,假如我们人为的在action后面加上 action="/admin/login" ,虽然你指定了namespace="/admin" ,那struts2框架不会给你加上.action的后缀(可以查看输出的html源码中是没有.action这个后缀的),最后还是会出现标题中所提示的警告,虽然还是可以正常工作.

 

所以,我个人总结为如下:

1. 如果你的xml配置文件中设置了namespace ,建议你的终端的struts2表单标签中也设置一样的namespace

2. 如果已经按照1中的设置,那么 action = 后面直接写action名字即可,不需要加后缀(默认就会加上,xml中也一样)

3. 以上仅是针对使用struts2标签的情况下,如果你没用struts2 的标签而是使用的原始HTML标签,则action=后面必须是完整的路径地址: /应用名/名称空间/xxx.action  (action 后缀要有),因为很显然,struts2不会给你自动加上那些信息。

 

 

分享到:
评论
2 楼 cwalet 2010-11-20  
显然这个错误是在使用struts标签时产生的,
原因是没有在struts.xml中配置action或在form标签中使用了匿名的namespace所致
1 楼 wen.owen 2010-03-04  
有点没有看明白,请教一下楼主。

“那么框架认为输出表单的请求和提交表单的请求都是在一个名称空间中,即使在/admin名称空间中,此事虽然可以找到,可以运行,但是会提示这个警告.”

有两个问题:
1. 输出表单的请求和提交表单的请求分别是什么意思?
2. 框架是怎么找到对应的Action的?

相关推荐

    Java邮件开发Fundamentals of the JavaMail API

    Fundamentals of the JavaMail API Presented by developerWorks, your source for great tutorials ibm.com/developerWorks Table of Contents If you're viewing this document online, you can click ...

    BURNINTEST--硬件检测工具

    - Minor changes to the No operation error watchdog timer for the CD and Hard disk tests. - Minor correction to the Butterfly seek test. - Video playback trace logging increased. Release 5.3 build ...

    OutlookAttachView v2.73

    o Added option to scan only emails with the specified 'From' and 'To' strings. * Version 1.82 o When adding a folder to scan (with the Add button), the folder path in now delimited with quotes, ...

    LCTF软件备份VariSpec™ Liquid Crystal Tunable Filters

    For this reason, the functions that handle these actions offer the option of waiting until the action is complete before returning (so-called synchronous operation); although they can be called in an ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    is now available for writing (during the writing, the standard grid object TFlexGrid is released, and a user's one is set). - ADD: In the class TFlexGrid added the properties HOffset and VOffset - ...

    Turbo C 2.00[DISK]

    When /P is specified, the EGA palette will be restored each time the screen is swapped. In general, you don't need to use this switch unless your program modifies the EGA palette registers, or ...

    Turbo C 2.01[DISK]

    When /P is specified, the EGA palette will be restored each time the screen is swapped. In general, you don't need to use this switch unless your program modifies the EGA palette registers, or ...

    php.ini-development

    Please see the actual settings later in the document for more details as to why ; we recommend these changes in PHP's behavior. ; display_errors ; Default Value: On ; Development Value: On ; ...

    CE中文版-启点CE过NP中文.exe

    Fixed the found code dialog count size Fixed mono freezing Cheat Engine when it crashes/freezes Additions and changes: Changed the processlist and added an Applications view similar to the ...

    DebuggingWithGDB 6.8-2008

    4.1 Compiling for Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2 Starting your Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

Global site tag (gtag.js) - Google Analytics