Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7544889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:47:23+00:00 2026-05-30T08:47:23+00:00

I want to access a model attribute in Javascript. I use the following code:

  • 0

I want to access a model attribute in Javascript. I use the following code:

model.addAttribute("data", responseDTO);

My DTO class:

public class ResponseDTO {

    private List<ObjectError> errors;

    private Boolean actionPassed;

    private String dataRequestName;

    // and setter getter for all fields
}   

I tried accessing the DTO using:

var data = "${data}";

But it is giving me a string representation of responseDTO instead, i.e com.req.dto.ResponseDTO@115f4ea. I can successfully access a field inside the DTO using:

 var data = "${data.actionPassed}";  

But this is not working for the errors attribute inside the DTO, as it is a List of ObjectError. How can I get complete responseDTO object in Javascript?

Thanks!


EDIT :

Initially I was using jquery.post

$.post('ajax/test.html', function(data) {
  // Here I was able to retrieve every attribute even list of ObjectError.
});

Now I want to remove Ajax and want to convert it into non-ajax approach (because of some unavoidable reasons). So I am doing a normal form submit and want load same form again and trying to load data model attribute in Javascript so that I can keep the rest of the code as it is.
I was wondering if it can be achieved in Javascript as it is doable using Jquery post?


EDIT 2 :

I tried (Thank you @Grant for suggestions)

JSONObject jsonObject =JSONObject.fromObject(responseDTO);
String jsonString = jsonObject.toString();
model.addAttribute("data",jsonString);    

and in Javascript

var data = eval('('+ ${dataJson} +')');   // Getting error on this line  
alert(data.actionPassed);   

But getting error and no alert is displayed
Error :
enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:47:25+00:00Added an answer on May 30, 2026 at 8:47 am

    First of all, there’s no way to convert a Java object to a Javascript object directly since they have nothing to do with each other. One is server-side language and the other is client-side language.

    So to accomplish this goal, you have to do some convertion. I think you have two options:

    1. Convert ResponseDTO object to JSON string and pass it to jsp and you may get the javascript object directly.
    2. Pass ResponseDTO object to JSP and populate the javascript object as what you are trying now.

    For option #1, you should use a library to generate JSON string by the Java object. You can use this one JSON-lib.
    e.g:

    JSONObject jsonObject = JSONObject.fromObject( responseDTO );  
    /*  
      jsonStr is something like below, "errors" represents the List<ObjectError>
      I don't know what's in ObjectError, errorName is just an example property.
      {
        "dataRequestName":"request1",
        "actionPassed":true,
        "errors":[{"errorName":"error"},{"errorName":"unknown error"}]
      } 
    */
    String jsonStr = jsonObject.toString();
    model.addAttribute("dataJson", jsonStr);  
    
    /*In JSP, get the corresponding javascript object
     by eval the json string directly.*/
    <script>
    var data = eval('('+'${dataJson}'+')'); 
    </script>
    

    For option #2,

    //Pass java object as you do now
    model.addAttribute("data",responseDTO);
    
    //In JSP, include jstl taglib to help accessing List.
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    <script>
    var errorArr = [], errorObj;
    <c:forEach var="error" items="${data.errors}">
        errorObj = { errorName: '${error.errorName}' };
        errorArr.push(errorObj);                                  
    </c:forEach>
    
    //Populate the corresponding javascript object.
    var data = {
      dataRequestName: '${data.dataRequestName}',
      actionPassed: ${data.actionPassed},
      errors: errorArr
    };
    </script>
    

    As you can see, option #2 is complicated and only useful if the Java object is simple while option #1 is much easier and maintainable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to access a MySQL database directly from JavaScript code in an HTML
i want to access a integer and a string from a class to all
How do I access a model's parent's attribute in a form? For example, for
I have the following rails model: class Customer < ActiveRecord::Base attr_accessible :firstname, :lastname, :email,
I need to access attribute names of a model but it is not showing
I want to implement an Observer of a Model class which does not change
Here's my code: class Publisher(models.Model): name = models.CharField( max_length = 200, unique = True,
I have a model class that has an AreDuesPaid property that I want only
What I want to do is the following: I have a model called Control:
I want to access the call stack at runtime in a Native C++ application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.