简体   繁体   中英

Is it a good practice to use JSTL inside a script (javascript) tag?

I'm developing a web app using JSTL and Javascript in Eclipse Juno. I've been reading questions like How to set the JSTL variable value in javascript? and my code works good even if I have error in eclipse:

在此输入图像描述

But... Is it a good practice to use JSTL and Javascript like this?

Does it cause a low performance in the time of rendering the webpage?

Can this be done in other way?

Is it a good practice to use JSTL and Javascript like this?

It is not bad practice or good practice. The bad practice would be using JSTL to control the flow of JavaScript, which is plain wrong because JSTL runs on server side and JavaScript on client side.

Does it cause a low performance in the time of rendering the webpage?

JSTL will only help to generate the HTML for the current view. JavaScript is not involved in the HTML generation at server side but at client side unless you work with nodejs or similar technologies.

Can this be done in other way?

This depends on what you're doing. Common way to access to data when accessing to a web page:

  1. Application Server (AS) receives a GET request on http://www.foo.com/bar
  2. AS pre process the GET request (load data from database or another data source, pre calculations, etc)
  3. AS creates the response for the GET request (apply the data to generate the HTML)
  4. AS sends the response to the client.
  5. The browser client renders the HTML.

Another way to do it:

  1. Application Server (AS) receives a GET request on http://www.foo.com/bar
  2. AS creates the response for the GET request (generate the HTML which contains JavaScript functions to load the data in the onload event).
  3. AS sends the response to the client.
  4. The browser client renders the HTML.
  5. The onload event fires and load data in the onload event through RESTful services. This way, the data interaction is handled in client side only, but the data comes from server side.

These are two very simple alternatives to handle the same problem. Which one to choose and work with will depend entirely on your design, there's no definitive answer.

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