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