您的位置:首页 > 移动开发

关于struts2总是报There is no Action mapped for namespace / and action name login错误的一点经验

2012-05-23 10:15 651 查看
最近在网上总是看到许多人出现There is no Action mapped for namespace / and action name login错误!

我也遇到过n次了,现在总结一下经验:

首先检查xml文档

1、检查struts.xml是否在src目录或者src的子目录下。

2、检查struts.xml的语法是否正确,不要抱struts写错了。

3、看struts.xml中的result标签的name属性值是否和java类返回值一致。

示例:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<package name="struts2" namespace="/" extends="struts-default">
<action name="login" class="cn.itcast.test.LoginAction" method="execute">
<result name="success">/welcome.jsp</result>
<result name="login">/login.jsp</result>
</action>
</package>

</struts>



其次,检查.java文件的返回值是否和xml中的name属性值一致

再看jsp页面表单的action

1、使用<s:form action="/login" method="post">

2、使用<form action="struts/login" method="post">

3、<S:form>有namesp属性而 <form>没有

最后只要你的包都导入正确并且web.xml配置正确了 应该就没有问题了的

LoginAction.java

package cn.itcast.test;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {// 该类继承了ActionSupport类。这样就可以直接使用SUCCESS,
// LOGIN等变量和重写execute等方法

private static final long serialVersionUID = 1L;
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;
}

@Override
public String execute() throws Exception {
if ("haha".equals(username) && "hehe".equals(password))// 如果登录的用户名=haha并且密码=hehe,就返回SUCCESS;否则,返回LOGIN
{
return "success";
} else {
return "fail";
}
}
}


login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>Login</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">

</head>

<body>

<s:form action="/login"  method="post">
<s:label value="系统登陆"></s:label>
<s:textfield name="username" label="账号" />
<s:password name="password" label="密码" />
<s:submit value="登录" />
</s:form>

</body>
</html>


welcom.jsp:

<body>
欢迎${username }!
</body>


web.xml:

<?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"> 
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