JSP语法的xml表达
大家总是不喜欢看到<%和%>在jsp页面上,其实我们可以使用比较优雅的xml方式来表达
1,隐含的注释
JSP语法:<%-- 内容 --%>
XML语法:无
2,变量或函数的声明
JSP语法:<%! declaration;[declaration;]+... %>
例如:
<%!
int i=0;int a,b,c;
Color red=new Color(255,0,0);
private static final String OK="ok";
public String getAnswer(){
return OK;
}
%>
XML语法:
<jsp:declaration>
declartion;[declaration;]+...
</jsp:declaration>
例如:
<jsp:declaration>
int i=0;int a,b,c;
Color red=new Color(255,0,0);
private static final String OK="ok";
public String getAnswer(){
return OK;
}
</jsp:declaration>
3,表达式
JSP语法:<%=expression%>
例如:
<%=Math.round(Math.random*100)%>
XML语法:
<jsp:expression>
expression
</jsp:expression>
例如:
<jsp:expression>
Math.round(Math.random*100)
</jsp:expression>
4,代码片断
JSP语法:<% your java code %>
例如:
<%
String username=request.getParameter("username").trim();
String password=request.getParameter("password").trim();
%>
XMl语法:
<jsp:scriptlet>
your java code
</jsp:scriptlet>
例如:
<jsp:scriptlet>
String username=request.getParameter("username").trim();
String password=request.getParameter("password").trim();
</jsp:scriptlet>
5,指令:include
JSP语法:<%@include file="relativeURL"%>
例如:<%@include file="static_head.html"%>
XML语法:<jsp:directive.include file="relativeURL"/>
例如:<jsp:directive.include file="static_head.html"/>
6,指令:page
JSP语法:<%@page attribtes%>
例如:<%@page import="java.util.HashMap"%>
XML语法:<jsp:directive.page attribute/>
例如:<jsp:directive.page import="java.util.HashMap"/>
7,指令:taglib
JSP语法:<%@taglib uri="URIForLibrary" prefix="tagPrefix"%>
XML语法:无
对于jsp操作而言,本来就是xml格式。
下面使用xml表达方式书写一个jsp页面
example.jsp
<jsp:directive.page contentType="text/html;charset=GBK" />
<jsp:directive.page import="java.util.*,java.text.*" />
<jsp:declartion>
public String getCustomDate(Date,date,String pattern){
SimpleDateFormat format=new SimpleDateFormat();
format.applyPattern(pattern);
return format.format(date);
}
</jsp:declartion>
<jsp:scriptlet>
Date date=(Date)request.getAttribute("date");
String dateStr=getCustomDate(date,"yyyy-MM-dd,hh:mm:ss");
</jsp:scriptlet>
<div style="font-size:11pt">
显示时间:<jsp:expression>dateStr</jsp:expression>
</div>
http://blog.csdn.net/donkeyzheng/archive/2006/06/13/795121.aspx
1,隐含的注释
JSP语法:<%-- 内容 --%>
XML语法:无
2,变量或函数的声明
JSP语法:<%! declaration;[declaration;]+... %>
例如:
<%!
int i=0;int a,b,c;
Color red=new Color(255,0,0);
private static final String OK="ok";
public String getAnswer(){
return OK;
}
%>
XML语法:
<jsp:declaration>
declartion;[declaration;]+...
</jsp:declaration>
例如:
<jsp:declaration>
int i=0;int a,b,c;
Color red=new Color(255,0,0);
private static final String OK="ok";
public String getAnswer(){
return OK;
}
</jsp:declaration>
3,表达式
JSP语法:<%=expression%>
例如:
<%=Math.round(Math.random*100)%>
XML语法:
<jsp:expression>
expression
</jsp:expression>
例如:
<jsp:expression>
Math.round(Math.random*100)
</jsp:expression>
4,代码片断
JSP语法:<% your java code %>
例如:
<%
String username=request.getParameter("username").trim();
String password=request.getParameter("password").trim();
%>
XMl语法:
<jsp:scriptlet>
your java code
</jsp:scriptlet>
例如:
<jsp:scriptlet>
String username=request.getParameter("username").trim();
String password=request.getParameter("password").trim();
</jsp:scriptlet>
5,指令:include
JSP语法:<%@include file="relativeURL"%>
例如:<%@include file="static_head.html"%>
XML语法:<jsp:directive.include file="relativeURL"/>
例如:<jsp:directive.include file="static_head.html"/>
6,指令:page
JSP语法:<%@page attribtes%>
例如:<%@page import="java.util.HashMap"%>
XML语法:<jsp:directive.page attribute/>
例如:<jsp:directive.page import="java.util.HashMap"/>
7,指令:taglib
JSP语法:<%@taglib uri="URIForLibrary" prefix="tagPrefix"%>
XML语法:无
对于jsp操作而言,本来就是xml格式。
下面使用xml表达方式书写一个jsp页面
example.jsp
<jsp:directive.page contentType="text/html;charset=GBK" />
<jsp:directive.page import="java.util.*,java.text.*" />
<jsp:declartion>
public String getCustomDate(Date,date,String pattern){
SimpleDateFormat format=new SimpleDateFormat();
format.applyPattern(pattern);
return format.format(date);
}
</jsp:declartion>
<jsp:scriptlet>
Date date=(Date)request.getAttribute("date");
String dateStr=getCustomDate(date,"yyyy-MM-dd,hh:mm:ss");
</jsp:scriptlet>
<div style="font-size:11pt">
显示时间:<jsp:expression>dateStr</jsp:expression>
</div>
http://blog.csdn.net/donkeyzheng/archive/2006/06/13/795121.aspx