Utilizing JSTL: Executing a function within <script> through the c:set tag in JSTL

Can someone help me with this code snippet?

<c:set var="cls" value="${myFunction(param)}"/>

.....

<script>
    function myFunction(param) {
        if(param == true) {
            return "aaa";
        } else {
            return "bbb";
        }
    }
</script>

However, I am encountering the following error:

org.apache.jasper.el.JspELException: /script.jsp(22,12) '${myFunction()}' Function [:myFunction] not found

Answer №1

Is it possible to achieve what you are attempting?

The simple answer is NO...

Let's explore the reason behind this. Take a clue from JSTL (JavaServer Pages Standard Tag Library).. notice the word 'Server'. Yes.

JSP pages undergo compilation similar to Java files. Once successfully compiled, they are sent as a response which is then rendered by the browser for display.

JavaScript: a client-side programming language where codes are executed by the browser rather than the server.

During compilation of a Jsp file, only the Scriptlet and JSTL parts are considered while the rest of the code (HTML, CSS, JS) is treated as text.

Now, imagine a scenario in your code where the Java compiler encounters myFunc, assuming it must be a method but not finding any reference.

Since Javascript functions can only run on the browser side, Java cannot recognize myFunc, resulting in an unknown variable cls.

This explanation sheds light on how JSP and Server Side processes work together.

So, keeping these factors in mind, brainstorm alternative solutions to tackle your issue.

Happy Coding :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Passing an array from PHP to the DataTables JavaScript library

I am attempting to pass a PHP array to the JS Datatable library for testing purposes. Instead of fetching data from a database, I have simplified my approach. Here is a basic representation of my PHP code: $data_fmt['data'][] = array("TEST"); e ...

A recursive function that utilizes a for loop is implemented

I am encountering a critical issue with a recursive function. Here is the code snippet of my recursive function: iterateJson(data, jsonData, returnedSelf) { var obj = { "name": data.groupName, "size": 4350, "type": data.groupType }; if ...

Navigating through the endless scroll behavior in Twitter using CasperJS (PhantomJS)

Having trouble with infinite scrolling on Twitter, as the page is not loading new content even when scrolling to the bottom. To test if any content loads at all, I have used the following code snippet: casper.open('https://twitter.com/<account> ...

Tips for determining whether a value is present in an array or not

I'm trying to prevent duplicate values from being pushed into the selectedOwners array. In the code snippet below, the user selects an owner, and if that owner already exists in the selectedOwners array, I do not want to push it again. How can I imple ...

While attempting to upload a file in my Mocha Node.js test, I encountered the message: "Received [Object null prototype] { file: { ... }}"

Everywhere I find the solution in white : app.use(bodyParser.urlencoded({extended: true})); I can use JSON.stringify(req.files) However, I am sure there is a way to fix my problem. My Mocha test : it('handles file upload', async function () { ...

Bringing the value from the codebehind to the jquery function on the client side

Here is the code snippet from my asp.net web application's code behind where I have integrated another web service: [WebMethod] public static string GetData(string name) { WEBSERVICE.Service1 Client = new Service1(); string Nam ...

React automatic scrolling

I am currently working on implementing lazy loading for the product list. I have created a simulated asynchronous request to the server. Users should be able to update the page by scrolling even when all items have been displayed. The issue arises when ...

How to handle an unexpected token error when using a function within another function in

I'm encountering an issue where the function below is placed above my render function. It doesn't seem to allow me to nest a function within another function. Can you help me identify what's causing this problem? onSelected(i){ this.st ...

"What is the best way to change the props of a React component that has already been rendered from App.js

My React component, MoviesGallery.js, is set up with the following structure: class MoviesGallery extends Component { constructor(props) { super(props) this.state = { currentImage: 0 }; this.closeLightbox = this.closeLightbox. ...

Use Express JS to enable users to upload their profile picture either during account creation or on the registration form

Just starting out with node js, I'm working on a basic app where users can create accounts with their details. I've implemented mongo dB as the backend to store user information such as names and emails. Once an account is created, users are dir ...

The process of updating the value of an element in local storage with JavaScript

Most of the time we have an object stored in our localStorage. let car = { brand: "Tesla", model: "Model S" }; localStorage.setItem("car", JSON.stringify(car)); https://i.sstatic.net/qWEkE.png I am now eager to update the value of "model". H ...

What is the best way to turn off autocorrect in a textarea on IE11 without turning off spellcheck?

In my experience, disabling the spellcheck attribute seems to solve the auto-correct issue, but it also eliminates the underlining of misspelled words. <textarea id="TextArea1" spellcheck="false"></textarea> I prefer to keep spellcheck enabl ...

ERROR: JSON parsing failed due to an unexpected token "<", indicating an issue with the syntax and structure of the input data

Currently, I am following a tutorial on Scrimba to learn about React and React Router 6. Unfortunately, I have encountered an error with the data provided in the tutorial. The error message reads as follows: 67:1 Uncaught (in promise) SyntaxError: Unexpect ...

Observing data retrieved via ajax in Internet Explorer 9

Is there a way to view data returned by an ajax script in Internet Explorer 9? For instance, Firefox has the Firebug 'All' tab with a 'Response' Sub Tab that displays data returned by an Ajax Script. How can this be done in Internet Ex ...

Creating Child Components Dynamically using String Names in ReactJS

I've encountered an issue with dynamically rendering React Components within my DashboardInterface component. I have an array filled with string names of React Components, retrieved through an external mechanism. The goal is to render these Components ...

Creating a new session in Node.js for every request

Our team is currently developing a nodejs application that requires the maintenance of sessions. We have implemented express to handle node variables, but we are facing an issue where a new session is created every time we hit a new service. The structure ...

"The sliding function of the React Bootstrap carousel is malfunctioning as it goes blank just before transitioning

Here is the code snippet I am working with: Whenever the carousel transitions to the next image, the current image disappears before displaying the next one. I am using react-bootstrap version 5.1.0, but it seems like there may be an issue with the transi ...

Month and year selection feature in ExtJS 4 catalog

I recently came across an interesting Fiddle that featured a Month and Year picker for apps. After finding this Fiddle here, I noticed that it was built using Extjs 5.0, and it worked perfectly. However, when attempting to switch it to version 4.2.1, the l ...

"JavaScript issue: receiving 'undefined' when trying to retrieve input

This code snippet is for a web app that tracks the number of losses in a game. The problem arises when trying to retrieve the value, which returns undefined. Every time I reference the username variable, it returns undefined. document.addEventListener(&a ...

Is it possible in Angular.js to limit the visibility of a service to only one module or to a specific group of modules?

When working with Angular.js, the services declared on a module are typically accessible to all other modules. However, is there a way to restrict the visibility of a service to only one specific module or a selected group of modules? I have some service ...