简体   繁体   中英

jsp servlet form get value of checkbox/selection expression language

I create a web application with jsp and servlets.

<p>
   <label>UserName:</label> 
   <input name="userName" type="text"  value="${param.userName}">
</p>

Usually I use Expression Language in order not to lose the entered information into forms (in case of an input error in another field).

It is also possible to use this method also in combination with selections and checkboxes?

Or do you have any other ideas? I actually try to avoid this:

Servlet:

  if( request.getParameterValues("active") != null){
      request.setAttribute("vActive", "");
  }

JSP:

<input name="active" type="checkbox" <%  if ( request.getAttribute("vActive") != null ) { out.print(" checked=\"checked\""); }%> value="">

如果您只想基于请求范围中的某个值预先选择一个复选框,则可以像这样在EL中进行操作

<input name="active" type="checkbox" value="Car" ${vActive != null ? "checked" : ""} />

This works:

<input name="active" type="checkbox" ${empty(param.active) ? "" : "checked"} value="true">

And finally I understood how to use EL, thank you all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM