HTTP4.4错误 There is no Action mapped for namespace [/] and action name [hello] associated with context path [/StrutsDemo].
.opensymphony.xwork2.config.ConfigurationException: There is no Action mapped for namespace [/] and action name [hello] associated with context path [/StrutsDemo].
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:192) ~[struts2-core-2.5.5.jar:2.5.5]
at org.apache.struts2.factory.StrutsActionProxy.prepare(StrutsActionProxy.java:63) ~[struts2-core-2.5.5.jar:2.5.5]
at org.apache.struts2.factory.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37) ~[struts2-core-2.5.5.jar:2.5.5]
at com.opensymphony.xwork2.Default
以上是错误信息,我用的是strtus2.5.5版本的,jar包都导入了,项目启动是没有问题的,只是在跳转到相应的Action时报错,我已经把目录结构以及类的目录都写好了,名称也都核对过了,还是有问题,麻烦大家帮忙看一下!
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>StrutsDemo</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="hello" class="com.imooc.action.HelloAction">
<result name="success">/result.jsp</result> <!-- 默认name="success" -->
</action>
</package>
</struts>
default.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>default.jsp</title>
</head>
<body>
<h1>This is default.jsp!</h1>
<center>${tip}</center>
<form action="hello.action" method="post">
用户名:<input type="text" name="username" />
密码:<input type="text" name="password"/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
HelloAction.class
package com.imooc.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("Hello Action execute!");
return SUCCESS;
}
}
我已经根据下面的提示查错了,但是还是出错。
1.struts.xml文件错误。
这种错误又分为以下几种:
1)struts.xml文件名错误。一定要注意拼写问题;
2)struts.xml文件放置路径错误。一定要将此文件放置在src目录下。编译成功后,要确认是否编译到classes目录中;
3)struts.xml文件内容错误:启动页面一定要是自己工程里有的jsp文件。
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
如果你在Eclipse工程的WebContent文件夹下没有这个index.jsp文件,也会报同样的错误。
比如我自己定义了一个login.jsp文件,放在WebContent文件夹下,就写成
<welcome-file>login.jsp</welcome-file>
把login.jsp文件放在WEB-INF下自定义的jsp文件夹下,就写成
<welcome-file>/WEB-INF/jsp/login.jsp</welcome-file>
2.表单提交页面的错误