0

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?

Al.Boldyrev
  • 486
  • 7
  • 30
  • Try using like if (pageName.equals("some_parameter")) – rickz Aug 30 '16 at 17:33
  • 1
    "I always have NullpointerExceptions" - you do know why they occur, right? You can even make a debugger stop every time it runs into a NullpointerException. A stacktrace should also help locating where exactly you're dereferencing a null pointer. If you point a stacktrace here, let us also know which line in the code corresponds to the line that the exception occurs in. – Olaf Kock Aug 31 '16 at 07:20

4 Answers4

0

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);
}
0

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.

0

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

Community
  • 1
  • 1
Shivam Aggarwal
  • 795
  • 1
  • 11
  • 30
0

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