Use Hibernate session to query the database, and display the table content in the JSP page method

Source: Internet
Author: User

Tried a lot of methods are not successful, finally let me find this method!

First write the code in the background:

Transaction tx = Session.begintransaction (); List List = Session.createsqlquery ("SELECT * from Jike"). List (); Tx.commit (); List List1 = new ArrayList (); Iterator it = List.iterator (); while (It.hasnext ()) {jike user = new Jike ();//This sentence must be placed inside, otherwise the data ob Ject[] Objs = (object[]) it.next (); User.setxuehao (objs[0].tostring ()); User.setname (objs[1].tostring ()); List1.add ( user);} 1.HttpServletRequest request = Servletactioncontext.getrequest (); 2.request.setattribute ("list", List1); return " Success ";

You can also use the STRUTS2 default interceptor without writing 1 and 2, but Java must have both set () and get () methods.

Then write the JSP code that receives the data in the foreground:

<table border= "1" >          <s:iterator value = "#request. List" >                     <tr>                         <td>                               <s: Property value= "Xuehao"/>                        </td>                      </tr>           </s:iterator></table>

The above is a label that uses STRUTS2, or you can use an EL expression:

<td>${name}</td>

Or you can:

<s:iterator> inside plus ID, for example:

<s:iterator value= "#request. List" id= "id" >

<td><s:property value= "#id. Name"/></td>

Use Hibernate session to query the database, and display the table content in the JSP page method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.