天天看點

如何避免JSP檔案中的Java代碼?

本文翻譯自:How to avoid Java code in JSP files?

I'm new to Java EE and I know that something like the following three lines

我是Java EE的新手,我知道類似以下三行
<%= x+1 %>
<%= request.getParameter("name") %>
<%! counter++; %>
           

is an old school way of coding and in JSP version 2 there exists a method to avoid Java code in JSP files.

是一種舊式的編碼方式,在JSP版本2中,存在一種避免JSP檔案中的Java代碼的方法。

Can someone please tell me the alternative JSP 2 lines, and what this technique is called?

有人可以告訴我替代的JSP 2行,以及這種技術的名稱是什麼?

#1樓

參考:https://stackoom.com/question/DKfl/如何避免JSP檔案中的Java代碼

#2樓

if you simply want to avoid the drawbacks of Java coding in JSP you can do so even with scriplets.

如果您隻是想避免JSP中Java編碼的缺點,即使使用scriplet也可以這樣做。

Just follow some discipline to have minimal Java in JSP and almost no calculation and logic in the JSP page.

隻需遵循一些規則,在JSP中使用最少的Java,而在JSP頁面中幾乎沒有計算和邏輯。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%//instantiate a JSP controller
MyController clr = new MyController(request, response);

//process action if any
clr.process(request);

//process page forwaring if necessary

//do all variable assignment here
String showMe = clr.getShowMe();%>

<html>
    <head>
    </head>
    <body>
        <form name="frm1">
            <p><%= showMe %>
            <p><% for(String str : clr.listOfStrings()) { %>
            <p><%= str %><% } %>

            // and so on   
        </form>
    </body>
</html>
           

#3樓

A neat idea from the Python world are Template attribute languages ;

來自Python世界的一個巧妙的想法是模闆屬性語言 ;

TAL was introduced by Zope (therefore aka "Zope Page Templates", ZPT) and is a standard, with implementations in PHP, XSLT and Java as well (I have used the Python/Zope and PHP incarnations).

TAL由Zope引入(是以也稱為“Zope頁面模闆”,ZPT)并且是标準的,并且還實作了PHP,XSLT和Java(我使用了Python / Zope和PHP版本)。

In this class of templating languages, one above example could look like this:

在這類模闆語言中,上面的一個示例可能如下所示:
<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>
                

The code looks like ordinary HTML (or XHTML) plus some special attributes in an XML namespace;

代碼看起來像普通的HTML(或XHTML)以及XML命名空間中的一些特殊屬性;

it can be viewed with a browser and safely be tweaked by a designer.

它可以通過浏覽器檢視,并由設計師安全地進行調整。

There is support for macros and for i18n as well:

支援宏和i18n:
<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>
                

If translations of the content are available, they are used.

如果内容的翻譯可用,則使用它們。

I don't know very much about the Java implementation , though.

不過,我對Java實作不太了解。

#4樓

If somebody is really against programming in more languages than one , I suggest GWT, theoretically you can avoid all the JS and HTML elements, because Google Toolkit transforms all the client and shared code to JS, you won't have problem with them, so you have a webservice without coding in any other languages.

如果有人真的反對使用多種語言程式設計 ,我建議GWT,理論上你可以避免所有的JS和HTML元素,因為Google Toolkit将所有用戶端和共享代碼轉換為JS,你不會有問題,是以你有一個沒有任何其他語言編碼的網絡服務。

Even you can use some default CSS from somewhere as it is given by extensions (smartGWT or Vaadin).

即使你可以使用某些預設的CSS,因為它是由擴充(smartGWT或Vaadin)提供的。

You don't need to learn dozens of annotations.

您不需要學習幾十個注釋。

Of course if you want, you can hack yourself into the depths of the code and inject JS and enrich your HTML page, but really you can avoid it if you want, and the result will be good as it was written in any other frameworks.

當然,如果你願意,你可以深入了解代碼的深度并注入JS并豐富你的HTML頁面,但實際上如果你願意,你可以避免使用它,結果會很好,因為它是在任何其他架構中編寫的。

I say worths a try, and the basic GWT is well-documented.

