Domanda

Ho un URL come search.do?offset=20

L'offset

a volte è nell'URL a volte no. Quando non è nell'URL, voglio che sia 0.

provo, senza successo, a recuperare il valore con uno scriptlet come segue:

<%  Integer offset = (pageContext.findAttribute("offset")==null) ? new Integer("0") : new Integer((String) pageContext.findAttribute("offset")); %>

Qualcuno sa cosa sto facendo di sbagliato?

È stato utile?

Soluzione

Dovresti usare questo invece.

<% Integer offset = request.getParameter("offset") != null && request.getParameter("offset").length() > 0 ? new Integer(request.getParameter("offset")) : new Integer(0); %>

Fai attenzione perché se " offset " il parametro ha una rappresentazione di numero intero errata, verrà generata una NumberFormatException.

Questo è JSP di base. È possibile utilizzare Struts o altri framework J2EE che rendono queste conversioni più sicure per te.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top