Posted on 2007-08-02 10:13
G_G 阅读(1603)
评论(3) 编辑 收藏 所属分类:
JspTag
问题:
jsp 的简化与 统一性
解决: EL 标签
jsp.页面
<%@ taglib prefix="fn" uri="/WEB-INF/fn.tld" %>
<%@ taglib prefix="c" uri="/WEB-INF/c-1_0-rt.tld" %>
<%@ taglib prefix="x" uri="/WEB-INF/x-1_0-rt.tld" %>
<%@ page isELIgnored="false" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<c:set var="url"><%=basePath%>/xml</c:set>
<c:import var="xmlDoc" url="${url}"/>
<x:parse xml="${xmlDoc}" var="doc" />
//xml解析
<c:set var="bookName" ><x:out select="$doc/xml-body/books/book[@id='1']"/></c:set>
//xml 定位
${fn:substring(bookName,"0", fn:length(bookName)-2 ) }<br> //字符substring
<x:forEach var="item" select="$doc/xml-body/books/book" > //迭带
<x:out select="$item" /><br>
</x:forEach>
</body>
</html>
结果得到是: book1_goog
book1_google
book2_baidu
book3_tiancai/xml 路径是
package Servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class XmlServlet extends HttpServlet {
/**
*
*/
private static final String CONTENT_TYPE = "text/html; charset=GBK";
public void init() throws ServletException{}
public void doGet( HttpServletRequest request,
HttpServletResponse response )
throws ServletException,IOException{
PrintWriter out = response.getWriter();
response.setContentType(CONTENT_TYPE);
response.setCharacterEncoding("GBK");
out.print("<?xml version='1.0' encoding='UTF-8'?>");
out.print("<xml-body>");
out.print("<name>RW</name>");
out.print("<passWord>123456</passWord>");
out.print("<age>28</age>");
out.print("<books>");
out.print("<book id='1'>book1_google</book>"); //jsp 定位得到的 字符
out.print("<book id='2'>book2_baidu</book>");
out.print("<book id='3'>book3_tiancai</book>");
out.print("</books>");
out.print("</xml-body>");
}
public void doPost( HttpServletRequest request,
HttpServletResponse response )
throws ServletException,IOException{
doGet(request,response);
}
}
心得:写EL心情 就是好