JSP implements a custom form label that automatically generates the form label html code

  • 2021-07-13 06:03:33
  • OfStack

This article illustrates the JSP implementation of a custom form label that automatically generates the html code for the form label. Share it for your reference. The details are as follows:

This is a simple JSP form label written by myself, which is used to automatically generate checkbox, select, radio and other labels. It is passed into the menu set to generate html code, and the specified value is automatically selected for jsp page of java web project.

1. Servlet part code:


Map<String, String> map = new HashMap<String, String>();
map.put("2", " Options 2");
map.put("3", " Options 3");
map.put("4", " Options 4");
map.put("5", " Options 5");
map.put("6", " Options 6");
req.setAttribute("map", map);
List<String> list = new ArrayList<String>();
list.add("4");
list.add("5");
list.add("6");
req.setAttribute("list", list);

2. JSP code:


<%@taglib prefix="g" uri="http://www.golpesoft.com" %>
<g:select value="1">
  <option value="0"> Options 1</option>
  <g:option value="1"> This one was chosen </g:option>
  <!-- items Must be a collection or map -->
  <g:options items="${map }"/>
</g:select>
<!--checkValue Support el, It can be a collection or a string, specifying that the automatically generated checkbox Which are selected  -->
<g:checkboxs checkValue="${list }" items="${map }" name="checkbox"/>
<!-- checkValue Can only be a string, support el -->
<g:radios items="${map }" name="radio" checkValue="3"/>

3. Generated html code:


<select>
  <option value="0"> Options 1</option>
  <option value="1" selected="selected"> This one was chosen </option>
  <!-- items Must be a collection or map -->
  <option value="3"> Options 3</option>
<option value="2"> Options 2</option>
<option value="6"> Options 6</option>
<option value="5"> Options 5</option>
<option value="4"> Options 4</option>
</select>
<!--checkValue Support el, It can be a collection or a string, specifying that the automatically generated checkbox Which are selected  -->
<div class="checkbox-div"><input type="checkbox" id="checkbox1" name="checkbox" value="3" /><label for="checkbox1"> Options 3</label></div><div class="checkbox-div"><input type="checkbox" id="checkbox2" name="checkbox" value="2" /><label for="checkbox2"> Options 2</label></div><div class="checkbox-div"><input type="checkbox" id="checkbox3" name="checkbox" value="6" checked="checked" /><label for="checkbox3"> Options 6</label></div><div class="checkbox-div"><input type="checkbox" id="checkbox4" name="checkbox" value="5" checked="checked" /><label for="checkbox4"> Options 5</label></div><div class="checkbox-div"><input type="checkbox" id="checkbox5" name="checkbox" value="4" checked="checked" /><label for="checkbox5"> Options 4</label></div>
<!-- checkValue Can only be a string, support el -->
<div class="radio-div"><input type="radio" id="radio1" name="radio" value="3" checked="checked" /><label for="radio1"> Options 3</label></div><div class="radio-div"><input type="radio" id="radio2" name="radio" value="2" /><label for="radio2"> Options 2</label></div><div class="radio-div"><input type="radio" id="radio3" name="radio" value="6" /><label for="radio3"> Options 6</label></div><div class="radio-div"><input type="radio" id="radio4" name="radio" value="5" /><label for="radio4"> Options 5</label></div><div class="radio-div"><input type="radio" id="radio5" name="radio" value="4" /><label for="radio5"> Options 4</label></div>

I hope this article is helpful to everyone's JSP programming.


Related articles: