我知道类似以下三行

<%= x+1 %>
<%= request.getParameter("name") %>
<%! counter++; %>

是一种老式的编码方式,在JSP版本2中存在一种避免JSP文件中出现Java代码的方法。什么是可选的JSP2行,这种技术叫什么?


当前回答

学习使用JSTL自定义和编写自己的标记

注意,EL是EviL(运行时异常和重构)。

Wicket也可能是邪恶的(对于小型应用程序或简单的视图层来说,性能和费力)。

java2s示例

必须将其添加到web应用程序的web.xml中

<taglib>
    <taglib-uri>/java2s</taglib-uri>
    <taglib-location>/WEB-INF/java2s.tld</taglib-location>
</taglib>

在/WEB-INF中创建文件java2s.tld/

<!DOCTYPE taglib
  PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
   "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

<!-- A tab library descriptor -->
<taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor">
    <tlib-version>1.0</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>Java2s Simple Tags</short-name>

    <!-- This tag manipulates its body content by converting it to upper case
    -->
    <tag>
        <name>bodyContentTag</name>
        <tag-class>com.java2s.BodyContentTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
          <name>howMany</name>
        </attribute>
    </tag>
</taglib>

将以下代码编译为WEB-INF\classes\com\java2s

package com.java2s;

import java.io.IOException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class BodyContentTag extends BodyTagSupport{
    private int iterations, howMany;

    public void setHowMany(int i){
        this.howMany = i;
    }

    public void setBodyContent(BodyContent bc){
        super.setBodyContent(bc);
        System.out.println("BodyContent = '" + bc.getString() + "'");
    }

    public int doAfterBody(){
        try{
            BodyContent bodyContent = super.getBodyContent();
            String bodyString  = bodyContent.getString();
            JspWriter out = bodyContent.getEnclosingWriter();

            if ( iterations % 2 == 0 )
                out.print(bodyString.toLowerCase());
            else
                out.print(bodyString.toUpperCase());

            iterations++;
            bodyContent.clear(); // empty buffer for next evaluation
        }
        catch (IOException e) {
            System.out.println("Error in BodyContentTag.doAfterBody()" + e.getMessage());
            e.printStackTrace();
        } // End of catch

        int retValue = SKIP_BODY;

        if ( iterations < howMany )
            retValue = EVAL_BODY_AGAIN;

        return retValue;
    }
}

启动服务器并在浏览器中加载bodyContent.jsp文件:

<%@ taglib uri="/java2s" prefix="java2s" %>
<html>
    <head>
        <title>A custom tag: body content</title>
    </head>
    <body>
        This page uses a custom tag manipulates its body content.Here is its output:
        <ol>
            <java2s:bodyContentTag howMany="3">
            <li>java2s.com</li>
            </java2s:bodyContentTag>
        </ol>
    </body>
</html>

其他回答

Python世界的一个好主意是模板属性语言;TAL是由Zope(因此又称为“Zope页面模板”,ZPT)引入的,是一种标准,也有PHP、XSLT和Java的实现(我使用过Python/Zope和PHP的化身)。在这类模板语言中,上面的一个示例可能如下所示:

<table>
    <tr tal:repeat="product products">
        <td tal:content="product/name">Example product</td>
        <td tal:content="product/description">A nice description</td>
        <td tal:content="product/price">1.23</td>
    </tr>
</table>

代码看起来像普通的HTML(或XHTML)加上XML命名空间中的一些特殊属性;它可以用浏览器查看,也可以由设计师安全地调整。

支持宏以及国际化和本地化:

<h1 i18n:translate="">Our special offers</h1>
<table>
    <tr tal:repeat="product products">
        <td tal:content="product/name"
            i18n:translate="">Example product</td>
        <td tal:content="product/description"
            i18n:translate="">A nice description</td>
        <td tal:content="product/price">1.23</td>
    </tr>
</table>

如果内容的翻译可用,则使用它们。

不过,我对Java实现不太了解。

JSTL为条件、循环、集合、get等提供标记。例如:

<c:if test="${someAttribute == 'something'}">
   ...
</c:if>

JSTL与请求属性一起工作——它们通常由Servlet在请求中设置,Servlet转发到JSP。

如果有人真的反对用多种语言编程,我建议使用GWT。理论上,您可以避免所有JavaScript和HTML元素,因为GoogleToolkit将所有客户端和共享代码转换为JavaScript。你不会有问题,所以你有一个没有任何其他语言编码的网络服务。您甚至可以使用扩展(smartGWT或Vaadin)提供的某些默认CSS。你不需要学习很多注释。

当然,如果你愿意的话,你可以深入代码的深处,注入JavaScript,丰富HTML页面,但如果你愿意,你真的可以避免它,结果会和其他框架一样好。我认为值得一试,而且基本的GWT有很好的文档记录。

当然,许多程序员同事在此描述或推荐了其他几种解决方案。GWT适用于那些真正不想处理web部件或将其最小化的人。

使用Backbone.js或类似AngularJS的JavaScript框架进行UI设计并使用REST API获取数据。这将从UI中完全删除Java依赖项。

经验表明,JSP有一些缺点,其中之一是很难避免将标记与实际代码混合。

如果可以,那么可以考虑使用专门的技术来完成您需要做的事情。在JavaEE6中有JSF2.0,它提供了许多不错的功能,包括通过#{bean.method(argument)}方法将Javabean与JSF页面粘合在一起。