struts2 迭代器标签中包含 contains

发布于 2024-07-29 19:08:14 字数 678 浏览 5 评论 0原文

我有以下代码似乎不起作用。 我不明白为什么这行不通。

<s:iterator value=%{questions} id="question">
   <s:if test='%{incorrectQs.contains("#question.questionId")}'>
      Print something here
   </s:if>
</s:iterator>

在上面的代码中,基本上我在操作类中有一个名为 getQuestions 的方法。 该列表包含 Exam 类的对象,该类具有获取/设置 QuestionId。 我还有不正确的 Qs 列表,其中包含字符串。 我正在检查不正确的Qs是否包含questionId。 如果是的话我想打印一些文本。 尽管上面的代码不起作用,但下面的代码随机可以正常工作。 有时它有效,有时它不起作用...:(

<s:iterator value=%{questions}>
   <s:if test='%{incorrectQs.contains("${questionId}")}'>
      Print something here
   </s:if>
</s:iterator>

任何人都可以对此提供一些见解吗?

I have the following code which does not seem to work. I fail to see why this wont work.

<s:iterator value=%{questions} id="question">
   <s:if test='%{incorrectQs.contains("#question.questionId")}'>
      Print something here
   </s:if>
</s:iterator>

In the above code, basically I have a method called getQuestions in the action class. This list contains objects of Exam class which has a get/set questionId. I also have incorrectQs list which contains strings. I am checking if incorrectQs contains a questionId. if it does i want to print some text. Eventhough, the above code does not work, the code below works fine randomly. Sometimes it works and sometimes it does not...:(

<s:iterator value=%{questions}>
   <s:if test='%{incorrectQs.contains("${questionId}")}'>
      Print something here
   </s:if>
</s:iterator>

can anyone provide some insight in this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆伤 2024-08-05 19:08:14

我不确定你的 Action 类是什么样子,但你可以尝试这样的事情。 当您位于 s:iterator 标记内时,我认为每个 %{foo} 表达式将尝试访问您正在迭代的特定实例(问题,而不是列表)的问题)。

注意:可能有一种方法使用 #s:iterator 标记内引用父对象。 我只是还没有这样做,所以不能确定它是否会对您有所帮助。

public class ActionClass ... {

    private List<Question> mQuestions;
    private Set<Integer> mIncorrectQuestions;

    public String execute() {
        // build/read questions, populate incorrect questions, etc...
        return SUCCESS;
    }

    public List<Question> getQuestions() {
        return mQuestions;
    }

    // does not need to be an inner class
    public class Questions {

        private int mQuestionId;

        public int getQuestionId() {
            return mQuestionId;
        }

        public boolean getIsIncorrect() {
            return mIncorrectQuestions.contains(mQuestionId);
        }
}

然后在您的 JSP 中:

<s:iterator value='%{questions}'>
    <s:if test='%{isIncorrect}'>
        Question <s:property value='${questionId}'/> is Incorrect!
    </s:if>
</s:iterator>

I'm not sure what your Action class looks like, but you can try something like this. When you're inside the s:iterator tag, I think each %{foo} expression will attempt to access the specific instance you are iterating (the question, not the List of questions).

Note: There may be a way using # to reference the parent object from within the s:iterator tag. I just haven't done it, so can't say for sure if it would help you here.

public class ActionClass ... {

    private List<Question> mQuestions;
    private Set<Integer> mIncorrectQuestions;

    public String execute() {
        // build/read questions, populate incorrect questions, etc...
        return SUCCESS;
    }

    public List<Question> getQuestions() {
        return mQuestions;
    }

    // does not need to be an inner class
    public class Questions {

        private int mQuestionId;

        public int getQuestionId() {
            return mQuestionId;
        }

        public boolean getIsIncorrect() {
            return mIncorrectQuestions.contains(mQuestionId);
        }
}

And then in your JSP:

<s:iterator value='%{questions}'>
    <s:if test='%{isIncorrect}'>
        Question <s:property value='${questionId}'/> is Incorrect!
    </s:if>
</s:iterator>
剩余の解释 2024-08-05 19:08:14
#question.questionId.toString()
#question.questionId.toString()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文