這段時間都是在做pc端的業務,結果經理找到我說,可能要做移動端的應用,后臺邏輯還是用我寫的邏輯,但是需要改動一些,看看怎么處理。
由于移動端和pc端還是稍微有些區別的,我覺得最好是在一個地兒統一判斷,而且不要改動原先的代碼,這樣可以從一定程度上減少bug的數量。我的想法是首先應該判斷當前請求是否為移動端,然后設一個標識到session中,然后就可以隨便處理了。不管你是單獨處理,還是統一處理,直接讀取session就可以做相應的判斷了。
我封裝成了一個類,現在分享給大家:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package com.tgb.util; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 檢測是否為移動端設備訪問 * * @author : * @group : * @Version : * @Date : */ public class CheckMobile { // \b 是單詞邊界(連著的兩個(字母字符 與 非字母字符) 之間的邏輯上的間隔), // 字符串在編譯時會被轉碼一次,所以是 "\\b" // \B 是單詞內部邏輯間隔(連著的兩個字母字符之間的邏輯上的間隔) static String phoneReg = "\\b(ip(hone|od)|android|opera m(ob|in)i" + "|windows (phone|ce)|blackberry" + "|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp" + "|laystation portable)|nokia|fennec|htc[-_]" + "|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b" ; static String tableReg = "\\b(ipad|tablet|(Nexus 7)|up.browser" + "|[1-4][0-9]{2}x[1-4][0-9]{2})\\b" ; //移動設備正則匹配:手機端、平板 static Pattern phonePat = Pattern.compile(phoneReg, Pattern.CASE_INSENSITIVE); static Pattern tablePat = Pattern.compile(tableReg, Pattern.CASE_INSENSITIVE); /** * 檢測是否是移動設備訪問 * * @Title: check * @Date : * @param userAgent 瀏覽器標識 * @return true:移動設備接入,false:pc端接入 */ public static boolean check(String userAgent){ if ( null == userAgent){ userAgent = "" ; } // 匹配 Matcher matcherPhone = phonePat.matcher(userAgent); Matcher matcherTable = tablePat.matcher(userAgent); if (matcherPhone.find() || matcherTable.find()){ return true ; } else { return false ; } } } |
使用方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
/** * 檢查訪問方式是否為移動端 * * @Title: check * @Date : * @param request * @throws IOException */ public boolean check(HttpServletRequest request,HttpServletResponse response) throws IOException{ boolean isFromMobile= false ; HttpSession session= request.getSession(); //檢查是否已經記錄訪問方式(移動端或pc端) if ( null ==session.getAttribute( "ua" )){ try { //獲取ua,用來判斷是否為移動端訪問 String userAgent = request.getHeader( "USER-AGENT" ).toLowerCase(); if ( null == userAgent){ userAgent = "" ; } isFromMobile=CheckMobile.check(userAgent); //判斷是否為移動端訪問 if (isFromMobile){ System.out.println( "移動端訪問" ); session.setAttribute( "ua" , "mobile" ); } else { System.out.println( "pc端訪問" ); session.setAttribute( "ua" , "pc" ); } } catch (Exception e){} } else { isFromMobile=session.getAttribute( "ua" ).equals( "mobile" ); } return isFromMobile; } |
在登錄的時候,或者在action的execute中調用這個方法,不用改動原先的業務邏輯,即可判斷請求的是否為移動端,然后再根據結果去做相應處理,應該就簡單多了。
其實我在做的過程中,還是遇到了一個比較頭疼的問題。經理說移動端的應用要求使用json格式,所以我想統一做一個處理,如果是從移動端訪問,那么就把request和session中設置的Attribute全部讀取到map或者list中,然后再轉化為json格式輸出。想法的美好的,結果有點小殘酷。就拿登錄來說,登錄以后要跳轉到list.jsp頁,結果現在直接輸出list中的數據了,但是頁面沒有跳轉。頁面跳轉和返回json是沖突的。輸出json的話,輸出流就會關閉,不讓再跳轉,否則會提示錯誤。不知道大家有沒有好的解決方案,如果不行的話,只能每個請求單獨處理了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/xiaoxian8023/article/details/37527133