我說值得一試,基本的GWT已經有了很好的記錄。

And of course many fellow programmers hereby described or recommended several other solutions.

當然,許多其他程式員在此描述或推薦了其他幾種解決方案。

GWT is for people who really don't want to deal with the web part or to minimalize it.

GWT适用于那些真正不想處理Web部件或将其最小化的人。

#5樓

No matter how much you try to avoid, when you work with other developers, some of them will still prefer scriptlet and then insert the evil code into the project.

無論你試圖避免多少,當你與其他開發人員合作時,他們中的一些人仍然會喜歡scriptlet,然後将邪惡的代碼插入到項目中。

Therefore, setting up the project at the first sign is very important if you really want to reduce the scriptlet code.

是以,如果您真的想減少scriptlet代碼,那麼在第一個符号設定項目非常重要。

There are several techniques to get over this (including several frameworks that other mentioned).

有幾種技術可以克服這個問題(包括其他提到的幾個架構)。

However, if you prefer the pure JSP way, then use the JSTL tag file.

但是,如果您更喜歡純JSP方式,那麼請使用JSTL标記檔案。

The nice thing about this is you can also set up master pages for your project, so the other pages can inherit the master pages

關于這一點的好處是您還可以為項目設定母版頁,是以其他頁面可以繼承母版頁

Create a master page called base.tag under your WEB-INF/tags with the following content

在WEB-INF / tags下建立一個名為base.tag的母版頁,其中包含以下内容

On this mater page, I created a fragment called "title", so that in the child page, I could insert more codes into this place of the master page.

在這個mater頁面上,我建立了一個名為“title”的片段,這樣在子頁面中,我可以在母版頁的這個位置插入更多代碼。

Also, the tag

<jsp:doBody/>

will be replaced by the content of the child page

此外,标記

<jsp:doBody/>

<jsp:doBody/>

頁面的内容替換

Create child page (child.jsp) in your WebContent folder:

在WebContent檔案夾中建立子頁面(child.jsp):

<t:base>

is used to specify the master page you want to use (which is base.tag at this moment).

<t:base>

用于指定要使用的母版頁(此時為base.tag)。

All the content inside the tag

<jsp:body>

here will replace the

<jsp:doBody/>

on your master page.

标簽

<jsp:body>

所有内容都将替換

<jsp:doBody/>

<jsp:doBody/>

Your child page can also include any tag lib and you can use it normally like the other mentioned.

您的子頁面還可以包含任何标記庫,您可以像通常提到的那樣使用它。

However, if you use any scriptlet code here (

<%= request.getParameter("name") %>

...) and try to run this page, you will get a

JasperException because Scripting elements ( &lt;%!, &lt;jsp:declaration, &lt;%=, &lt;jsp:expression, &lt;%, &lt;jsp:scriptlet ) are disallowed here

.

但是,如果你在這裡使用任何scriptlet代碼(

<%= request.getParameter("name") %>

...)并嘗試運作這個頁面,你将得到一個

JasperException because Scripting elements ( &lt;%!, &lt;jsp:declaration, &lt;%=, &lt;jsp:expression, &lt;%, &lt;jsp:scriptlet ) are disallowed here

Therefore, there is no way other people can include the evil code into the jsp file

是以,其他人無法将惡意代碼包含在jsp檔案中

Calling this page from your controller:

從您的控制器調用此頁面:

You can easily call the child.jsp file from your controller.

您可以從控制器輕松調用child.jsp檔案。

This also works nice with the struts framework

這也适用于struts架構

#6樓

Technically, JSP are all converted to Servlets during runtime .

從技術上講,JSP在運作時都會轉換為Servlet 。

JSP was initially created for the purpose of the decoupling the business logic and the design logic, following the MVC pattern.

JSP最初是為了按照MVC模式解耦業務邏輯和設計邏輯而建立的。

So JSP are technically all java codes during runtime.

是以JSP在運作時在技術上都是java代碼。

But to answer the question, Tag Libraries are usually used for applying logic (removing Java codes) to JSP pages.

但回答這個問題,Tag Libraries通常用于将邏輯(删除Java代碼)應用于JSP頁面。