Posted on 2007-08-02 10:13
G_G 閱讀(1617)
評(píng)論(3) 編輯 收藏 所屬分類(lèi):
JspTag
問(wèn)題:
jsp 的簡(jiǎn)化與 統(tǒng)一性
解決: EL 標(biāo)簽
jsp.頁(yè)面
<%@?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>
結(jié)果得到是:?? 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);
????}
}
心得:寫(xiě)EL心情 就是好