简体   繁体   English

在 jsp 页面上显示 Arraylist

[英]Display Arraylist on jsp page

I am trying to display the following ArrayList in the.jsp page shown but I can't seem to see any values once I run my portlet, where is the problem?我试图在显示的.jsp 页面中显示以下 ArrayList 但运行我的 portlet 后似乎看不到任何值,问题出在哪里?

code.java代码.java

 public class TestPortlet extends MVCPortlet {
    public void displayProcess(ActionRequest request, ActionResponse response) {
        ArrayList<String> process = new ArrayList<>();
        process.add("a");
        process.add("b");
        process.add("c");
        process.add("d");
        process.add("e");

        request.setAttribute("processName", process);
    }
}

The jsp page is as shown: jsp页面如图:

<%@ include file="/init.jsp"%>

<jsp:useBean id="processName" class="java.util.ArrayList" scope="request" />

<aui:select id="process" name="processitems">
    <c:forEach items="${processName}" var="process">
        <aui:option value="${process}">
            ${process}
        </aui:option>
    </c:forEach>
</aui:select>

Any help would be much appreciated.任何帮助将非常感激。

Not sure if this is a complete answer, but some steps to figure out what the problem is.不确定这是否是一个完整的答案,但有一些步骤可以找出问题所在。 I'm taking your question as well as some comments:我正在接受您的问题以及一些评论:

You're implementing a portlet action handler, from there, you won't forward/dispatch to a particular jsp: A portlet's ACTION phase is only good for changing the state, while displaying the result is part of the VIEW phase.您正在实现一个 portlet 操作处理程序,从那里您不会转发/分派到特定的 jsp:portlet 的 ACTION 阶段仅适用于更改 state,而显示结果是 VIEW 阶段的一部分。 The code that you've posted (though it's obviously simplified) looks like it rather wants to live in doView() .您发布的代码(尽管它显然是简化的)看起来更想放在doView()中。

In fact, that may be all you need: If you just display the portlet, only the VIEW phase will be triggered.事实上,这可能就是您所需要的:如果您只显示portlet,则只会触发 VIEW 阶段。 Just displaying the portlet will not trigger the action handler, which you can validate in a debugger.仅显示 portlet不会触发操作处理程序,您可以在调试器中对其进行验证。

For the JSP: All you say is you "can't seem to see" any of the results.对于 JSP:您所说的只是“似乎看不到”任何结果。 Validate where your problem is: Is the list there?验证您的问题出在哪里:列表在那里吗? Does enumerating the list work?枚举列表有效吗? Is your problem with the AUI taglib?你的 AUI 标签库有问题吗? You can easily check this by removing all of the other tags and rather generate pure output by removing a bit.您可以通过删除所有其他标签轻松检查这一点,而是通过删除一点生成纯 output。

When you look at the output's source and any of the JSP stuff survives (eg the ${processName} , or <aui:select..> , <c:forEach...> , then you'll know that this is the root cause for "not seeing" anything. You might miss a taglib or other.当您查看输出的源代码并且任何 JSP 的东西仍然存在时(例如${processName}<aui:select..><c:forEach...> ,那么您就会知道这是根导致“没有看到”任何东西。你可能会错过一个标签库或其他。

Last: I've never tried this, but <aui:select> is a tag that's meant to be used within a form, and I'm not sure what it does outside of a form - you may want to surround it with <aui:form....> and see what happens then.最后:我从未尝试过,但<aui:select>是一个用于在表单中使用的标签,我不确定它在表单之外做什么 - 你可能想用<aui:form....>包围它<aui:form....>然后看看会发生什么。

I found the solution:我找到了解决方案:

I was to use:我要使用:

public void displayProcess(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {

instead of代替

 public void displayProcess(ActionRequest request, ActionResponse response) {

and at the bottom of my method the following:在我的方法的底部:

renderRequest.setAttribute("process", process); 
    super.render(renderRequest, renderResponse);

On the jsp page, at the topmost point;在 jsp 页面的最顶端; receive the ArrayList that you are passing as:接收您传递的 ArrayList:

<% ArrayList<String> process = (ArrayList) request.getAttribute("process"); %>

. .

The results are as follows:结果如下:

jsp上的ArrayList,liferay

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM