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

最近在网上总是看到许多人出现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>


 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怎么演

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值