Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struts2 how to create multiple views for mobile and desktop

Tags:

struts2

I am creating a site which will be accessible via mobile and desktop devices. So I want to create 2 views of my application. My action code and everything else in the backend (manageers, DAOs) is same. Just JSP changes for both.

How I can do this via Struts 2?

like image 583
Gagan Avatar asked Jul 15 '11 12:07

Gagan


2 Answers

In struts there are many way to obtain the same thing. In this case, the one I prefer is: You could write an interceptor that changes the return code based on the user-agent of the client, such that there would be versions for PC and mobile of each jsp. In your configuration you need to have all the result codes for all jsp (or you could simply define the result through the wildcard mapping). For example: change the result code from "success" to "mobile_success". In case you want map both results in the same jsp you can map, as I said before, in this way <result name="*success">

like image 165
Maurizio Cucchiara Avatar answered Oct 24 '22 05:10

Maurizio Cucchiara


not sure whether there is library for automating such task for struts 2. but if there is, using such libraries might be better

anyway, here is the theory. every browser has its own "signature" written in the request header, called "User-Agent". different browser (supposedly) has different user agent. for example, my firefox user agent is as following: Mozilla/5.0 (Windows NT 6.0; rv:5.0) Gecko/20100101 Firefox/5.0 FirePHP/0.5

basically, by detecting the user agent, you can know what browser is used to access your site. the list of mobile browser user agents can be found in http://www.zytrax.com/tech/web/mobile_ids.html

if i'm not wrong, you can retrieve the user agent in server by httpServletRequest.getHeader("User-Agent"); (correct me if i'm wrong)

you can then create an interceptor which will decide whether a client is from mobile or from desktop. that interceptor can return different result for different client type. for example, if the client is desktop, you can return "successDesktop" and if the client is mobile, you can return "successMobile".

well, hopefully someone else can come up with (far) easier solution

like image 25
fajrian Avatar answered Oct 24 '22 03:10

fajrian