idea创建struts2项目踩坑之artifacts设置

在IDEA中创建Struts2项目时遇到HTTP 404错误,问题出在Artifacts设置上。错误选择External Source导致Action未映射。正确做法应选择项目模块,避免资源找不到。调整后,Action成功运行,页面正常显示。

在学习 【黑马程序员】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

 



 

正确应该是选择

访问页面如下

因为是自学,也没有人指导,摸索了好久才发现问题之所在。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值