我知道类似以下三行
<%= x+1 %>
<%= request.getParameter("name") %>
<%! counter++; %>
是一种老式的编码方式,在JSP版本2中存在一种避免JSP文件中出现Java代码的方法。什么是可选的JSP2行,这种技术叫什么?
我知道类似以下三行
<%= x+1 %>
<%= request.getParameter("name") %>
<%! counter++; %>
是一种老式的编码方式,在JSP版本2中存在一种避免JSP文件中出现Java代码的方法。什么是可选的JSP2行,这种技术叫什么?
当前回答
如果您只是想避免JSP中Java编码的缺点,那么即使使用scriples也可以这样做。只需遵循一些规则,在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 forwarding, 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>
其他回答
学习使用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>
在JSP中使用JSTL标记库。这将非常有效。
我的朋友,这些都不再使用了。我的建议是将视图(CSS、HTML、JavaScript等)与服务器分离。
在我的例子中,我使用Angular处理视图,并且使用REST服务从服务器获取所需的任何数据。
相信我,这将改变你的设计方式。
JSP2.0有一个名为“标记文件”的特性,您可以在没有外部Java代码和tld的情况下编写标记。您需要创建一个.tag文件并将其放入WEB-INF\tag中。您甚至可以创建一个目录结构来打包标记。
例如:
/WEB-INF/tags/html/label.tag
<%@tag description="Rensders a label with required css class" pageEncoding="UTF-8"%>
<%@attribute name="name" required="true" description="The label"%>
<label class="control-label control-default" id="${name}Label">${name}</label>
像这样使用
<%@ taglib prefix="h" tagdir="/WEB-INF/tags/html"%>
<h:label name="customer name" />
此外,您还可以轻松阅读标签正文:
/WEB-INF/tags/html/bold.tag
<%@tag description="Bold tag" pageEncoding="UTF-8"%>
<b>
<jsp:doBody/>
</b>
使用它:
<%@ taglib prefix="h" tagdir="/WEB-INF/tags/bold"%>
<h:bold>Make me bold</h:bold>
示例非常简单,但您可以在这里执行许多复杂的任务。请考虑您可以使用其他标记(例如:JSTL,它具有控制标记,如if/forEcah/selected文本操作,如format/inclusions/capital或甚至SQL标记select/update),传递所有类型的参数,例如Hashmap、访问会话、请求等。。。在您的标记文件中。
标记文件非常容易开发,因为在更改它们时不需要重新启动服务器,就像JSP文件一样。这使它们易于开发。
即使您使用Struts2这样的框架,它有很多好的标记,您可能会发现拥有自己的标记可以大大减少代码。您可以将标记参数传递给struts,并通过这种方式自定义框架标记。
您不仅可以使用标记来避免Java,还可以最小化HTML代码。当我看到页面中出现重复的代码时,我自己会尝试检查HTML代码并构建标记。
(即使您最终在JSP代码中使用Java(我希望不会),也可以将该代码封装在标记中。)
使用Scriptlets是一种非常古老的方法,不建议使用。如果您想在JSP页面中直接输出一些内容,只需将表达式语言(EL)与JSTL一起使用即可。
还有其他选项,例如使用Velocity、Freemark、Thymelaf等模板引擎。但在大多数情况下,将普通JSP与EL和JSTL一起使用符合我的目的,对于初学者来说,这似乎也是最简单的。
此外,请注意,在视图层中执行业务逻辑不是最佳实践。您应该在服务层中执行业务逻辑,并通过控制器将输出结果传递给视图。