最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - checking whether the variable is true or not - Stack Overflow

programmeradmin5浏览0评论
 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

 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

Share Improve this question asked Oct 25, 2010 at 14:24 JohnJohn 1,1893 gold badges19 silver badges29 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

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

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 munication 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. E.g. 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

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.

发布评论

评论列表(0)

  1. 暂无评论