jsp的attribute指令
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsp的attribute指令相关的知识,希望对你有一定的参考价值。
<body bgcolor=cyan>
<h4>这是一个Tag文件,负责计算三角形的面积。</h4>
<%@ attribute name="sideA" required="true" %>
<%@ attribute name="sideB" required="true" %>
<%@ attribute name="sideC" required="true" %>
<%@ variable name-given="time" variable-class="java.util.Date" scope="AT_END"%>
<%!
public String getArea(double a,double b,double c)
if(a+b>c&&a+c>b&&b+c>a)
double p=(a+b+c)/2.0;
double area=Math.sqrt(p*(p-a)*(p-b)*(p-c));
return ("<br>三角形的面积:"+area);
else
return ("<br>"+a+","+b+","+c+"不能构成一个三角形,无法计算面积");
%>
<%
double a=Double.parseDouble(sideA);
double b=Double.parseDouble(sideB);
double c=Double.parseDouble(sideC);
out.println("<br>JSP页面传递过来的三条边:"+a+","+b+","+c);
out.println(getArea(a,b,c));
jspContext.setAttribute("<br>time",new Date());
%>
</body>
<body bgcolor=cyan>
<h4>这是一个Tag文件,负责计算三角形的面积。</h4>
<%@ attribute name="sideA" required="true" %>
<%@ attribute name="sideB" required="true" %>
<%@ attribute name="sideC" required="true" %>
<%@ variable name-given="time" variable-class="java.util.Date" scope="AT_END"%>
<%!
public String getArea(double a,double b,double c)
if(a+b>c&&a+c>b&&b+c>a)
double p=(a+b+c)/2.0;
double area=Math.sqrt(p*(p-a)*(p-b)*(p-c));
return ("<br>三角形的面积:"+area);
else
return ("<br>"+a+","+b+","+c+"不能构成一个三角形,无法计算面积");
%>
<%
double a=Double.parseDouble(sideA);
double b=Double.parseDouble(sideB);
double c=Double.parseDouble(sideC);
out.println("<br>JSP页面传递过来的三条边:"+a+","+b+","+c);
out.println(getArea(a,b,c));
jspContext.setAttribute("<br>time",new Date());
%>
</body>
倒数第三行,为什么在jsp调用的时候出错呢?
之前粘贴错误,所以多了一个
报错:The JSP specification requires that attribute
The JSP specification requires that an attribute name is preceded by whitespace
翻译成中文:JSP规范要求属性名称前面有空白。
报错全文信息:
type Exception report
message /foreground/common/link.jsp (line: 1, column: 84) The JSP specification requires that an attribute name is preceded by whitespace
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: /foreground/common/link.jsp (line: 1, column: 84) The JSP specification requires that an attribute name is preceded by whitespace
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:408)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:89)
org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:164)
org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:183)
org.apache.jasper.compiler.ParserController.getPageEncodingForJspSyntax(ParserController.java:479)
org.apache.jasper.compiler.ParserController.determineSyntaxAndEncoding(ParserController.java:420)
org.apache.jasper.compiler.ParserController.doParse(ParserController.java:190)
org.apache.jasper.compiler.ParserController.parseDirectives(ParserController.java:119)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:193)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:954)
org.apache.jsp.index_jsp._jspService(index_jsp.java:89)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)```
源码:
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<div class="row-fluid">
<div class="span12">
<div class="link">
<div class="linkHeader">友情链接</div>
<div class="datas">
<ul>
<li><a href="http://www.baidu.com" target="_blank">百度</a></li>
<li><a href="http://www.oschina.net" target="_blank">开源中国</a></li>
<li><a href="http://www.java1234.com" target="_blank">Java1234</a></li>
<li><a href="2" target="_blank">超链7</a></li>
<li><a href="http://www.csdn.com" target="_blank">CSDN</a></li>
<li><a href="" target="_blank">超链2</a></li>
<li><a href="" target="_blank">超链3</a></li>
<li><a href="" target="_blank">超链4</a></li>
<li><a href="" target="_blank">超链5</a></li>
</ul>
</div>
</div>
</div>
</div>
最后发现pageEncoding连在一起导致,按下空格一下,问题解决了
以上是关于jsp的attribute指令的主要内容,如果未能解决你的问题,请参考以下文章
JSP中,EL表达式向session中取出一个attribute和JSP脚本访问session取出一个attribute,写法有何不同?
报错:The JSP specification requires that attribute
The JSP specification requires that an attribute name is preceded by whitespace