刚接触struts,在eclipse中新建了一个tomcat project,建了一个首页index.html。在WEB-INF下新建web.xml,内容如下:<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"><web-app>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<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>/*</url-pattern>
</filter-mapping>
</web-app>但是这样子我访问http://localhost:8080/helloworld/,都会报404错误: The requested resource (/helloworld/) is not available.如果去掉filter那一段则可以看到index.html。请问这是什么原因呢?非常感谢!
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<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>/*</url-pattern>
</filter-mapping>
</web-app>但是这样子我访问http://localhost:8080/helloworld/,都会报404错误: The requested resource (/helloworld/) is not available.如果去掉filter那一段则可以看到index.html。请问这是什么原因呢?非常感谢!
<!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="tutorial" extends="struts-default">
<action name="HelloWorld" class="tutorial.HelloWorld">
<result>HelloWorld.jsp</result>
</action>
</package>
</struts>
原因是因为你加了这段代码后,struts会自动帮你把所有页面都过滤掉了
<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>/*</url-pattern>
</filter-mapping>具体你可以去看下这个网站:http://apps.hi.baidu.com/share/detail/16248885
应该在项目里的src下就有struts.xml文件啊
你加了filter那段后,它会把你的请求过滤给struts处理
应该是配置的问题,所以才不能访问
<welcome-file>index.html</welcome-file>
</welcome-file-list>
这个里面应该是index.jsp
<welcome-file>index.html</welcome-file>
</welcome-file-list>
放到后面去。DTD简单解释,打开或下载上面红色的DTD文件
找到
<!ELEMENT web-app (icon?, display-name?, description?, distributable?,
context-param*, filter*, filter-mapping*, listener*, servlet*,
servlet-mapping*, session-config?, mime-mapping*, welcome-file-list?,
error-page*, taglib*, resource-env-ref*, resource-ref*, security-constraint*,
login-config?, security-role*, env-entry*, ejb-ref*, ejb-local-ref*)>他的含义是,<web-app>这个元素下面可以有<icon><display-name><ejb-local-ref>这样的子元素,并且这些子元素的顺序,必须是按照它列出的顺序。后面的星号,问号的含义(其他很多地方,比如正则也是)不带符号,表示1个,且必须有
+,1到无穷
?, 0到1个
*, 0到无穷你上面welcome-file-list跑到了filter, filter-mapping前面,违反了DTD,所以web.xml加载失败。