简体   繁体   中英

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. Will this be called?

Is this right way i am checking a Scriptlet inside a JavaScript

You need to get the following concept right: Java/JSP runs at webserver and produces HTML/CSS/JS output. Webserver sends HTML/CSS/JS output to webbrowser. Webbrowser retrieves HTML/CSS/JS output and displays HTML, applies CSS and executes JS. If Java/JSP has done its job right, you should not see any line of Java/JSP code in webbrowser. Rightclick page in webbrowser and choose View Source . Do you see it, right?

The webbrowser has totally no notion about the Java/JSP code on the server side. All it knows about and can see is the HTML/CSS/JS code it has retrieved. The only communication way between webbrowser and webserver is using HTTP requests. 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. In the webserver, the Java/JSP (and Servlet) code can be configured so that it executes on certain URL's only. Eg a JSP page on a certain location, a Servlet which is mapped on a certain url-pattern , etcetera.

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. To have JavaScript to execute Java/JSP methods, all you need is to let JavaScript fire a HTTP request .

See also: Communication between Java/JSP/JSF and 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. The call to boolean will convert the given value in 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. You need to redefine the variable in JavaScript, for example:

<%
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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