简体   繁体   English

如何将条件包含在组合的JSP和JavaScript函数中?

[英]How to include a condition into a combined JSP and JavaScript function?

Now I have 2 following JavaScript functions : 现在,我有2个以下JavaScript函数:

function clearBillingCache(){
    window.location = "billingSearchClear.html"
}

function clearBillingCache_1(){
<%
    request.getSession().setAttribute("stickyCarrier", null);
    request.getSession().setAttribute("stickyAgency", null);
%>
}

How to have a combined function, so only when request.getSession().getAttribute("stickyCarrier") != null do the following : 如何具有组合功能,因此仅当request.getSession()。getAttribute(“ stickyCarrier”)!= null时,才可以执行以下操作:

<%
  request.getSession().setAttribute("stickyCarrier", null);
  request.getSession().setAttribute("stickyAgency", null);
%>
window.location = "billingSearchClear.html"

otherwise do nothing. 否则什么都不做。

Edit : 编辑:

Thanks Bhaskara ! 谢谢Bhaskara!

But actually it's more than what I showed, it might get into an infinite loop, because there is a redirect in : 但是实际上它比我展示的要多,它可能会陷入无限循环,因为其中存在重定向:

@RequestMapping(value = "/billingSearchClear.html", method = RequestMethod.GET)
public String clearCache(HttpServletRequest request) {
    String returnVal = "redirect:/billingSearch.html";

    request.getSession().setAttribute("stickyCarrier", null);
    request.getSession().setAttribute("stickyAgency", null);

    return returnVal;
}

And I'm calling clearBillingCache() in <body onload=...> 我在<body onload=...>调用clearBillingCache()

Please note that the code: 请注意代码:

   function clearBillingCache_1(){
<%
    request.getSession().setAttribute("stickyCarrier", null);
    request.getSession().setAttribute("stickyAgency", null);
%>

} }

will be rendered as 将呈现为

function clearBillingCache_1(){

    }

You can check this by doing a "View Source" in your browser. 您可以通过在浏览器中执行“查看源代码”来进行检查。 Which means the session attributes defined in the scriptlets will anyway be set irrespective of the condition. 这意味着无论情况如何,都将设置在脚本中定义的会话属性。 I personally recommend you not to use the Scriptlets. 我个人建议您不要使用脚本。 You can use JSTL to do this. 您可以使用JSTL来做到这一点。 The idea is to use for conditional check and for setting the attributes and a Javascript code just to redirect. 这个想法是用于条件检查,设置属性和仅用于重定向的Javascript代码。 JSTL: JSTL:

<c:if test="${stickyCarrier != null}">
            <c:set var="stickyCarrier" value="null" scope="session"  />
            <c:set var="stickyAgency" value="null" scope="session"  />

        </c:if>

and then your javascript: 然后你的JavaScript:

function clearBillingCache(){
    window.location = "billingSearchClear.html"
}

Edit: 编辑:

Ok Frank. 好的,弗兰克。 So there is some more change required. 因此,还需要进行一些更改。 You are being redirected from a controller/servlet. 您正在从控制器/ servlet重定向。 Please follow the below steps: 请按照以下步骤操作:

  • Change your controller code to this: 将您的控制器代码更改为此:

     @RequestMapping(value = "/search.html", method = RequestMethod.GET) public String clearCache(HttpServletRequest request) { String returnVal = "redirect:/billingSearch.html"; if(request.getSession().getAttribute("stickyCarrier") != null) { request.getSession().setAttribute("stickyCarrier", null); request.getSession().setAttribute("stickyAgency", null); } return returnVal; 
  • Remove the javascript functions : clearBillingCache_1() and clearBillingCache() 删除javascript函数: clearBillingCache_1()clearBillingCache()

  • Remove body onload call on the html 删除HTML上的body onload调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM