There is no Action mapped for namespace / and action name login等Strut2错误总结!! (2011-11-04 14:25:07)

There is no Action mapped for namespace / and action name login等Strut2错误总结!!

  (2011-11-04 14:25:07)
标签: 

it

分类: SSH
There is no Action mapped fornamespace / and action namelogin
这个错误找的哥,好累!
总算揪出来是哪里捣的鬼了。
 
主要原因:
1.配置文件名为struts.xml不是,my eclipse自动生成的,struts-config。
2.这个配置文件还放错了位置。放在类路径下,不长眼的我,居然放错了!
 
下面是一些转载的参考文件:
首先说明一下示例的文档结构:
        strut2                   //工程名称
           |-----src                                     
              |-----cn.balance      //包名称 :随便换
|----LoginAction.java    //act ion类
  |----struts.xml       //strut2.0的配置文件:放在src 文件夹下,最终会被程序复制到WEB-INF的       //classes 文件夹下,
        ///这个地方省略系统自动生成的项目
        |------WebRoot
|------META-INF
|------WEB-INF
|----lib
|----web.xml
|-----error.html
|-----index.jsp
|-----success.html

+下面是代码:

LoginAction.java:
/
imp ort com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

private String username ;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute () throws Exception
{
if(getUsername().equals("root")&&this.getPassword().equals("root")){
return "success";
}
else
{
return   "error";
}
}
}
//

struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name ="strut2" extends ="struts-default"   namespace="/strut" >
<act ion name ="login" class="cn.balance.LoginAction">
  <result name ="success" >/success.html</result>
<result name ="error">/error.html</result>
</act ion>
</package>
</struts>     


///
web.xml ---->本例为Myeclipse8.0GA创建,与手动创建的<filter-class>有所不同:
             此为另一种方式:   org.apache.struts2.dispatcher.FilterDispatcher
请视具体strut2的版本,情况而定(两个都一样)
/
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <welcome-file-list>
       <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <filter>
     <filter-name>struts2</filter-name>
     <filter-class>
     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteF ilter
     </filter-class>
    </filter>
    <filter-mapping>
     <filter-name>struts2</filter-name>
     <url-pattern>*.act ion</url-pattern>
    </filter-mapping></web-app>


error.html
//
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
       <title>error.html</title>
       <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
       <meta http-equiv="description" content="this is my page">
       <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      
       <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

    </head>
   
    <body>
       Name or Password error!But   Your demo still sucess!!! Congratulations!
    </body>
</html>


//
success.html
///

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
       <title>success.html</title>
       <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
       <meta http-equiv="description" content="this is my page">
       <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      
       <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

    </head>
   
    <body>
       success!Congratulations!
    </body>
</html>

//
index.jsp
<%@ page language="java" imp ort="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
out.print(basePath);
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
       <base href="<%=basePath%>">
      
       <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">     
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
    </head>
   
    <body>
       <form   act ion="strut/login.act ion" >  
        Name:<input type="text" name ="username"><br>
       Password: <input type="password" name="password"><br>
       <input type="submit"value ="Submit">     
        
       </form>
      
    </body>
</html>

↑ 好了以上就是程序的全部代码 ↑ 下面说一下常见得错误:
错误:
There is no Action mapped for namespace / and actionname login

首先出现这种错误是因为配置文件没找到,检查一下你的struts.xml是否在classes 文件夹下
路径:“安装Tomcat 的路径”\webapps\“你工程的名称”\WEB-INF\classes

其次,查看你的struts.xml这个文件名称是否与“struts.xml”一样,并且检查其中内容是否正确,每一个属性都要检查
注意拼写错误!!!!!!

再次,如果没用<s:form>的话形式应该是这样:<form   act ion="strut/login.act ion" >  
如果你用的是<s:form >形式那应该是:<s:form name ="form1" act ion ="login" namespace="/strut">

↓↓↓要记住↓↓↓
form 标签   没有namespace属性
s:form 标签   有namespace属性
………………………………↑曾经因为这个蛋疼两天
运行起来 成功就出现 :
success!Congratulations!!
用户名或密码错误出现:(本例用户名密码都是root)
Name or Password error!But   Your demo still sucess!!! Congratulations!

好了,看看你的程序运行起来没::::::

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值