码迷,mamicode.com
首页 > 其他好文 > 详细

练习Struts2遇到的问题

时间:2016-12-27 07:50:41      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:配置   classpath   pack   mod   val   技术分享   namespace   资料   oct   

今天在练习Struts2时,使用分模块来加载xml文件时遇到了一个问题,调试了一整天,问题是这样的:

在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>
<include file="/struts/struts-common.xml"></include>
<include file="/struts/struts-item.xml"></include>
</struts>

下图是两个文件的目录结构

技术分享

在struts-item.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="item" extends="common" namespace="/item">
<action name="*" class="cn.itcast.action.ItemAction" method="{1}">
<result name="success" type="dispatcher">/jsp/{1}.jsp</result>
</action>
</package>
</struts>

在struts-common.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>
<!-- 配置常量 -->
<!-- 开发模式 -->
<constant name="struts.devMode" value="true"></constant>
<constant name="struts.configuration.xml.reload" value="true"></constant>
<constant name="struts.i18n.reload" value="true"></constant>
<!-- 主题 -->
<constant name="struts.ui.theme" value="simple"></constant>
<!-- 扩展名 -->
<constant name="struts.action.extension" value="action"></constant>
<package name="common" extends="struts-default">
<!-- 公用的action、result等配置 -->
</package>
</struts>

当在浏览器中输入地址“http://localhost:8080/temp/item/queryitem”进行访问时总是报出

HTTP Status 404 - /temp/item/queryitem

上网查阅资料都说是路径有include标签里的file路径有问题,于是乎我不断变换路径如下:

<include file="/config/struts/struts-common.xml"></include>
<include file="/config/struts/struts-item.xml"></include>

<include file="config/struts/struts-common.xml"></include>
<include file="config/struts/struts-item.xml"></include>

进而报出:

HTTP Status 404 - There is no Action mapped for action name queryitem

令我很是神伤啊,最后我发现其实我原来的file路径并没有错,错误是在于上面红色部分冲突,原因是拦截器只能拦截.action的请求而我的访问路径没有加.action后缀导致了此问题。改正之后问题解决。

 

注:图中config目录是源文件夹;源文件夹的作用是使得编译器在编译时将其下的java文件编译成class文件,资源文件原样(目录不变)拷贝到类路径(classpath),在我们用eclipse创建javaweb项目的时候注意指定class文件的输出路径,默认为build\classes,但是我们一般都将其改变为WebRoot\WEB-INF\classes目录。

练习Struts2遇到的问题

标签:配置   classpath   pack   mod   val   技术分享   namespace   资料   oct   

原文地址:http://www.cnblogs.com/itzfz/p/6224507.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!