struts2标签使用注意事项
本文主要为项目过程中碰到Struts2的s标签不能使用的问题的一个记录,先将产生的原因记录如下:
因过滤器配置不当引起s标签不能使用
在jsp页面中加入了<%@ taglib uri="/struts-tags" prefix="s" %>后使用s标签报错,错误如下:
org.apache.jasper.JasperException: An exception occurred processing JSP page /jsp/stores/AddStoresIn.jsp at line 95
92: </head>
93:
94: <body>
95: <s:text name="gtest">asfdasfdsf</s:text>
96: <table width="100%" border="0" cellspacing="0"
97: cellpadding="0">
98: <tr>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
root cause
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]
org.apache.struts2.views.jsp.TagUtils.getStack(TagUtils.java:60)
org.apache.struts2.views.jsp.StrutsBodyTagSupport.getStack(StrutsBodyTagSupport.java:44)
org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:48)
org.apache.jsp.jsp.stores.AddStoresIn_jsp._jspx_meth_s_005ftext_005f0(AddStoresIn_jsp.java:462)
org.apache.jsp.jsp.stores.AddStoresIn_jsp._jspService(AddStoresIn_jsp.java:152)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
参考文章:http://www.huomo.cn/developer/article-bd15.html
原因:因为web.xml中的
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
拦截的是*.action的请求行为,如果不经过action就无法调用struts2的标签包,使得s标签无法使用
解决办法:把<url-pattern>*.action</url-pattern>改为<url-pattern>/*</url-pattern>或者通过action访问带s标签的jsp页面
另一个注意事项,如果使用的是servlet2.3以下的规范须在web.xml加入taglib申明。。。。。 自己写的tld怎么引用,一直报错
我的异常网推荐解决方案:An exception occurred processing JSP page,http://www.myexception.cn/j2se/33144.html