无法从Struts 2标记内部访问scriptlet变量

时间:2013-11-26 10:36:52

标签: java jsp java-ee struts2 scriptlet

我正在尝试访问JSP中struts2 s:if标记内的scriptlet变量

<%
Website website = (Website)request.getAttribute(Website.NAME);
out.println(website.isUkSite());  // prints true
 %>

<s:if test="#website.isIrishSite()">  // this is returning false 
....

实际上这个s:if应评估为true。有什么问题?

2 个答案:

答案 0 :(得分:1)

问题是动作上下文中没有名为website的变量。为了使它工作,你可以使用

<%
Website website = (Website)request.getAttribute(Website.NAME);
out.println(website.isUkSite());  // prints true

ActionContext.getContext().put("website", website); 
 %>

但scriplets是一个糟糕的选择,您可以删除此代码并使用

<s:if test="#request.website.isIrishSite()">

答案 1 :(得分:0)

这终于工作了

<%
Website website = (Website)request.getAttribute(Website.NAME);
out.println(website.isUkSite());  // prints true
 %>

<s:if test="website.ukSite">    
  // this is executed finally !!
</s:if>