top | item 37652412

(no title)

chunsj | 2 years ago

Why does almost all the Java based web development framework use "template" system, instead of generating HTML from code(like Flutter/Swift)? Is this due to the inherent limitation of Java language/syntax?

discuss

order

Tainnor|2 years ago

In order to make this not extraordinarily painful, you need some good language support for internal DSLs, or a template language / preprocessor, both of which Java doesn't really have. You could still do it somehow, but it probably wouldn't be the nicest experience. Plus, historically, the Java community has been in love with XML, so you'll find lots of templating libraries based on XML.

Swift has function builders which are part of the magic that makes such DSLs practical.

In Kotlin, which also runs on the JVM, this is possible much more easily than in Java: https://kotlinlang.org/docs/typesafe-html-dsl.html

xxs|2 years ago

>instead of generating HTML from code

At the relative bottom (Servelet API level) it works that way, servlets have a direct access to input/output stream, and they have to write plain byte[] (or string via Writer).

As for java syntax, it's not great for string manipulation, encoding (you will need some functions for all the html/javascript escaping).

If you see url's containing ".do" extensions - that was the standard for calling servlets w/o anything, it has changed, of course.

andretti1977|2 years ago

Just a note about the “.do” extension: if I correctly recall, it was introduced by struts framework which used “Action” as a naming convention such as Spring uses “Controller” as suffix, and so they used “.do” as extension.

ecshafer|2 years ago

I wonder why more java frameworks don't reuse the parsing from JSP, but just drop the servlett part. Though maybe that's not as easily done as said.

xxs|2 years ago

JSP literally compiles/translates to Servlets, as in extends them in the code, and everything you see is in their 'service' method effectively. JSP can consider JSP alike to C's preprocessor.

drdec|2 years ago

JSPs are implemented by transpiling them into servlets, so JSPs without the servlets doesn't really save anything (assuming you want to re-use what's out there).