3
votes

Help me please with converting Object to JSON String in JSTL. This is the part of the code of my JSP page:

<div class="form-group input-group">
     <span class="input-group-addon">Equipment</span>
     <select class="selectpicker" name="equipment" data-live-search="true">
          <jsp:useBean id="Gson" class="com.google.gson.Gson"/>
          <c:forEach var="equipment" items="${EquipmentBean.findAllEquipment()}">
               <option value="${Gson.toJson(equipment)}"><c:out value="${equipment.name}"/></option>
          </c:forEach>
     </select>
</div>

I need it for further using JSON in JS. When I do this and look at serialized string in browser, I see the next: nickname=Hero&level=80&race=Human&weapon=1&equipment=%7B. equipment = %7B. What is the %7B? I get normal name in c:out, I can get any field, but don't convert it to JSON. Thank's in advance.

1
please post sample JSONBioGenX
Putting JSON in html attribute won't work. It needs to be escaped. If you're learning I'd suggest you looking at modern web framework like Spring MVC or Playframework (if you're intrested in server-side rendering)Piotr Gwiazda

1 Answers

0
votes

http://thinkinginsoftware.blogspot.com.tr/2013/02/custom-jsp-taglib-to-convert-object-to.html you can write a taglib to convert your object into json. You may check this link.