今天遇到一个问题,到现在也没能比较圆满的解决,是不是Struts在标签库上还不够完善呢。比如有一个界面是显示课件列表的,在最后一栏里可以对数据进行操作,如下所示:
Code | Name | Author | OP |
---|---|---|---|
10000001 | 风洞模型课件 | 刘金东 | View Edit Delete |
10000002 | 卡门涡阶课件 | 季铭义 | View Edit Delete |
10000003 | 复变函数课件 | 秦江 | View Edit Delete |
10000004 | 听力课件 | 郭长凯 | View Edit Delete |
现在希望当用户按删除时先弹出个确认框,提示“是否确认删除风洞模型课件”,用户可以选择确认或取消。其中“是否确认删除”是在资源文件里定义的(prompt.confirm.delete=是否确认删除{0}),“风洞模型课件”是课件的名称,课件bean名为"ware"。如果写成HTML,就是:
<
a href
=
"
"
onclick
=
"
return confirm('是否确认删除风洞模型课件')
"
>
Delete
</
a
>
但因为信息都是动态的,所以就有问题了。因为在<html:link>的onclick="..."里,"<%"必须紧跟在第一个单引号后才能正确解析,即不能写为onclick="return confirm('<%=str%>')"。所以,现在有两种方法实现所需要的功能:
1、不用<html:link>,直接用HTML的<a>标记:
<
bean:define id
=
"
toDel
"
name
=
"
ware
"
property
=
"
name
"
type
=
"
String
"
/>
< a href = " delete.do?code=<bean:write name= " ware " property= " code " /> "
onclick = " return confirm('<bean:message key= " prompt.confirm. delete " bundle= " root " arg0= " <%= toDel %> " />'); " >
< bean:message key = " course.list.op.delete " />
</ a >
< a href = " delete.do?code=<bean:write name= " ware " property= " code " /> "
onclick = " return confirm('<bean:message key= " prompt.confirm. delete " bundle= " root " arg0= " <%= toDel %> " />'); " >
< bean:message key = " course.list.op.delete " />
</ a >
2、使用<html:link>,事先定义一个只含一个参数的script函数,代码如下:
<
script language
=
"
JavaScript
"
>
<!--
function confirmDelete(str){
return confirm(' < bean:message key = " prompt.confirm.delete " bundle = " root " arg0 = " '+str+' " /> ');
}
-->
</ script >
<!--
function confirmDelete(str){
return confirm(' < bean:message key = " prompt.confirm.delete " bundle = " root " arg0 = " '+str+' " /> ');
}
-->
</ script >
然后在删除链接的地方这样写:
<
bean:define id
=
"
toDel
"
name
=
"
ware
"
property
=
"
name
"
type
=
"
String
"
/>
< html:link action = " /delete " paramId = " code " paramName = " ware " paramProperty = " code " onclick = " <%= " return confirmDelete( ' "+toDel+" ' ); " %> " >
< bean:message key = " course.list.op.delete " />
</ html:link >
< html:link action = " /delete " paramId = " code " paramName = " ware " paramProperty = " code " onclick = " <%= " return confirmDelete( ' "+toDel+" ' ); " %> " >
< bean:message key = " course.list.op.delete " />
</ html:link >
这两种方法都能达到目的,我暂时使用了第2种用法,毕竟在struts程序的jsp里直接使用<a>标记有点别扭。我看了一下struts文档,能把资源中的{0}转换为实际内容的标签好象只有<bean:message>这一个,其实如果有办法让<bean:message>得到的内容放进某个bean里就很好办了,可惜……。
另外,没研究过EL标签库,不知道会不会有帮助。
本文转自博客园八进制的博客,原文链接:[Struts]在jsp里处理比较复杂的内容?,如需转载请自行联系原博主。