使用Struts2遇到There is no action mapped for namespaces / and action name的问题

在最初配置struts中会遇到There is no Action mapped for namespace / and action name类似的问题,很多情况是我们粗心大意导致的,以下为总结的解决方法:

1、struts.xml文件配置错误,这是其中一个很大的原因

 DTD的问题在此不再赘述,网上相关的帖子很多,在这里出现的问题主要是适用版本2.0的问题,注意区分2.0和2.1,建议都用2.0
 其次就是编码格式的问题,如果遇到中文乱码,记得加

<span style="font-size:18px;"><constant name="struts.i18n.encoding" value="GBK"></constant></span>

大多数问题处在package中,

extends的作用主要用于继承struts-default或者其它的package,根据自己的情况更改,建议不要漏掉;

namespace不要拼写错误,因为在项目中会分单独每个模块,建议在平时的练习中最好在namespace中写成/模块名,好区分,若平时练习可以写/,在访问的时候直接写

<%=request.getContextPath()%>/index.action
在package中还有比较容易忽视的地方是action中<result>/r1.jsp</result> 会比较容易漏掉“/”,在访问action中提示The requested resource is not available. 多半是因为漏加/仔细一点
<span style="font-size:18px;"><?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>
     <constant name="struts.i18n.encoding" value="GBK"></constant>
    <package name="actions" namespace="/actions" extends="struts-default">
        <action name="index">
            <result>/r1.jsp</result>
        </action>
    </package>
</struts></span>

2、index.jsp中容易出现的问题

<span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%
    String context=request.getContextPath();
    System.out.println(context);
%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
    <a href="<%=context%>/actions/index.action">点击</a>
</body>
</html></span>

这里出现的问题主要是在jsp的存放位置,注意区分Eclipse和MyEclipse,我是用的是EclipseEE,jsp的存放位置WebContent,与WEB-INF在同一级,这也是访问不到的重要原因之一

建议在访问的时候最好加上.action

3、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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Struts</display-name>
   <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>index.jsp</welcome-file>
  </welcome-file-list>
  
</web-app>

这里最好是复制以上filter的,不要更改内容,问题可能会出现在<welcome-file-list>,因为最初生成的web.xml文件中有一个index.html和index.htm,根据自己的情况进行选择

以上是经验之谈,希望能帮助到大家~~




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

DYanchao2015

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

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

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

打赏作者

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

抵扣说明:

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

余额充值