有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaLiferay门户不将对象从流程阶段发送到jsp

我是liferay的新手,现在我正在用liferay食谱学习liferay。我在将对象从porlet发送到jsp页面时遇到问题 here这是我的猪肉,。在LibraryPortlet.java中,我有一个函数:

public void searchBooks(ActionRequest actionRequest,
        ActionResponse actionResponse) throws IOException, PortletException {

    String searchTerm = ParamUtil.getString(actionRequest, "searchTerm");

    if (Validator.isNotNull(searchTerm)) {
        try {
            List<LMSBook> lmsBooks = LMSBookLocalServiceUtil
                    .searchBooks(searchTerm);

            actionRequest.setAttribute("SEARCH_RESULT", lmsBooks);
            actionRequest.setAttribute("test", "sentence to test");
            actionResponse.setRenderParameter("jspPage",
                    LibraryConstants.PAGE_LIST);

        } catch (SystemException e) {
            e.printStackTrace();
        }
    }
}

在文件list.jsp中,我有

List<LMSBook> booksTemp = (List<LMSBook>) request
        .getAttribute("SEARCH_RESULT");

List<LMSBook> books = Validator.isNotNull(booksTemp) ? ListUtil
        .copy(booksTemp) : LMSBookLocalServiceUtil.getLMSBooks(0,
        LMSBookLocalServiceUtil.getLMSBooksCount());
System.out.println("test: "+ request.getAttribute("test"));

list.jsp中,booksTempnull因为liferay无法将我的对象从LibraryPortlet.java发送到jsp,我尝试发送字符串“句子到测试”,但在控制台中,它显示test:null

任何人都有同样的问题,你能帮我吗


共 (1) 个答案

  1. # 1 楼答案

    必须首先使用正确的请求来设置属性。问题是在LibraryPortlet中,您使用的是PortletRequest类型,而JSP中的对象是HttpServletRequest

    对于您的情况,请在LibraryPortlet.java中执行以下操作:

    HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
    
    request.setAttribute("SEARCH_RESULT", lmsBooks);
    

    这也适用于您正在设置的任何其他属性