Navigating arrays of intricate items in Javascript/JSP

This is my server-side class

public class DefinitionT implements java.io.Serializable {

private int id;
private String value;
    .....

The two fields in the class have getters and setters. On the JSP side, I have a variable declared as

    <script type="text/javascript">
    var g_definitions="<s:property value="definitions"/>";
</script>

In my Struts Action, there is a getter method called getDefinitions which returns a list of DefinitionT objects.
In my JavaScript code, I have the following snippet:

    for (var i = 0; i < 2; i++) { 
    $("#definitionsDiv").append(g_definitions[i].value);
}

Initially, I was attempting to determine the number of objects in the list but g_definitions.size is showing "Undefined". When trying to access g_definitions[0].value, it also shows undefined. How should I appropriately handle this list?

Answer №1

It is not possible to directly assign a Java list to a JavaScript variable.

You have two choices:
1. Convert the Java list to a JSON object.
2. Use JSTL or snippets to retrieve the value from the variable.

<s:iterator value="definitions" var="definition">
       $("#definitionsDiv").append(<s:property value="definition"/>);
</s:iterator>

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

Using a key as an argument in the `map` function invocation

I keep getting a warning message stating that each child in the array does not have a unique key. const ITEMS = [ { "name": "apple", displayName: "Apple" "name": "orange", displayName: "Orange" }, { "name": "banana", di ...

Array fed with Mongoose query

Currently experiencing challenges with a section of my code. I am utilizing Mongoose to retrieve data from my database, where I intend to push a portion of that data into an array titled 'authors' for further manipulation. Although I am able to ...

How would you achieve the same thing using just plain JavaScript?

$('.li').prepend('<span class="pictos">j</span>'); ...

What determines the node that is returned when a selector in a querySelector call matches multiple nodes?

I encountered a scenario where I needed to focus on either an input tag, if it was present, or its container if it wasn't. To tackle this issue, I devised a clever solution: document.querySelector('.container input, .container').focus(); I ...

What are the potential drawbacks of utilizing setState within the componentDidUpdate method in React?

I am facing a situation where I need to update a child component's state whenever a prop changes. Here is how I have approached it: componentDidUpdate(prevProps) { const { searchValue, searchCriterion } = this.props; if (searchValue !== prevP ...

Generating a Transform stream with ExcelJS to produce xlsx files

Currently, I am utilizing the ExcelJS module and creating a wrapper to suit my specific needs. This wrapper implements the Transform Stream API, and surprisingly, the node version being used is 0.10.40. The ExcelJS module offers a stream API, and based on ...

Divide material-ui toolbar into separate left and right sections

Is there a way to split the material-ui toolbar into a left and right part? I want to display the numSelected on the left side of the toolbar, and the delete button and edit button on the right side. Currently, my output shows these buttons just beside t ...

Determine if an object is already present in a JSON array by comparing their respective IDs

I have a shopping cart stored in JSON format. [{"tuote":{"id":"2","name":"Rengas 2","count":16,"price":"120.00"}},{"tuote":{"id":"1","name":"Rengas 6","count":"4","price":"25.00"}},{"tuote":{"id":"4","name":"Rengas 4","count":"4","price":"85.00"}}] Form ...

Is there a way for me to fetch the attribute value of a checkbox with JavaScript?

Is there a way to extract the attribute values of a checkbox using javascript? I am implementing a javascript function within the onclick() event. Although within that function I can determine whether the checkbox is checked, I am unable to retrieve its ...

Generating objects dynamically using an array of paths in dot notation

Is it possible to dynamically generate an object with an array of strings in dot notation? The idea is to construct a JSON object from a CSV file, filter the properties, and create a new JSON object. Let's say we want to pass something like this... ...

Unable to retrieve an image file from my Node.js server using my frontend application, which are both independent of each other

I am currently running my nodejs backend on localhost:8080 and frontend on localhost:8081 using http-server. I am facing an issue where I am unable to download a file from the server side to the client side. Since I am new to node js, I am encountering som ...

Starting a line series from the beginning of the y-axis on a bar chart using chart.js

We have a new request from the business regarding the implementation of chart.js. Take a look at the image below, which shows a combination of bar and line charts. The line chart contains only a few data points. https://i.sstatic.net/mCSlR.png Within th ...

JavaScript Data Types in Node.js

Currently diving into the world of Node.js. Can someone clarify the meaning of the following code snippet? And also, what data type is being utilized here and what are its practical applications? var x = { a = { n: 0 } }; ...

Using a JavaScript file within a webpage that has already been loaded

Seeking assistance as I encounter a dilemma: providing code would be overwhelming, but perhaps someone can assist me in brainstorming a solution. Here's the issue: I have an index.php file with a div that dynamically loads (via jQuery .load()) another ...

What sets apart object destructuring from destructuring assignment?

Consider this scenario where we have an object: let obj = { a: 1, b: 2 } let { a, b } = obj; console.log(a, b); // output 1, 2 Now, let's examine a different case where 'a' and 'b' are already initialized: let obj = { a: 1, b: 2 ...

``There seems to be an issue with the ng-click event

I am a novice in Angular and had everything working well when it was all in one file. Now, as I attempt to use partials, I am encountering issues. Below is my index page: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" ng-app="JBenchAp ...

In React, is it typical to maintain identical values in both state and ref?

When working with my React app, I encountered a situation where I needed to access state values inside setTimeout() and setInterval(). However, due to closures being bound to their context once created, using state values in these functions would not refle ...

Troubleshooting: Missing MapState in Vuex4 for Vue3 within an MVC project

Within my MVC project, I have successfully integrated Vue3 with Vuex4. However, I have encountered an issue specifically related to the mapState function. Upon using the following import statements, an error is triggered: import Vue from 'vue'; ...

Extracting data from a JSON object using Angular

Recently, I've been delving into the world of AngularJS and encountered a hurdle with LocalStorage. After spending numerous hours trying to figure out how to save data locally, I believe I have finally got it working as intended. Now, my next challeng ...

How to send a JavaScript variable to Flask and trigger an AJAX reload action

Introduction: I have explored similar inquiries and attempted to apply relevant code/concepts but without success. -https://stackoverflow.com/questions/43645790/passing-javascript-variable-to-python-flask -https://stackoverflow.com/questions/10313001/is- ...