Dwz: Data Conversion from database to JSP

Source: Internet
Author: User

Dwz: Data Conversion from database to JSP

Preface: This is often the case where the data format obtained from the database is not displayed on the JSP page. I recommend the following three methods.

Direct Database SQL Conversion

Some data can be directly converted using SQL, but generally the processing format is relatively simple.

Convert (case m. stauts when 1 then 'enabled 'when 2 then' to stop receiving new tickets 'when 3 then' disabled account' end, char) stauts

This form is not good at processing complex types, but it is easy to get it ready at a time.

Use JSTL

JSTL format processing is also quite good. Here is an article on JSTL label reference manual.


  

In this form, the front and back-end data must follow the jstl label format.

Control terminal Conversion

This method is mainly to use the controller to convert the obtained data, replace it with the format required by the front-end, and then display it for the front-end. Here I will describe it in detail.

First look at the background data:

Id Uid Username Ip Logintime Logoutime
1 1 00010001 127.0.0.1 1434679452651 1435021823460

 


Then we use SQL statements to obtain the original data.

 

select m2.uid,        convert(m2.username,char) username,        m2.ip ip,        m2.logintime logintime,        m2.logoutime logoutime,        (m2.logoutime-m2.logintime) onlinetime        from loginfo

 


Then we use the controller for conversion.

 

List
  
    memloginfolist = this.memLoginfoMapper.getMemLoginfoList(vo, vo.createRowBounds());            for (HashMap map : memloginfolist) {                String logintime = DateUtil.formatTimeMillis(map.get("logintime").toString());                String logoutime = DateUtil.formatTimeMillis(map.get("logoutime").toString());                String onlinetime = DateUtil.formatTimeInterval(map.get("onlinetime").toString());                map.put("logintime", logintime);                map.put("logoutime", logoutime);                map.put("onlinetime", onlinetime);            }
  
Public static String formatTimeInterval (String time) {long timeInterval = Long. parseLong (time); long day = 0; long hour = 0; long min = 0; long sec = 0; day = timeInterval/(24*60*60*1000 ); hour = (timeInterval/(60*60*1000)-day * 24); min = (timeInterval/(60*1000 )) -day * 24*60-hour * 60); sec = (timeInterval/1000-day * 24*60*60-hour * 60*60-min * 60 ); stringBuilder result = new StringBuilder (); if (day> 0) {result. append (day); result. append ("day");} if (hour> 0) {result. append (hour); result. append ("when");} if (min> 0) {result. append (min); result. append ("points");} if (sec> 0) {result. append (sec); result. append ("seconds");} return result. toString ();}

 


Front-end display is


This type of processing is relatively casual.

 

Summary: I have always wanted to write a custom jstl label, but it is more troublesome to use, so we recommend the above three methods for you.

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.