JSTL parse negative number java.lang.NumberFormatException: For input string: ""

882 Views Asked by At

I request url http://localhost:8080/myApp/reg/show?pageNumber=1&pageRange=-1 and in show.jsp i have

<ts:pagination allData="${tecBacking.tec}"  
          pageRange="${param.pageRange}"
          pageNumber="${param.pageNumber}"
          url="show">
     <jsp:attribute name="tableHeaders">
          <th>Идентификатор</th>
          <th>Код</th>
          <th>Описание</th>
          <th>Тип</th>
          <th>Създадено от</th>
     </jsp:attribute>
</ts:pagination>

And my pagination tag

<%@tag description="This is for common pagination" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%-- The list of normal or fragment attributes can be specified here: --%>

<%@attribute name="tableHeaders" fragment="true" required="true"%>
<%@attribute name="allData" required="false" type="java.util.Collection"%>
<%@attribute name="pageNumber" required="false" type="java.lang.Long"%>
<%@attribute name="pageRange" required="false" type="java.lang.Long"%>
<%@attribute name="url" required="false"%>

<c:set target="${paginationModel}" property="objects" value="${(empty allData) ? paginationModel.objects : allData}" />
<c:set target="${paginationModel}" property="pageNumber" value="${(empty pageNumber) ? 1 : pageNumber}" />
<c:set target="${paginationModel}" property="pageRange" value="${(empty pageRange) ? 10 : pageRange}" />
<c:set target="${paginationModel}" property="url" value="${(empty url) ? paginationModel.url : url}"/>

paginationModel is

@Named
@SessionScoped
public class PaginationModel implements Serializable {

    private List<List<String>> objects;
    private Long pageNumber;
    private Long pageRange;
    private String url;

    public PaginationModel() {
        objects = new ArrayList<>();
        pageNumber = 1L;
        pageRange = 10L;
        url = "";
    }
    //getters and setters
}

But in response I receive this exception

javax.servlet.ServletException: javax.servlet.jsp.JspException: java.lang.NumberFormatException: For input string: ""

The problem is in parsing -1 from pageRange requestParam. I try to make change in pagination tag like:

<c:set target="${paginationModel}" property="pageRange" value="${10}" />

and everything is OK, but when I try

<c:set target="${paginationModel}" property="pageRange" value="${pageRange}" />

I receive same exception. I try to call ${pageRange} in pagination tag (and parsed ${10} to paginationModel.pageRange) and output is

-1 I can't understand where is my mistake(paginationModel.pageRange has type java.lang.Long and attribute pageRange has set type="java.lang.Long") and how to fix exception.

1

There are 1 best solutions below

0
On

java.lang.NumberFormatException: For input string: ""

The problem is in parsing -1 from pageRange requestParam.

This is incorrect.

java.lang.NumberFormatException: For input string: "" is caused by parsing "" (empty string), for example, new Long(""). Could you check if your program works after changing value="${pageRange}" to value="${param.pageRange}"?