1

In my JSP page, I am including another JSP and I want to restrict it based on the view of source page either mobile or web.

<%@include file="/templates/jsp/header/search.jsp" %>

This is my include statement and I have a JavaScript function which determines whether is a desktop view or not. Below is my JS function:

function isDesktopView(){
    if (bootstrapEnv == "lg" || bootstrapEnv == "md") {
        return true;
    }
}

I was trying to something like

<s:if test = "%{isDesktopView()}">
    <%@include file="/templates/jsp/header/search.jsp" %>
</s:if>
uladzimir
  • 5,639
  • 6
  • 31
  • 50
ThatMan
  • 158
  • 1
  • 4
  • 18
  • 2
    You tried to run a Javascript (Client side) in a Jsp tag (Server side). This is the most common mistake. JSP only read JSP. Javascript only runs on Cilent side, were there is no JSP anymore. But I think you can use the request to check if the request come from a mobile explorer – AxelH Sep 29 '16 at 11:05
  • Possible duplicate of [How to detect mobile (iOS and Android) using JSP/Java?](http://stackoverflow.com/questions/6770572/how-to-detect-mobile-ios-and-android-using-jsp-java) – AxelH Sep 29 '16 at 11:06

1 Answers1

0

As @AxelH stated in the comments, you are mixing JS and JSP. JSP run server-side before the page is sent to the browser, JS is executed client side by the browser. If you want the page included server side, you may need to tack on a parameter to the request that is sent (mypage/home?isDesktopView=true). In the Controller, you would add the parameter to the view and then you could use the code you had to determine which header to include.

<s:if test = "%{isDesktopView()}">
    <%@include file="/templates/jsp/header/search.jsp" %>
</s:if>

Another solution would be to check the screen size when the page is loaded (JS), then load the correct content into a div. This would mean two separate requests, but gets the job done. Although, if it is the page header you are trying to include, I would not recommend this.

$(function() {
    if(bootstrapEnv == "lg" || bootstrapEnv == "md") {
        $('#myDiv').load('/templates/jsp/header/search');
    }
});
Shaggy
  • 1,444
  • 1
  • 23
  • 34