Struts2:根目录后面的文档中的标记必须格式正确

时间:2012-12-07 15:41:57

标签: xml struts2 filter markup well-formed

我刚刚发现了Struts2,我对web.xml文件有疑问。

它在滤镜标记附近给出了错误:

<filter>
   <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

错误说明:在此行找到多个注释: - 开始元素<filter>的标记 - 根元素后面的文档中的标记必须是 - 形成。

有什么问题?我该如何解决?

谢谢!

感谢Aleksander的兴趣:

这是我的web.xml文件:

    `<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <display-name>struts2example</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

<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>`

1 个答案:

答案 0 :(得分:2)

您将标记放在<web-app>根标记之外。将<filter>置于<web-app>

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <display-name>struts2example</display-name>
  <welcome-file-list>
   <welcome-file>index.html</welcome-file>
   <welcome-file>index.htm</welcome-file>
   <welcome-file>index.jsp</welcome-file>
   <welcome-file>default.html</welcome-file>
   <welcome-file>default.htm</welcome-file>
   <welcome-file>default.jsp</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>

自Struts 2.1.3起,org.apache.struts2.dispatcher.FilterDispatcher也被弃用,而是使用org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter