DWZ: Database-to-JSP data conversion

Source: Internet
Author: User

Foreword : There are often such cases, the data format obtained from the database is not what we want to show on the JSP page, I recommend to everyone the following three ways.

Database SQL Direct Conversion

There is some data that we can direct directly through SQL, but the general processing format is relatively single content.

end,char) stauts

This form is not good at dealing with more complex types of formats, but it is convenient to place them once.

Using Jstl

JSTL format processing is also quite good, here is an article Jstl label reference Manual

<fmt:formatNumber value="${item.order_price}" pattern="#,##0.00#"/>

This form requires that the front and back data follow the JSTL label format.

Control side for conversion

The main way is to use the controller to convert the obtained data, replace it with the format required by the front end, and then for the front-end display, here I say a bit more.

First look at the background data:

ID UID username IP Logintime Logoutime
1 1 00010001 127.0.0.1 1434679452651 1435021823460


Then we get the raw data through the SQL statement

<select id="getMemLoginfoList" resultType="hashmap" parameterType="map">select m2.uid,        convert(m2.username,char) username,        m2.ip ip,        m2.logintime logintime,        m2.logoutime logoutime,        (m2.logoutime-m2.logintime) onlinetime        from loginfo</select>


And then we switch through the controller.

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 StaticStringFormattimeinterval(String time) {LongTimeInterval = Long.parselong (time);LongDay =0;Longhour =0;LongMin =0;LongSEC =0; Day = TimeInterval/( -* -* -* +); Hour = (TimeInterval/( -* -* +)-Day * -); Min = ((TimeInterval/( -* +))-Day * -* --Hour * -); SEC = (TimeInterval/ +-Day * -* -* --Hour * -* --min * -); StringBuilder result =NewStringBuilder ();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 ("Min"); }if(Sec >0) {Result.append (SEC); Result.append ("Seconds"); }returnResult.tostring (); }


The front end is displayed as


This form of treatment is more casual.

Summary : I have always wanted to write a custom jstl tag, but it is more troublesome to use, so I recommend the above three ways to everyone.

DWZ: Database-to-JSP data conversion

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.