简体   繁体   English

如何将java对象传递给jsp页面

[英]How to pass a java object to jsp page

I have a serveresource method which is invoked on clicking a link. 我有一个servresource方法,可以在单击链接时调用。 The serveresource method takes the input from the parameter that was passed and retrieves a row from the database. serveresource方法从传递的参数中获取输入并从数据库中检索行。 Now all the values in a row are in set using the mutator methods. 现在,使用mutator方法将行中的所有值都设置为set。 I have everything in a java object. 我拥有java对象中的所有内容。 I need to pass this object to a jsp page to print the values of a single row on the jsp page. 我需要将此对象传递给jsp页面以在jsp页面上打印单行的值。 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. 我不知道如何在jsp页面中处理该java对象,而不是将每个值设置为serveresource方法中的属性。 Need help from experts.. Thanks in Advance 需要专家的帮助..在此先感谢

UPDATE 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. 这是因为我有一个Ajax调用,当我设置值时,它处于完全不同的生命周期中,这导致了问题。 I figured it out. 我想到了。

You should defining the java object as Bean in JSP . 您应该在JSP中将java对象定义为Bean The Bean in JSP can be defined using < jsp:useBean..> standard jsp tag. 可以使用<jsp:useBean ..>标准jsp标记定义JSP中的Bean。 And set and get property using < jsp:setProperty..> and < jsp:getProperty..> standard jsp tags. 使用<jsp:setProperty ..>和<jsp:getProperty ..>标准jsp标记设置和获取属性

Refernces: Refernces:

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>

you can bind with request object 你可以绑定请求对象

In Servlet or JSP
request.setAttribute("strIdentifire", yourJavaObject);


In JSP
YourJavaObjectClass obj = (YourJavaObjectClass)request.getAttribute("strIdentifire");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM