简体   繁体   English

JSTL与JSP Scriptlets

[英]JSTL vs JSP Scriptlets

I want someone to explain some points in BlausC's amazing answer in this question . 我希望有人在这个问题中解释一下BlausC的惊人答案。

He said that scriptlets had some disadvantages, which are: 他说小脚本有一些缺点,它们是:

  1. Reusability: you can't reuse scriptlets. 可重用性:您无法重用scriptlet。 My question : how could I reuse JSTL code? 我的问题:我怎样才能重用JSTL代码?

  2. Replaceability: you can't make scriptlets abstract. 可替换性:您不能使scriptlet抽象化。 What does abstract mean and how could JST become abstract? 抽象是什么意思,JST如何变得抽象?

  3. OO: you can't make use of inheritance/composition. OO:你不能利用继承/组合。 How could I use OO paradigms in JSTL? 我如何在JSTL中使用OO范例?

  4. Debugging: if a scriptlet throws an exception halfway, all you get is a blank page. 调试:如果一个scriptlet中途抛出异常,你得到的只是一个空白页面。

  5. Testability: scriptlets can't be unit tested. 可测试性:scriptlet不能进行单元测试。 What does that mean, and how can JSTL be unit tested? 这是什么意思,JSTL如何进行单元测试?

  6. Maintainability: per saldo, more time is needed to maintain mingled/cluttered/duplicated code logic. 可维护性:每个saldo,需要更多的时间来维护混杂/混乱/重复的代码逻辑。 What does this mean? 这是什么意思?

The last thing is what he quoted form Oracle's recommendation: 最后一件事是他引用甲骨文的建议:

JSP scriptlets should not be used for writing business logic. JSP scriptlet不应该用于编写业务逻辑。

In the MVC pattern, I use scriptlets only in the presentation layer. 在MVC模式中,我仅在表示层中使用scriptlet。 What does he mean here? 他在这里是什么意思?

You seem to concentrate on only the presentation and flow-control part of the scriptlets as in using if , for and switch statements and out.print() things. 您似乎只关注scriptlet的表示和流控制部分,就像使用ifforswitch语句以及out.print() You seem to compare scriptlets 1:1 with JSTL. 您似乎将Scriptlet 1:1与JSTL进行比较。 This is wrong. 这是错的。 I was not talking about the flow control part only (which is indeed to be replaced by JSTL), but about writing raw Java code in JSP files in general. 我不是只谈论流控制部分(实际上是由JSTL取代),而是关于在JSP文件中编写原始Java代码。 Ie gathering request parameters, validating and converting values, interacting with database and other Java classes/methods, etc. All things you normally (indirectly) do in a Servlet or Filter. 即收集请求参数,验证和转换值,与数据库和其他Java类/方法交互等等。您通常(间接)在Servlet或Filter中执行的所有操作。

You should not have scriptlet code in JSPs. 应该在JSP中的scriptlet代码。 I'd recommend 100% JSTL and zero scriplet code. 我推荐100%JSTL和零scriplet代码。

JSPs should be purely presentation. JSP应该是纯粹的表示。 That's the hidden benefit of writing JSPs using only JSTL, because they get all their dynamic data elsewhere. 这是使用JSTL编写JSP的隐藏好处,因为它们可以在其他地方获取所有动态数据。 Let the service layer have the business logic and determine what data the JSP needs. 让服务层具有业务逻辑并确定JSP需要哪些数据。

This answers your unit testing question, too. 这也回答了你的单元测试问题。 You should not have to unit test JSPs; 您不应该单元测试JSP; those would be Selenium-like UI tests. 那些将是类似Selenium的UI测试。 If the logic is in the service tier, it's obvious how you test it. 如果逻辑位于服务层中,那么测试它的方式就很明显了。

JSPs should not be inherited. 不应继承JSP。 You can certainly compose them together using something like SiteMesh, but inheritance has no part in your JSPs. 你当然可以使用类似SiteMesh的东西将它们组合在一起,但是继承在你的JSP中没有任何作用。 Once they inherit from Servlet, the chain should be ended. 一旦他们从Servlet继承,链就应该结束了。

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. 但这并不意味着没有一个明显的赢家:它是JSTL。 No one should be using scriptlets in JSPs, except for very rare one-liners. 没有人应该在JSP中使用scriptlet,除了非常罕见的单行程序。 Scriptlets are begging for trouble. Scriptlets正在乞求麻烦。

These days I prefer Velocity as my web UI template solution for Java, much more than JSPs. 这些天我更喜欢Velocity作为Java的Web UI模板解决方案,而不仅仅是JSP。 Just my opinion. 只是我的观点。

