您的位置:首页 > 编程语言 > Java开发

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

2014-09-03 16:32 357 查看


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

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.

解决办法:

Xml代码


<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中配置:

Xml代码


struts.ognl.allowStaticMethodAccess=true

或者

在struts.xml中配置:

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>

问题解决
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: