struts2标签在jsp不显示问题(不经过action跳转)

今天终于发现这个问题是怎么回事了
首先,看看myeclipse自动生成的web.xml

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>

注意到<url-pattern>*.action</url-pattern> 就是这句,它跟这个报错有关

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. - [unknown location]

没有关联filter
因此,解决方法是把<url-pattern>*.action</url-pattern> 改成<url-pattern>/*</url-pattern>
或者添加

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.jsp</url-pattern>
  </filter-mapping>

修改后的代码如下

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.jsp</url-pattern>
  </filter-mapping>

这样就可以不用经过action,也能在jsp上使用struts2的标签,当然,这句是一定要的

<%@ taglib prefix="s" uri="/struts-tags" %>

顺带提一下,我之前的遇到的struts2标签不显示的解决方法

  1. 经过action就显示了
  2. 后台的类写错了

你可能感兴趣的:(标签,bug,struts2.0)