日期:2014-05-20  浏览次数:20641 次

struts2-location.href使用404问题
使用struts2在location.href这使用出现404问题。
HTTP Status 404 - There is no Action mapped for namespace [/] and action name [login!add] associated with context path [/Struts2].

这是jsp页面代码:
  <body>
     <form action="login.action">
      <input type="text" name="username" ><br>
      <input type="password" name="password" ><br>
      <input type="submit" value="提交" ><br>
      <input type="button" value="查询" onclick="location.href='login!query.action'" >
<input type="button" value="添加" onclick="location.href='login!add.action'" >
<input type="button" value="删除" onclick="location.href='login!delete.action'" >
<input type="button" value="修改" onclick="location.href='login!update.action'" >
     </form>
  </body>
struts.xml配置文件代码:
<struts>
<package name="default"  extends="struts-default">
<action name="login"  class="Test.LoginAction" method="execute">
<result name="success">pages/success.jsp</result>
<result name="add">pages/add.jsp</result>
<result name="update">pages/update.jsp</result>
<result name="delete">pages/delete.jsp</result>
<result name="query">pages/query.jsp</result>
</action> 
提交按钮能够通过,但是增删改查这四个就不行,为什么?请教各位大神!!
------解决方案--------------------
应该是你的struts配置文件的问题
------解决方案--------------------
There is no Action mapped for namespace [/] and action name [login!add] associated with context path [/Struts2].

配置问题,你访问的url是什么,项目名称是什么
------解决方案--------------------
配置文件struts.xml检查检查,
------解决方案--------------------
你用的根据不同参数名执行不同方法,不知道你写对没
------解决方案--------------------
英文的意思在ActionMap中 找不到/login!add,也就是说你没有给这个入口,你说你的提交<input type="submit" value="提交" ><br>可以执行是因为提交对应的路径是<form action="login.action">所以他找到的就是名称为login的action,这个是存在的所以可以执行。而你的其他的方法指定了不仅是action名称而且指定了这个action对应的执行方法,所以自然找不到了。
解决方法:<action name="login"  class="Test.LoginAction" method="execute">把这个配置五个分别对应你的五个按钮。
楼上说的去掉method的方法我觉得不可行,有两个疑问一个是如果去掉有可能还是找不到,如果能找到那么都会走execute方法那么在具体的execute方法中需要判断是哪个按钮的请求(这个难度应该很大)。如果你有什么好的解决方案也可以回复我!
------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:

 

Quote: 引用:

应该是你的struts配置文件的问题


引用:
There is no Action mapped for namespace [/] and action name [login!add] associated with context path [/Struts2].

配置问题,你访问的url是什么,项目名称是什么


引用:
配置文件struts.xml检查检查,

struts配置文件就是上面贴出来的那些啊 ,就是那个使用onclick="location.href='login!query.action'" >的出错,那个提交按钮的能够通过。。。项目名称就是struts2

HTTP Status 404 - There is no Action mapped for namespace [/] and action name [login!add] associated with context path [/Struts2].
你用的是DMI动态方法调用,在名为login的Action访问add()方法。
可是你在struts.xml定义的Action方法名为execute:
<action name="login"  class="Test.LoginAction" method="execute">
这就不对了,你要么把execute去掉,要么把method改成add。
你的result的name只是Action中return的出口,而不是你的入口,好好检查一下。


把execute去掉这样是不行的,默认的方法也是execute()啊,不写的效果是一样的。还有把method改成add的话四个增删改查这四个button还是没有实现。 不过谢谢你的建议。

其实以前只是在书上看过DMI,一直没有实践过,今天就利用你这个问题的机会写了一个小demo测试了一下:
1.jsp代码和你的一样:

  <body>
  <span style="font-size: 100px;color: red;font-weight: bold;">Struts2 DMI Test</span><br/>
  <form action="login.action">
      <input type="text" name="username" ><br>
      <input type="password" name="password" ><br>
      <input type="submit" value="提交" ><br>
      <input type="button" value="查询" onclick="location.href='testone!query.action'" >
<input type="button" value="添加" onclick="location.href='testone!add.action'" >
<input type="button" value="删除" onclick="location.href='testone!delete.action'" >
<input type="button" value="修改" onclick="location.href='testone!update.action'" >
  </form>
  </body>


2.struts.xml代码:

<package name="my01" namespace="/" extends="struts-default">