Different jsp rendering in Liferay

拥有回忆 提交于 2020-01-17 03:46:26

问题


I need to make following construction:

String pageName = request.getParameter("pageName");
if ("some_parameter".equals("pageName")) {
    include("html/one.jsp", request, response);
}
if ("third_parameter".equals("pageName")) {
    include("html/two.jsp", request, response);
}

But I always have troubles like NullPointerException.

I mean I try to make different renderURL show different jsps but I don't know how. If you don't undestand what Im about but have knowledge in this theme please write smth in comments I'll give more info. Thanks in advance!


OK, I give you my example:

public static final String VIEW = "view";                           //view.jsp parameter
public static final String ADD_BOOK = "add_book";                   //add_book.jsp parameter

private final String VIEW_PAGE_PATH = "/html/view.jsp";             //view.jsp path parameter
private final String ADD_BOOK_PAGE_PATH = "/html/add_book.jsp";     //add_book.jsp path parameter

@Override
public void render(RenderRequest request, RenderResponse response)
        throws PortletException, IOException {

    String pageName = request.getParameter("pageName");
    if (pageName.equalsIgnoreCase(ADD_BOOK)) {              
        include(ADD_BOOK_PAGE_PATH, request, response);
    } else {
        include(VIEW_PAGE_PATH, request, response);
    }
}

and my view.jsp:

<portlet:renderURL var="addBookVar">
    <portlet:param name="pageName" value="<%=Library.ADD_BOOK %>"/>
</portlet:renderURL>

<a href="${addBookVar}">Add Book</a>

Why this code doesn't work? What should I do in order to achieve situation when different renderURLs show different jsps?


回答1:


Please check pageName for "not null" first before going for comparison. Use StringUtils.isNotEmpty(..) class ref for checking "if string is not null and not empty" before making comparison. Source Post




回答2:


replace your variables with the string value and should work Note: putting variables between quotes make it string not variables

your code should look like this:

String pageName = request.getParameter("pageName");
if (pageName.equals("some_parameter")) {
   include("html/one.jsp", request, response);
}
if (pageName.equals("third_parameter")) {
   include("html/two.jsp", request, response);
}



回答3:


There may be more than one reason you get NullPointerException. It's because the parameters you checked doesn't exist in the request, the value would be null.So better to debug it to find out the exact issue here.




回答4:


try getPortletContext().getRequestDispatcher(ADD_BOOK_PAGE_PATH).include(request,response) instead of include(ADD_BOOK_PAGE_PATH, request, response)



来源:https://stackoverflow.com/questions/39233585/different-jsp-rendering-in-liferay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!