如何避免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的母版页,其中包含以下内容

 <%@tag description="Overall Page template" pageEncoding="UTF-8"%> <%@attribute name="title" fragment="true" %> <html> <head> <title> <jsp:invoke fragment="title"></jsp:invoke> </title> </head> <body> <div id="page-header"> .... </div> <div id="page-body"> <jsp:doBody/> </div> <div id="page-footer"> ..... </div> </body> </html> 

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):

 <%@ taglib prefix="t" tagdir="/WEB-INF/tags" %> <t:base> <jsp:attribute name="title"> <bean:message key="hello.world" /> </jsp:attribute> <jsp:body> [Put your content of the child here] </jsp:body> </t:base> 

<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页面。

发布了0 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/105200257
今日推荐