/src/main/java/com/tmm/nosql/mongodb/taglibs/JsonGetVariable.java
Java | 63 lines | 44 code | 11 blank | 8 comment | 2 complexity | bcd1dd431e761499477ef83861ac4873 MD5 | raw file
- /**
- *
- */
- package com.tmm.nosql.mongodb.taglibs;
- import javax.servlet.jsp.JspException;
- import javax.servlet.jsp.JspTagException;
- import javax.servlet.jsp.PageContext;
- import javax.servlet.jsp.tagext.TagSupport;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonObject;
- /**
- * @author robert.hinds
- *
- * TagLib designed to get String from Google JSON Array
- */
- public class JsonGetVariable extends TagSupport{
-
- private static final long serialVersionUID = 2617352233757753085L;
- private JsonObject json = null;
- private String key;
- private String var;
- @Override
- public int doStartTag() throws JspException {
- try {
- if (json!=null && key!=null){
- JsonElement j = json.get(key);
- pageContext.setAttribute(var, (j!=null?j.getAsString():"not known"), PageContext.PAGE_SCOPE);
- }
- } catch (Exception e) {
- e.printStackTrace();
- throw new JspTagException("Error getting String From JSON: " + e.getMessage());
- }
- return SKIP_BODY;
- }
-
-
- public JsonObject getJson() {
- return json;
- }
- public void setJson(JsonObject json) {
- this.json = json;
- }
- public String getKey() {
- return key;
- }
- public void setKey(String key) {
- this.key = key;
- }
- public String getVar() {
- return var;
- }
- public void setVar(String var) {
- this.var = var;
- }
- }