Use Java within Javascript -


i have jsp page looks following:

what intent is, current environment via system variable , generate correct url environment.

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>  <% string sysenv = ""; if(system.getproperty("env.name").equals("test")) {     sysenv = "test"; }  %>  <!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd"><html> <head>     <%@ include file="/includes/common.jsp" %>     <script type="text/javascript" src="<c:out value="${requestscope.scriptslocation}"/>/scripts/general.js"></script>      <c:if test="${requestscope.isfeed == true}">         <jsp:include page="/includes/someinclude.jsp"/>         <script src="http://www.myothersite.com/feed/d/some.js" type="text/javascript"></script>          <script type="text/javascript">         var environ = <%=sysenv%>;          if(environ == 'test'){                 var theurl = 'http://mywebsite.com?isfeed=true&env=test';         } else {         var theurl = 'http://mywebsite.com?isfeed=true';         }         ...      </script>  ... 

i think i'm supposed have tags-logic import looping i'm trying achieve in java, not working.

since you're in jsp why not (roughly) this:

<script>   var url = 'http://mywebsite.com?isfeed=true';   if ('<%= system.getproperty("env.name") %>' === 'test') {     url += '&env=test';   }   // etc. 

personally i'd move logic out of view , create request-scoped attribute in filter, part of struts 1 request processor, etc. since scriptlets bad. gives opportunity set @ runtime.

in addition, url generation should also moved out of view layer.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

android - Keyboard hides my half of edit-text and button below it even in scroll view -