刚接触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。请问这是什么原因呢?非常感谢!

解决方案 »

  1.   

    你是说struts.xml吗?我放在了WEB-INF/src下。我不清楚这两个文件应该怎么放?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="tutorial" extends="struts-default">
            <action name="HelloWorld" class="tutorial.HelloWorld">
                <result>HelloWorld.jsp</result>
            </action>
        </package>
    </struts>
      

  2.   

    我奇怪的是,为什么web.xml中有了filter的那段后,我连http://localhost:8080/helloworld/都访问不了了呢?
      

  3.   

    访问地址应该是这样的:http://localhost:8080/helloworld.action
      

  4.   

    访问地址应该是这样的:http://localhost:8080/helloworld.action顶楼上,那个web.xml最好放在最后
      

  5.   


    原因是因为你加了这段代码后,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
      

  6.   

    你是不是struts.xml的位置放错了啊?
    应该在项目里的src下就有struts.xml文件啊
    你加了filter那段后,它会把你的请求过滤给struts处理
    应该是配置的问题,所以才不能访问
      

  7.   

    <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      </welcome-file-list>
    这个里面应该是index.jsp
      

  8.   

    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">DTD有顺序的,你换用schema或者,按照DTD的顺序,把<welcome-file-list>
      <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加载失败。
      

  9.   

    建议,如果服务器不是太旧的话,使用2.4/2.5版的web.xml,并且建议使用schema而不是dtd,那个不限制先后顺序
      

  10.   

    <url-pattern>/*</url-pattern> 表示你过滤全部文件,没有定路action都不能访问