`

struts2标签使用需注意的几个地方

    博客分类:
  • J2EE
阅读更多
1.在jsp页面上直接使用struts2的s标签,要求必须经过FileterDispatcher过滤,否则总会报错
org.apache.jasper.JasperException: 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.

解决办法:
<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>*.action</url-pattern><!--过滤主要操作 -->
	</filter-mapping>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>*.jsp</url-pattern>
<!--因为若在jsp页面使用struts标签,就必须经过FilterDispacher的过滤,这样配置便可保证所有的jsp都经过FilterDispatcher了,否则要为每一个jsp写配置一个action地址转入-->
	</filter-mapping>





2.在使用struts标签的过程中,会生成一些table,td标签并且标签自动换行,去除的方法如下:
1.在标签中加theme="simple"
<s:textfield name="userName" theme="simple" ></s:textfield>--普通文本框的使用
2.在struts.xml文件中进行配置:
<constant name="struts.ui.theme" value="simple" />
这便可去除标签并解决自动换行的问题。

3.ognl表达式不能直接访问static方法的问题


今天在页面直接用ognl调用方法:

<s:property value="@com.DateUtil@getTimeArray()"/>来获取时间,但始终进不去方法。

后来把此代码拷到另外一个工程中,就可以正常调用方法了。

查了网上资料,原因如下,有些struts2的配置项里其中有一个名为struts.ognl.allowStaticMethodAccess的配置项默认是false;

解决办法:

在struts.properties中配置:
struts.ognl.allowStaticMethodAccess=true


或者

在struts.xml中配置:
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />


可直接调用方法:
eg: <a
                          href="http://v.t.sina.com.cn/share/share.php?url=http://<s:property value="couponDetailVO.cityGbName"/>.koubei.com/youhuiquan/<s:property value="couponDetailVO.id"/>&title=<s:property value="@java.net.URLEncoder@encode(couponDetailVO.name,'UTF-8')"/>"
                          class="k2-icon-node-m k2-icon-s-sina" target="_blank">分享到新浪微博</a>
问题解决

分享到:
评论
2 楼 u013810758 2014-08-04  
很好 谢谢
1 楼 hbxflihua 2011-10-18  
嗯,很受用!

相关推荐

Global site tag (gtag.js) - Google Analytics