在最初配置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,根据自己的情况进行选择
以上是经验之谈,希望能帮助到大家~~