现在的位置: 首页 > 综合 > 正文

记Struts 2不再支持在struts tag里使用JSP EL表达式

2013年08月16日 ⁄ 综合 ⁄ 共 841字 ⁄ 字号 评论关闭

当你使用struts 2 tags时,如果使用代码:

<s:set name="name" value="<%= "'" + request.getParameter("name") + "'" %>" />    
或者 <s:set name="name" value="${param.name}" />

都会发生下列错误:

According to TLD or attribute directive in tag file, attribute value does not accept any expressions

而得使用如下,可行:

<s:set name="name" value="#parameters.name[0]" />

 

原因可能是因为你使用了<%%>代码等。struts2从2.0开始已经不支持struts tag和el表达式的混合使用,而只支持ognl。

Trick Tip:

对于<s:property>,是使用

<s:property value="#parameters.name" />

 

而对于<s:set>,则要使用(否则出错):

<s:set name="name" value="#parameters.name[0]" />

 

 但是如果使用struts include tag来传参数,则<s:property>和<s:set>无法获取传来的参数,如:

  <body>
      <s:include   value= "/welcome.jsp">  
            <s:param name="name">Scott</s:param>
      </s:include> 
  </body>

 

那么在webcome.jsp里通过<s:property>和<s:set>无法获取"name"参数:

<s:property value="#parameters.name" />

<s:set name="name" value="#parameters.name[0]" />

而只能够用

<%request.getParameter("name")%>

来获取

抱歉!评论已关闭.