0

Short Version:

In a java web app, I can set javabean objects as requestscoped parameters, and access detailed fields, even hierarchical, within these objects through i.e. JSTL/EL while building a website.

Can I in any way access the full extent of these JavaBeans, in i.e. javascript-functions that are fired on, for instance, onclick of some elements within my web page?


Long version:

I am making a java web-app, and I am trying to learn the basics, so I am not using any frameworks like Spring or Struts, but I am building the app by the front controller pattern.

I have a page, which should be able to create new, recieve, edit and/or finally update data in my database. The database has foreign keys, and my choices in the editor should depend on the number of elements of other linked tables in the database.

I would like my editor to be able to:

  1. Create the editor menu based on secondary tables of the database (static until leaving the site)
  2. Load data from a database-element
  3. Edit data in html-elements
  4. Undo changes (which is basically repeating step 2, if data is available still)
  5. Save data, and reset editor.

Point 4 is the center of attention here. I wish to be able to do step 4 without reloading the whole page. If I am able to do this, I figure step 2 should also be executed client side, as it does the same thing, only first time. It feels like a setup like this will grant me a good seperation of creating the form itself server side, and let step 2-4 happen client side, until step 5 again requires server side action.

I am not sure how to approach this goal though, or if it is a good idea. It is only a problem, when data is loaded from the database, and I want to store that data client side. Right now I am building the form in jsp/html/jstl, and I am using requestScoped java objects to do it, through a HttpServletRequest-object from the Servlet Controller. I have been trying to use these objects in javascript functions, with limited success. I have been able to extract all data, even hierirchal object's fields, except those in collections. Unfortunately these are essential to my editor page.

I have been looking into JSON for this, but is seems like i need to do big adjustements in my java code to implement this. Is it worth it?

finally, to repeat the question: How can I access requestScoped JavaBean-objectdata to be available client side, in i.e. Javascript?

jumps4fun
  • 3,994
  • 10
  • 50
  • 96
  • Are you using mvc architecture...? – bachman Mar 21 '14 at 12:58
  • Yes. More specifically, my architecture is based on figure 5 of this [article](http://www.javaranch.com/journal/200603/frontman.html). Even more precisely, it is directly implemented as "Request(action) based MVC", as described in the answer of [this question](http://stackoverflow.com/questions/3541077/design-patterns-web-based-applications) – jumps4fun Mar 21 '14 at 13:02
  • It is not possible to use Jstl/EL inside java script but you can try using scriplets to collect the data from Java beans and invoke the appropriate handler to update the page. – bachman Mar 21 '14 at 14:50
  • I just went through your question again and found that you want it done without reloading the page. To avoid page reloading you can try Ajax. Hope it helps you ;) – bachman Mar 21 '14 at 14:52
  • You can't access beans or any other server-side objects in memory on the client side. But you can send an Ajax request to the servlet to return the bean values to javascript, if the beans are in the session scope. But if the bean is in the request scope, you can't. If its request scope and you want Javascript to have access, you have to print it out into the page. – developerwjk Mar 21 '14 at 20:43

0 Answers0