如何将java对象传递给jsp页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9679217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass a java object to jsp page
提问by user525146
I have a serveresource method which is invoked on clicking a link. The serveresource method takes the input from the parameter that was passed and retrieves a row from the database. Now all the values in a row are in set using the mutator methods. I have everything in a java object. I need to pass this object to a jsp page to print the values of a single row on the jsp page. I am not sure how to handle that java object in the jsp page instead of setting each value as an attribute in the serveresource method. Need help from experts.. Thanks in Advance
我有一个单击链接时调用的 serveresource 方法。serveresource 方法从传递的参数中获取输入并从数据库中检索一行。现在,使用 mutator 方法设置了一行中的所有值。我在一个java对象中拥有一切。我需要将此对象传递给 jsp 页面以在 jsp 页面上打印单行的值。我不确定如何在 jsp 页面中处理该 java 对象,而不是将每个值设置为 serveresource 方法中的属性。需要专家的帮助.. 提前致谢
UPDATE
更新
It was because I have an Ajax call and when I set values it is in a completely different life cycle which is causing the problem. I figured it out.
这是因为我有一个 Ajax 调用,当我设置值时,它处于完全不同的生命周期,这导致了问题。我想到了。
回答by Ramesh PVK
You should defining the java object as Bean in JSP. The Bean in JSP can be defined using < jsp:useBean..>standard jsp tag. And set and get property using < jsp:setProperty..> and < jsp:getProperty..>standard jsp tags.
您应该在 JSP中将java 对象定义为 Bean。JSP中的Bean可以使用<jsp:useBean..>标准jsp标签来定义。并使用< jsp:setProperty..> 和< jsp:getProperty..>标准jsp 标签设置和获取属性。
Refernces:
参考资料:
回答by Crollster
The usual method is to add it to the HttpServletRequest
object, thus:
通常的方法是将其添加到HttpServletRequest
对象中,因此:
MyBean myBean = new MyBean();
myBean.setValue("something);
myBean.setAnotherValue("something else");
// ... stuff ...
request.setAttribute("myBean", MyBean);
This can be accessed from the jsp page using EL thus:
这可以使用 EL 从 jsp 页面访问,因此:
<table>
<tr>
<td>${myBean.value}</td>
<td>${myBean.anotherValue}</td>
</tr>
</table>
回答by Ankur Loriya
you can bind with request object
你可以绑定请求对象
In Servlet or JSP
request.setAttribute("strIdentifire", yourJavaObject);
In JSP
YourJavaObjectClass obj = (YourJavaObjectClass)request.getAttribute("strIdentifire");