I can't speak for BalusC but in general I believe he was getting at the idea that these kinds of things should be accomplished by your ordinary Java code (in the Controller and Model layers if you're into the whole MVC thing). 我不能代表BalusC,但总的来说,我相信他已经认识到这些事情应该由你的普通Java代码完成(如果你进入整个MVC的话,在Controller和Model层中)。

  1. You can't literally reuse JSP tags at an individual level, but you can reuse the classes they call into. 您无法在单个级别上重复使用JSP标记,但可以重用它们调用的类。

  2. JSTL can't be abstract, but ordinary Java code (which you can perhaps invoke from JSTL) can be. JSTL不能是抽象的,但是普通的Java代码(你可以从JSTL调用它)也可以。

  3. Again, you can't make objects usefully in jstl, but you can in all the classes that are called. 同样,你不能在jstl中有用地创建对象,但是你可以在所有被调用的类中。

  4. JSTL by itself is not unit-testable. JSTL本身不是单元可测试的。 But the classes and methods you call through it are. 但是你通过它调用的类和方法是。

我没有看到scriplets特别糟糕,如果你遵循它的设计模式,我在spring mvc上工作很多,在我的jsp中,我只是在scriplits中获取模型数据,并使用简单的java代码在html中向用户显示,我觉得它给了我比JSTL更多的自由。

It depends on the pattern you're using. 这取决于您使用的模式。 By using the MVC ( spring, struts, ... ) you should avoid the usage of scriptlets in your JSP, because it represent the view it should contain pure XHTML tags. 通过使用MVCspring,struts,... ),您应该避免在JSP中使用scriptlet,因为它代表了应该包含纯XHTML标记的视图。 JSTL is a declarative language some kind of XML, while scriplet isn't. JSTL是一种声明性语言,某种XML,而scriplet则不是。

Particularly I have used JSTL in combination with AJAX via prototype for generating RIA without needing to implement another pattern. 特别是我通过原型使用JSTL与AJAX结合生成RIA而无需实现其他模式。 Recently I have seen this kind of programming with ExtJS and DWR . 最近我看到了ExtJSDWR的这种编程。 In my case I found It was necessary to combine both JSTL and scriplets always preferring JSTL when possible. 在我的情况下,我发现有必要将JSTL和scriplet结合起来,在可能的情况下总是更喜欢JSTL。

<!-- simple controller, each action is called by means of AJAX -->
<% String signExt="jpg"; %>
<% int r=0, iMaxRows=0, iMaxCols=0;%>
<c:choose>

    <c:when test="${param.action == 'get_chrequest_profile_table_by_family_and_date'}">
        <sql:query var="dataset">
            CALL GetProfilesView('<c:out value="${param.family_name}" />', '<c:out value="${param.reg_date}" />')
        </sql:query>

        <c:set var="strElements"><c:out value="${dataset.rowCount}" /></c:set> 
        <%  
        String strElements = pageContext.getAttribute("strElements").toString();
        int iElements = (int)Integer.valueOf(strElements).intValue(); 
        String to = "";
        %>

        <table class="tb_profiles" id="tb_profiles" iElements="<%=iElements%>"
               width="100%" frame=void border="0" cellPadding="0" cellSpacing="0" style="border-top: 3px solid gray; border-left: 1px solid gray">

            <%for(int i=1, j=0, col=0; i<100; i++){%>
            <tr>
                <%for(j=0; j<4; j++, col++){%>
                <c:set var="c" scope="page"><%=col%></c:set>

                <td name='<c:out value="${dataset.rows[c].chreqprofile_id}" />' >
                    <table width="100%" frame="below" cellPadding="0" cellSpacing="0"style="border-right: 1px solid gray;">

                        <%if( col < iElements){%>
                            <tr style="height:10mm">
                                <td class="td_function" style="cursor:default;">
                                    <c:out value="${dataset.rows[c].description}" />
                                </td>
                            </tr>
                            .................
                            <tr style="height:14mm">                        
                                <td class="td_signature" align="center" vAlign="middle">
                                    <img class="img_signature"
                                         src='../xdata/signatures/<c:out value="${dataset.rows[c].responsible_name}"/>.<%=signExt%>'
                                         alt='<c:out value="${dataset.rows[c].email}" />' 
                                    />
                                </td>
                            </tr>
                            .................

                            <c:set var="sMail"><c:out value="${dataset.rows[c].email}"/></c:set>
                            <% if( col < iElements-1){
                                    to = to + pageContext.getAttribute("sMail").toString() + ","; 
                               }else{
                                    to = to + pageContext.getAttribute("sMail").toString();
                               }
                            %>                      
                        <%}else{%>
                            <tr style="height:10mm">
                                <td class="td_function" style="cursor:default;">x</td>
                                .............
                            </tr>
                        <%}%>
                    </table>
                </td>               

                <%}%>
            </tr>
            <%
                if( col >= iElements){break;}
            }%>
        </table>
        <span id="span_mail_to" style="display:none;"><%=to%></span>        
    </c:when>   
    <c:when test="${param.action == 'functions_form_insert'}">  
        .............
    </c:when>   
</c:choose>

Here is a table comparing JSP and Facelets that may possibly be helpful to someone, somewhere: 这是一个比较JSP和Facelets的表,它可能对某人有帮助,某处:

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM