2014-11-24 38 views
0

现在我有2个以下JavaScript函数:如何将条件包含到组合的JSP和JavaScript函数中?

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

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

如何有一个组合的功能,所以只有当request.getSession()的getAttribute( “stickyCarrier”)= NULL 做到以下几点:!

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

否则什么都不做。

编辑:

谢谢Bhaskara!

但实际上它比我表现出更多的,它可能会进入一个无限循环,因为在一个重定向:

@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; 
} 

而且我在<body onload=...>

+0

弗兰克,我在你的新编辑后更新了我的答案。我的理解是用户点击链接,然后检查是否有会话属性,如果有会话属性,请将其删除并将用户发送到/billingSearch.html。如果我的理解错误,请纠正我 – Bhaskara 2014-11-26 17:45:38

回答

1

请注意,调用clearBillingCache()验证码:

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

}

将被渲染为

function clearBillingCache_1(){ 

    } 

您可以在浏览器中执行“查看源代码”来检查此问题。这意味着无论如何将无论如何设置脚本中定义的会话属性,而不管条件如何。我个人建议你不要使用Scriptlets。你可以使用JSTL来做到这一点。这个想法是用于条件检查和设置属性和一个JavaScript代码只是为了重定向。 JSTL:

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

     </c:if> 

,然后你的JavaScript:

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

编辑:

好弗兰克。所以还有一些更改需要。您正在从控制器/ servlet重定向。请按照下面的步骤:

  • 更改控制器代码如下:

    @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; 
    
  • 删除的JavaScript函数:clearBillingCache_1()clearBillingCache()

  • 上的HTML清除体内的onload调用
相关问题