var xxx = <%=variable%>;
此時瀏覽器直器當機,看來是語法的錯誤,可是當時居然找不到,靜下心來後,發現原來要這樣子用
var xxx = '<%=variable%>'; 或是 var xxx = "<%=variable%>";
因為javascript原本就可以用單或雙引號,在此做個筆記,以免忘記
var xxx = <%=variable%>;
var xxx = '<%=variable%>'; 或是 var xxx = "<%=variable%>";
public class AttrTag extends TagSupport {
private String scope;
private String content;
// 這兩個屬性都給setter即可
@Override
public int doStartTag() throws JspException {
System.out.println("doStartTag");
Object value = null;
if ("P".equals(scope)) {
value = pageContext.getAttribute(content, PageContext.PAGE_SCOPE);
}
if ("R".equals(this.scope)) {
value = pageContext.getAttribute(content, PageContext.REQUEST_SCOPE);
}
if ("S".equals(this.scope)) {
value = pageContext.getAttribute(content, PageContext.SESSION_SCOPE);
}
if ("A".equals(this.scope)) {
value = pageContext.getAttribute(content, PageContext.APPLICATION_SCOPE);
}
if (value == null) {
return SKIP_BODY;
} else {
return EVAL_BODY_INCLUDE;
}
}
<tag> <name>displayScope</name> <tag-class>attr.AttrTag</tag-class> <body-content>JSP</body-content> <attribute> <name>scope</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>content</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
<%
String scope = "P";
if ("P".equals(scope)) {
pageContext.setAttribute("ooo", scope);
} else if ("R".equals(scope)) {
request.setAttribute("ooo", scope);
} else if ("S".equals(scope)) {
session.setAttribute("ooo", scope);
} else if ("A".equals(scope)) {
application.setAttribute("ooo", scope);
}
%>
<bruce:displayScope scope="<%=scope%>" content="ooo">
<h1>範圍:<%=scope%></h1>
<h1>pageScope內容是${pageScope.ooo}</h1>
<h2>requestScope內容是${requestScope.ooo}</h2>
<h3>sessionScope內容是${sessionScope.ooo}</h3>
<h4>applicationScope內容是${applicationScope.ooo}</h4>
</bruce:displayScope>
<%
List<String> list = new ArrayList<String>();
list.add("A1");
list.add("B2");
list.add("C3");
request.setAttribute("ooo", list);
%>
<bruce:iterate content="ooo" id="abc">
${abc}<br/>
</bruce:iterate>
<tag> <name>iterate</name> <tag-class>attr.AttrHaveContent2</tag-class> <body-content>JSP</body-content> <attribute> <name>content</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>id</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
public class AttrHaveContent extends TagSupport {
private String content;
private Iterator<?> it;
public void setContent(String content) {
this.content = content;
}
@Override
public int doStartTag() throws JspException {
System.out.println("doStartTag");
Object value = pageContext.getAttribute(content, PageContext.REQUEST_SCOPE);
System.out.println("value=" + value);
if (value == null) {
return SKIP_BODY;
} else {
List<?> x = ((List<?>) value);
// Set<String> x = ((Map<String, Object>) value).keySet();
// Collection<Object> x = ((Map<String, Object>) value).values();
it = x.iterator();
if (it.hasNext()) {
pageContext.setAttribute(id, it.next());
return EVAL_BODY_INCLUDE;
}
return SKIP_BODY;
}
}
@Override
public int doEndTag() throws JspException {
System.out.println("doEndTag");
return SKIP_PAGE;
}
@Override
public int doAfterBody() throws JspException {
System.out.println("doAfterBody");
if (it.hasNext()) {
pageContext.setAttribute(id, it.next());
return EVAL_BODY_AGAIN;
} else {
return SKIP_BODY;
}
}
}
List<?> x = ((List<?>) value);
for (int i = 0; i < x.size(); i++) {
String s = (String) x.get(i);
setValue(s.substring(1), "x");
}
enu = getValues();
if (enu.hasMoreElements()) {
pageContext.setAttribute(id, enu.nextElement());
return EVAL_BODY_INCLUDE;
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="attrHaveContent" prefix="bruce"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head></head> <body> <bruce:endTagTest /> ooo </body> </html>
<jsp-config> <taglib> <taglib-uri>attrHaveContent</taglib-uri> <taglib-location>/WEB-INF/AttrHave.xml</taglib-location> </taglib> </jsp-config>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"> <tlib-version>1.0</tlib-version> <short-name>ht</short-name> <tag> <name>endTagTest</name> <tag-class>attr.AttrHaveContent</tag-class> <body-content>empty</body-content> </tag> </taglib>
public class AttrHaveContent extends TagSupport {
@Override
public int doEndTag() throws JspException {
System.out.println("doEndTag");
JspWriter out = this.pageContext.getOut();
try {
out.println("xxx");
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_PAGE;
}
}
public class EmptyTag extends TagSupport {
@Override
public int doStartTag() throws JspException {
try {
pageContext.getOut().println("<h1>tag success</h1>");
} catch (IOException e) {
e.printStackTrace();
}
// return TagSupport.SKIP_BODY
return super.doStartTag();
}
}
<?xml version="1.0" encoding="UTF-8"?> <taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"> <tlib-version>1.0</tlib-version> <short-name>ht</short-name> <tag> <name>helloTag</name> <tag-class>empty.EmptyTag</tag-class> <body-content>empty</body-content> </tag> </taglib>
<%@taglib uri="WEB-INF/helloTag.tld" prefix="bruce" %> <body> <bruce:helloTag /> </body>
<jsp-config> <taglib> <taglib-uri>http://javabruce.blogspot.tw/empty</taglib-uri> <taglib-location>/WEB-INF/helloTag.tld</taglib-location> </taglib> </jsp-config>
<%@taglib uri="http://javabruce.blogspot.tw/empty" prefix="bruce" %>
public class AttrTag extends TagSupport {
private String format;
private Double dou;
public void setFormat(String format) {
this.format = format;
}
public void setDou(Double dou) {
this.dou = dou;
}
@Override
public int doStartTag() throws JspException {
NumberFormat formatter = new DecimalFormat(this.format);
try {
pageContext.getOut().write(formatter.format(dou));
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
}
<tag> <name>displayNumber</name> <tag-class>attr.AttrTag</tag-class> <body-content>empty</body-content> <attribute> <name>format</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>dou</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
<%@taglib uri="http://javabruce.blogspot.tw/attr" prefix="bruce" %> <bruce:displayNumber dou="12345.6789" format="#,###,###.###"/>
<jsp-config> <taglib> <taglib-uri>http://javabruce.blogspot.tw/empty</taglib-uri> <taglib-location>/WEB-INF/helloTag.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://javabruce.blogspot.tw/attr</taglib-uri> <taglib-location>/WEB-INF/attr.tld</taglib-location> </taglib> </jsp-config>
<%
pageContext.setAttribute("d", 54321.87654d);
String f = "#,###,###.###";
%>
<bruce:displayNumber dou="${d}" format="<%=f%>"/>