[英]How to access as JsonNode from session | ${user.get()}
I'm creating a simple session object (JsonNode) 我正在创建一个简单的会话对象(JsonNode)
JsonNode me = mapper.readValue(result, JsonNode.class);
HttpSession session = request.getSession(true);
session.setAttribute("user", me);
The trying to access it like this. 试图像这样访问它。
${user} // works print {"id":8,"name":"jones"..}
${user.id} // trows Error "Property 'id' not found on type org.codehaus.jackson.node.ObjectNode"
${user.get(0)} // nothing printed out.
${user.size()} // return 4, id,name,username,token
How do would i re factor my code to fix the problem, or even the selector? 我将如何重构代码以解决问题,甚至选择器?
The version @mindas suggested should work, unless you are using Tomcat 7. Seems that Tomcat 7 has a bug when calling overloaded methods with JSP EL ( see an issue here ). 除非您使用的是Tomcat 7,否则建议的版本@mindas应该可以使用,除非使用JSP EL调用重载方法时,Tomcat 7似乎有错误( 请参阅此处的问题 )。 Are you using Tomcat 7? 您正在使用Tomcat 7吗?
The ${user.get(0)}
isn't printing anything because that method is for accessing the value of the specified element of an array node. ${user.get(0)}
不打印任何内容,因为该方法用于访问数组节点指定元素的值。 Yours is not an array node and if you use ${user.get("id")}
you should get the value for the id
field, unless you are on Tomcat 7 where you will get some error like Cannot convert id of type class java.lang.String to int
. 您的不是数组节点,如果您使用${user.get("id")}
,则应该获取id
字段的值,除非您在Tomcat 7上,否则会收到诸如Cannot convert id of type class java.lang.String to int
。
You could try a quick and dirty test with ${user.findValue("id")}
and see if you at least get some result, but if you don't mind me saying, I think it will be better if you would not expose a raw object like JsonNode
to the JSP but some sort of User
POJO with getters and setters for id
, name
, username
and token
instead. 您可以使用${user.findValue("id")}
进行快速而肮脏的测试,看看是否至少能得到一些结果,但是如果您不介意我说的话,我认为如果您不这样做的话会更好。将JsonNode
之类的原始对象JsonNode
给JSP,但是将某种User
POJO带有id
, name
, username
和token
getter和setter方法。 You could then use ${user.id}
in the JSP and you will have no issue with Tomcat. 然后,您可以在JSP中使用${user.id}
,而Tomcat则没有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.