Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request.getParameter with JSP

Tags:

jsp

I'm trying to auto-populate a drop down list based on a request parameter. I'm fairly new to JSP so forgive me for the simple question.

The following works fine and displays the alert correctly:

 alert('<%=request.getParameter("lang") %>');

So I know what I am trying to do is easy enough. But when I add this same logic in with my select statement using:

<option <c:if test="${request.getParameter(\"lang\")=='En'}"> selected="selected" </c:if>    value="<c:out value="${english}"/>">English</option>

I get an exception saying "The function getParameter must be used with a prefix when a default namespace is not specified".

I'm a little confused as to why this doesn't work here...

Thanks in advance

like image 890
Fraser Avatar asked Mar 26 '12 13:03

Fraser


2 Answers

request.getParameter() will not resolved by EL. Request parameter can be accessed using implicit variable param. i.e, ${param.lang}

Change this

<option <c:if test="${request.getParameter(\"lang\")=='En'}"> selected="selected" </c:if>    value="<c:out value="${english}"/>">English</option>

to

<option <c:if test="${param.lang == 'En'}"> selected="selected" </c:if> value="${english}">English</option>
like image 111
Ramesh PVK Avatar answered Oct 22 '22 07:10

Ramesh PVK


another way :

<option <c:if test='${param[lang] == "En"}'> selected="selected" </c:if>
    value="${english}">

    English
</option>

Make sure you have added c-taglibs URI on top of the JSP.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
like image 38
tusar Avatar answered Oct 22 '22 05:10

tusar