Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将条件包含到JSP和JavaScript组合函数中?_Javascript_Jsp_Conditional Statements - Fatal编程技术网

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

如何将条件包含到JSP和JavaScript组合函数中?,javascript,jsp,conditional-statements,Javascript,Jsp,Conditional Statements,现在我有以下两个JavaScript函数: function clearBillingCache(){ window.location = "billingSearchClear.html" } function clearBillingCache_1(){ <% request.getSession().setAttribute("stickyCarrier", null); request.getSession().setAttribute("stickyAg

现在我有以下两个JavaScript函数:

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

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

我正在
中调用
clearBillingCache()
,请注意代码:

   function clearBillingCache_1(){
<%
    request.getSession().setAttribute("stickyCarrier", null);
    request.getSession().setAttribute("stickyAgency", null);
%>
您可以通过在浏览器中执行“查看源代码”来检查这一点。这意味着不管条件如何,在Scriptlet中定义的会话属性都将被设置。我个人建议您不要使用Scriptlet。您可以使用JSTL来实现这一点。其思想是用于条件检查以及设置属性和Javascript代码以重定向。 JSTL:

编辑:

好的,弗兰克。因此,还需要进行一些修改。正在从控制器/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上的body onload调用

Frank,在您的新编辑之后,我更新了我的答案。我的理解是,用户单击一个链接,然后检查是否有会话属性,如果有会话属性,则删除它并将用户发送到/billingSearch.html。如果我的理解有误,请纠正我
   function clearBillingCache_1(){
<%
    request.getSession().setAttribute("stickyCarrier", null);
    request.getSession().setAttribute("stickyAgency", null);
%>
function clearBillingCache_1(){

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

        </c:if>
function clearBillingCache(){
    window.location = "billingSearchClear.html"
}
@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;