刚开始学习JSP技术,遇到了瓶颈。
如何从 JSP 声明块?
这不起作用:
<%!
void someOutput() {
out.println("Some Output");
}
%>
...
<% someOutput(); %>
服务器说没有“out”。
U:我确实知道如何使用返回字符串的方法重写代码,但是有没有办法在 ?尽管它可能不是最佳的,但它仍然很有趣。
您不能在指令内使用“out”变量(也不能使用任何其他“预先声明的”脚本变量)。
JSP 页面由 Web 服务器转换为 Java servlet。例如,在 tomcats 内部,scriptlet 内部的所有内容(以“
举个例子,一个非常简单的页面(我们称之为 foo.jsp):
<html>
<head/>
<body>
<%!
String someOutput() {
return "Some output";
}
%>
<% someOutput(); %>
</body>
</html>
最终看起来像这样(为了清楚起见,忽略了很多细节):
public final class foo_jsp
{
// This is where the request comes in
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
// JspWriter instance is gotten from a factory
// This is why you can use 'out' directly in scriptlets
JspWriter out = ...;
// Snip
out.write("<html>");
out.write("<head/>");
out.write("<body>");
out.write(someOutput()); // i.e. write the results of the method call
out.write("</body>");
out.write("</html>");
}
// Directive gets translated as separate method - note
// there is no 'out' variable declared in scope
private String someOutput()
{
return "Some output";
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)