Struts2 Struts OGNL if语句与action类';s变量不';不行,
问题是: 在action类中,我有一个变量:Struts2 Struts OGNL if语句与action类';s变量不';不行,,struts2,ognl,Struts2,Ognl,问题是: 在action类中,我有一个变量: private String commentAdd = "yes"; action类转到reslut.jsp,在reslut.jsp中我有: <s:set name="allowAddComment" value="commentAdd"/> <s:if test="%{#allowAddComment=='yes'}"> <script type="text/javascri
private String commentAdd = "yes";
action类转到reslut.jsp,在reslut.jsp中我有:
<s:set name="allowAddComment" value="commentAdd"/>
<s:if test="%{#allowAddComment=='yes'}">
<script type="text/javascript">
window.close();
</script>
</s:if>
window.close();
但是它不起作用,一些专家能给我一些建议吗?谢谢。几件事
- 属性需要通过公共getter公开(或者在S2的更高版本中作为公共成员公开,但最好使用getter)
- 为什么使用字符串作为布尔值?只需使用布尔值
- 为什么要将属性设置为其他变量?只要使用该属性
import com.opensymphony.xwork2.ActionSupport;
public class PageAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private boolean addComment;
public boolean isAddComment() {
return addComment;
}
public void setAddComment(boolean addComment) {
this.addComment = addComment;
}
public String execute() {
return SUCCESS;
}
}
window.close();
谢谢你的建议,我刚刚看到这篇文章:,我猜jsp可以在相关的action类中获取变量,即使变量是私有的?但是通过您的评论,问题似乎是jsp无法获取私有变量。我说的对吗?@user1055108该页面具有框架属性的getter。我不明白为什么它使用一个额外的变量;戴夫·牛顿:是的,这也让我困惑。在jsp中,他们从不调用get方法,struts会调用get方法吗?@user1055108您需要了解struts 2是如何工作的;在JSP OGNL表达式中,您按名称引用属性。您可以通过调用getter来访问它,但没有理由这么做。@Dave Newton我知道它是如何工作的,我是否需要在xml中执行某些操作,您能否给我一个示例,因为我的jsp仍然无法执行内部js。我将在何处执行isAddComment()方法。并应添加一个#::?
<s:if test="%{addComment}">
<script type="text/javascript">
window.close();
</script>
</s:if>