JSTL vs JSP Scriptlets

前端 未结 6 690
面向向阳花
面向向阳花 2020-12-04 13:09

I want someone to explain some points in BlausC\'s amazing answer in this question.

He said that scriptlets had some disadvantages, which are:

  1. Reusa

6条回答
  •  情话喂你
    2020-12-04 14:01

    You should not have scriptlet code in JSPs. I'd recommend 100% JSTL and zero scriplet code.

    JSPs should be purely presentation. That's the hidden benefit of writing JSPs using only JSTL, because they get all their dynamic data elsewhere. Let the service layer have the business logic and determine what data the JSP needs.

    This answers your unit testing question, too. You should not have to unit test JSPs; those would be Selenium-like UI tests. If the logic is in the service tier, it's obvious how you test it.

    JSPs should not be inherited. You can certainly compose them together using something like SiteMesh, but inheritance has no part in your JSPs. Once they inherit from Servlet, the chain should be ended.

    Besides, it's a false alternative. Neither one should require reuse, inheritance, or unit testing. But that doesn't mean there isn't a clear winner: it's JSTL. No one should be using scriptlets in JSPs, except for very rare one-liners. Scriptlets are begging for trouble.

    These days I prefer Velocity as my web UI template solution for Java, much more than JSPs. Just my opinion.

提交回复
热议问题