简体   繁体   English

检查变量是否为真

[英]checking whether the variable is true or not

 function add(){
        <%if(empRecNum != null && !(empRecNum.equals("")))
    {
       empSelected=true;
    }
    boolean canModify = UTIL.hasSecurity("PFTMODFY") && empSelected;

     %>
    df('ADD');        

    }

When i click on add, i need to check whether the empSelected is true or not and pass this canModify value. 当我单击添加时,我需要检查empSelected是否为true并传递此canModify值。 Will this be called? 会叫吗?

Is this right way i am checking a Scriptlet inside a JavaScript 这是我检查JavaScript中的Scriptlet的正确方法吗

You need to get the following concept right: Java/JSP runs at webserver and produces HTML/CSS/JS output. 您需要正确理解以下概念:Java / JSP在Web服务器上运行并产生HTML / CSS / JS输出。 Webserver sends HTML/CSS/JS output to webbrowser. 网络服务器将HTML / CSS / JS输出发送到网络浏览器。 Webbrowser retrieves HTML/CSS/JS output and displays HTML, applies CSS and executes JS. Webbrowser检索HTML / CSS / JS输出并显示HTML,应用CSS并执行JS。 If Java/JSP has done its job right, you should not see any line of Java/JSP code in webbrowser. 如果Java / JSP正确完成了工作,则webbrowser中应该看不到任何Java / JSP代码行。 Rightclick page in webbrowser and choose View Source . 右键单击Web浏览器中的页面,然后选择“ 查看源代码” Do you see it, right? 你看到了吧?

The webbrowser has totally no notion about the Java/JSP code on the server side. Web浏览器完全不了解服务器端的Java / JSP代码。 All it knows about and can see is the HTML/CSS/JS code it has retrieved. 它所知道并能看到的只是它检索到的HTML / CSS / JS代码。 The only communication way between webbrowser and webserver is using HTTP requests. Web浏览器和Web服务器之间的唯一通信方式是使用HTTP请求。 In the webbrowser, a HTTP request can be fired by entering URL in address bar, clicking a (bookmark) link, pressing a submit button or executing XMLHttpRequest using JavaScript. 在Web浏览器中,可以通过在地址栏中输入URL,单击(书签)链接,按下提交按钮或使用JavaScript执行XMLHttpRequest来触发HTTP请求。 In the webserver, the Java/JSP (and Servlet) code can be configured so that it executes on certain URL's only. 在Web服务器中,可以配置Java / JSP(和Servlet)代码,使其仅在某些URL上执行。 Eg a JSP page on a certain location, a Servlet which is mapped on a certain url-pattern , etcetera. 例如,某个位置的JSP页面,映射到特定url-pattern等的Servlet。

In a nutshell, to have JavaScript to access Java/JSP variables, all you need is to let Java/JSP print them as if it is a JavaScript variable. 简而言之,要使JavaScript访问Java / JSP变量,您需要做的就是让Java / JSP将它们打印为好像是JavaScript变量一样。 To have JavaScript to execute Java/JSP methods, all you need is to let JavaScript fire a HTTP request . 要使JavaScript执行Java / JSP方法,您需要做的就是让JavaScript 触发HTTP请求

See also: Communication between Java/JSP/JSF and JavaScript 另请参阅: Java / JSP / JSF与JavaScript之间的通信

A more elagant way to do this 一种更愚蠢的方法

var canModify = Boolean(${canModify});

Use jstl el, it turns more clear what do you intend to do. 使用jstl el可以更清楚地知道您打算做什么。 The call to boolean will convert the given value in javascript boolean. 对boolean的调用将转换javascript boolean中的给定值。

Remember: 记得:

Boolean(true); // returns true
Boolean(false); // return false
Boolean(); // returns false

The canModify value defined in JSP is never passed to JavaScript. JSP中定义的canModify值永远不会传递给JavaScript。 You need to redefine the variable in JavaScript, for example: 您需要在JavaScript中重新定义变量,例如:

<%
if (canModify) {  // This is the JSP variable
%>
  var canModify = true;  // This is the JavaScript variable
<%
} else {
%>
  var canModify = false;
<%
}
%>

On a different note, you should abandon JSP scriptlets and switch to JSTL. 另外,您应该放弃JSP scriptlet并切换到JSTL。

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

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