struts2异常笔记
异常信息:The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filt
异常信息:The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag.
环境:tomcat6.0
web.xml信息
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml 信息:
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<package name="ygn.action" extends="struts-default">
<action name="HelloWorld" class="ygn.action.HelloWorld">
<result>HelloWorld.jsp</result>
</action>
</package>
</struts>
SayHello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>Say Hello</title>
</head>
<body>
<h3>Say "Hello" to :</h3>
<s:form action="HelloWorld">
Name:<s:textfield name="name"/>
<s:submit/>
</s:form>
</body>
</html>
异常分析:以上的配置及文件中,如果采用 http://ip:port/SayHello.jsp,那么会出现前面所提到的异常。如果采用http://ip:port/SayHello.action 进行访问,那么正常。
原因:如果想要在jsp文件中,采用 struts的tag,那么必须通过web.xml所配置的过滤器访问文件,否则会有异常,即 之前所出现的异常。
解决方案:
方案一:
采用 http://ip:port/SayHello.action 访问
方案二:
将web.xml 的过滤器,从 *.action 修改为: /*
方案三:
修改SayHello.jsp 文件,不使用 struts 的标签。
异常信息: Exception starting filter struts2 Unable to load configuration. - action - file:/F:/javaPro/workspace1/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/myStruts/WEB-INF/classes/struts.xm
环境包:
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar
freemarker-2.3.13.jar
log4j-1.2.14.jar
ognl-2.6.11.jar
struts2-core-2.1.6.jar
xwork-2.1.2.jar
struts.xml配置:
异常重点有提示:struts.xml
解决办法:将package节点添加一个属性:extends="struts-default"。
struts2中的规范中,package节点中必须添加exends属性,要不然80%会以上的异常噢。我自己都范了好几次这样的错误!
异常信息:HTTP Status 404 - There is no Action mapped for namespace / and action name
解决办法:package 元素里的 namespace 去掉.
调用 action 名称的页面应该放在 namespace 的名称里面(文件夹,路径)
<package name="example" namespace="/example" extends="struts-default">
<action name="HelloWorld" class="example.HelloWorld">
<result>/example/HelloWorld.jsp</result>
</action>
HelloWorld.jsp 文件应该放在 namespace="/example" example 文件夹里面. 否则调用 action 会出错.
关于 namespace :
struts.xml 文件中 package 元素下 namespace 属性的作用
namespace的作用是控制相应package下的action的url地址,url地址在web编程中是基础中的基础. 我们的程序不同的功能实际上就是对相应url地址的访问来触发的,这个要牢牢掌握,有点象java的classpath
Struts2 的 struts.xml 中是分 package 配置的,可以为 package 设置 namespace 属性,如
<package namespace="/secure" ....>
......
</package>
如果没有指定 namespace 属性,默认 namespace 是 ""。使用 namespace 可以方便于按不同目的规划对应用的访问规则。比如不同 namespace 下配置了不同的拦截器就可以实现权限的控制,如 "/secure" 下已登陆用户才能访问,"/public" 下可公开访问的。
配置了 namespace 直接就是反应在访问 URL 上,例如 namespace="/secure" name="test" 的 action
<package namespace="/secure" ....>
<action name="test" ....
</package>
访问它的 URL 就是 http://ip:port/context/secure/test.action ,那如果在 namespace "/secure" 下没有 test action 会出现什么情况呢?Struts 还会尝试在默认 namespace,即 "" 下找 test。
再举个例子,URL 是 http://ip:port/context/some/path/test.action 时,如果在 "/some/path" namespace 下找不到 test action,也是到 "" (default namespace) 下找 test action,但不会去 "/some" 下找的。
用标签 <s:url value="/secure/test.action"/> 对应页面源文件是 /context/secure/test.action
稍有麻的就是 <s:form action="/secure/test.action" .... 对应的源文件是 <form action="/context/secure/test.action" ...
但是后台会有警告:
警告: No configuration found for the specified action: '/secure/test.action' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
Struts2 把 action 属性值当整一个 Action Name 了,但这也不影响使用,这个 URL 正好能与 (package namespace) + (action name) 合上拍。
但是对于使用了动态方法调用(struts.enable.DynamicMethodInvocation = true)就没这么幸运了。很容易想当然的 <s:form action="/secure/test!update.action" .... 生成的 HTML 源文件却是 action="/TestStruts2/om/test"
同时后台的警告信息是:
警告: No configuration found for the specified action: '/secure/test' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
很显然对于这个 action="/TestStruts2/om/test",提交时是会得到 HTTP Status 404 - /context/secure/test 错误。
正确的用法是 <s:action...> 也有一个 namespace 属性,对了,就是
<s:form namespace="/secure" action="test!login">
生成的 HTML 源文件是:<form action="/TestStruts2/om/test!login.action" ....>
我们要的就是这个。
如果不配置 namespace 属性,我们能不能在访问 action 时也用上目录层次呢?可以,那是在 struts1 习惯的做法,配置 <action name="secure/test" ....> name 中使用斜杠,但在 Struts2 中 Action Name 中使用斜杠需要设置
struts.enable.SlashesInActionNames=true 默认为 false
可是 Struts2 大概不赞同这种做法,力挺 namespace 的作用。
对于上面使用了斜框的 Action Name,<s:form 中的写法要用
<s:form action="secure/test">
生成 HTML 源文件:<form action="/context/secure/test.action" .....
<s:form action="secure/test!update">
生成 HTML 源文件:<form action="/context/secure/test!login.action" .....
上面的 action 后加不加 .action 无所谓,只是要保证 <s:form> 的 action 属性一定要与 struts.xml 中的 <action> 的 name 匹配上,如果你自作多情的在前面加个斜杠,如写成了
<s:form action="/secure/test!update"> 、 <s:form action="/secure/test"> 或者 <s:form action="/secure/test!update.action"> 生成的 HTML 源文件就都成了:<form action="/context/secure/test" .....
这也是从 Struts1 带来的弊病,因为 Struts1 中 <html:form> action 属性对应的是 <action> 的 path,而 Struts2 中 <s:form> 的 action 属性对应的是 <action> 的 name;name 要完全匹配,path 可以加些层次。
我为何以费这么些功夫来搞清楚这个呢,也就是因为使用 <s:form action=""> 是时而行,时而不行,很似迷惑,一度怀疑是应用服务器在从中作梗。坦白的说,就是写本篇日志完成红线以上的内容时仍未能知晓就理,这也正好映证了写下来的意义。因为在这过程中你在试图让别人更好的理解,倘若自己都说不清,他人只能是云里雾里了。
在此大家一定要好好的检察一下自己发布的项目,如tomcat下的webapp下的项目文件。
注意:
在首次进行struts的开发中,初学者经常会遇到HTTP Status 404 There is no Action mapped for namespace / and action name 的问题,出现上述的问题,大概从以下几个方面进行检查:
(1)首先,检查struts.xml的位置,一般在eclipse或者myeclipse下放在src目录下,在运行后,我们可以看到,我们在src中 所写的类,struts.xml,在web-inf下的classes下都能找到。其中类都已经编译通过变成了.class文件。
(2)其次注意路径的问题,仔细检查是不是在相对路径方面除了问题。
(3)检查拼写,这个问题一定要避免放生,一些小的错误比如说把struts.xml写成了sturts.xml或者写成了struts2.xml,单词 的拼写错误,这些都会导致上述错误。
昇腾计算产业是基于昇腾系列(HUAWEI Ascend)处理器和基础软件构建的全栈 AI计算基础设施、行业应用及服务,https://devpress.csdn.net/organization/setting/general/146749包括昇腾系列处理器、系列硬件、CANN、AI计算框架、应用使能、开发工具链、管理运维工具、行业应用及服务等全产业链
更多推荐


所有评论(0)