简体   繁体   中英

Set JSTL condition from a JSP scriptlet, then pass to java script

Ok. I am not sure if this is really the goal of my question, but I am really trying to understand how to solve my problem. I keep running into hurdles. I am trying to pass a Boolean variable from a scriplet to JSTL, then call a js function to read whether its true or false. Here is what I have so far:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!--Take out after testing --> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head></head>
<body>
<form name="myForm" action="test.html">
----
<% if (myJavaFunction.FormIsChanged() && myJavaFunction.ErrorCount()>0) {%>
<br />
<%@include file="/countallerrors.jsp"%>
<br />
<% } %>
<c:set var="ErrorPresent" value="false"/>
</form> 
</body>
</html>

Basically my question is how can I set my "ErrorPresent" variable to be equal to (myJavaFunction.FormIsChanged() && myJavaFunction.ErrorCount()>0) ?

I am new to JSTL and after some research today, I found that its not in good practice to use scriptlets, so I would like to try and see if I can do this with JSTL. Once I get the JSTL Boolean variable to function, I would like to pass to a java script function, and this function will determine if the form has errors before it is submitted. Any direction would be greatly appreciated.

Thanks, Naina

You're correct that it's typically not great practice to use scriptlet--it's better to code up specific tags to encapsulate business logic. That helps enforce separation of presentation from control code.

But to accomplish what you want, try this:

<% boolean errorPresent = myJavaFunction.formIsChanged() && myJavaFunction.errorCount()>0; %>

The variable errorPresent is now available to your page's scope, and can be referenced using normal JSTL interpolation, like <p>errorPresent: ${errorPresent}</p>

On another stylistic note: Java style is for variable and method names to be camelCased with initial lowercase. Class names should be MixedCase with initial capital letter. My sample above corrects that. (Constants should be ALL_CAPS_WITH_UNDERSCORES )

我认为您正在寻找的答案是:

<c:set var="ErrorPresent" value="${(myJavaFunction.FormIsChanged() && myJavaFunction.ErrorCount()>0)}"/>

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