在学习 【黑马程序员】Struts2框架教程(完整版+源码资料)视频教程时,视频中使用的是eclipse,而我使用的是idea,开发工具存在差异,难免会有问题。
就是在一开始的时候,第一个action都跑不通。
工程目录如下图

动作类:HelloAction.java
package com.itheima.web.action;
public class HelloAction {
public String syaHello() {
return "success";
}
}
struts配置文件:struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="hello" class="com.itheima.web.action.HelloAction" method="syaHello">
<result name="success">success.jsp</result>
</action>
</package>
</struts>
web应用基础配置:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="2.5">
<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>
</web-app>
web应用资源:index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>点我</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/hello.action">点我</a>
</body>
</html>
web应用资源:success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>success页面</title>
</head>
<body>
success
</body>
</html>
特别是,在创建Tomcat服务器后,配置Deployment这个页时,一定要增加Artifacts,见下图。

我当时就是错误的选择了External Source,然后选择web目录,

以至于配置的action都不起作用,都是这样的报错:

HTTP Status 404 - There is no Action mapped for namespace [/] and action name [hello] associated with context path [/web].
type Status report
message There is no Action mapped for namespace [/] and action name [hello] associated with context path [/web].
description The requested resource is not available.
Apache Tomcat/6.0.53
正确应该是选择

访问页面如下

因为是自学,也没有人指导,摸索了好久才发现问题之所在。
在IDEA中创建Struts2项目时遇到HTTP 404错误,问题出在Artifacts设置上。错误选择External Source导致Action未映射。正确做法应选择项目模块,避免资源找不到。调整后,Action成功运行,页面正常显示。
2507

被折叠的 条评论
为什么被折叠?



