PageRenderTime 51ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/tmm/nosql/mongodb/taglibs/JsonGetVariable.java

https://github.com/robhinds/SpringMongoIntegrationExample
Java | 63 lines | 44 code | 11 blank | 8 comment | 2 complexity | bcd1dd431e761499477ef83861ac4873 MD5 | raw file
  1. /**
  2. *
  3. */
  4. package com.tmm.nosql.mongodb.taglibs;
  5. import javax.servlet.jsp.JspException;
  6. import javax.servlet.jsp.JspTagException;
  7. import javax.servlet.jsp.PageContext;
  8. import javax.servlet.jsp.tagext.TagSupport;
  9. import com.google.gson.JsonElement;
  10. import com.google.gson.JsonObject;
  11. /**
  12. * @author robert.hinds
  13. *
  14. * TagLib designed to get String from Google JSON Array
  15. */
  16. public class JsonGetVariable extends TagSupport{
  17. private static final long serialVersionUID = 2617352233757753085L;
  18. private JsonObject json = null;
  19. private String key;
  20. private String var;
  21. @Override
  22. public int doStartTag() throws JspException {
  23. try {
  24. if (json!=null && key!=null){
  25. JsonElement j = json.get(key);
  26. pageContext.setAttribute(var, (j!=null?j.getAsString():"not known"), PageContext.PAGE_SCOPE);
  27. }
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. throw new JspTagException("Error getting String From JSON: " + e.getMessage());
  31. }
  32. return SKIP_BODY;
  33. }
  34. public JsonObject getJson() {
  35. return json;
  36. }
  37. public void setJson(JsonObject json) {
  38. this.json = json;
  39. }
  40. public String getKey() {
  41. return key;
  42. }
  43. public void setKey(String key) {
  44. this.key = key;
  45. }
  46. public String getVar() {
  47. return var;
  48. }
  49. public void setVar(String var) {
  50. this.var = var;
  51. }
  52. }