2008-09-26 77 views
30

我剛開始學習JSP技術,並遇到了一堵牆。如何從JSP <%! ... %>塊輸出HTML?

如何從<%的方法輸出HTML! ...%> JSP聲明塊?

這不起作用:

<%! 
void someOutput() { 
    out.println("Some Output"); 
} 
%> 
... 
<% someOutput(); %> 

服務器說沒有「出」。

U:我知道如何用這種方法重寫代碼返回一個字符串,但有沒有辦法在<%之內做到這一點! void(){}%>?雖然它可能不是最佳的,但它仍然很有趣。

回答

27

您不能在指令內使用'out'變量(或任何其他「預先聲明的」scriptlet變量)。

JSP頁面被您的網絡服務器翻譯成Java servlet。例如,在tomcat中,腳本中的所有內容(開始「<%」)以及所有靜態HTML都被轉換爲一種巨大的Java方法,它將您的頁面逐行寫入到稱爲「out」的JspWriter實例。這就是爲什麼你可以直接在scriptlet中使用「out」參數的原因。另一方面,指令(以「<%!」開始)被翻譯爲獨立的Java方法。

舉個例子,一個非常簡單的頁面(我們稱之爲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"; 
    } 
} 
+0

謝謝你的不錯的答案,但它不是我要找的。 – ansgri 2008-09-26 16:48:32

+3

除非使用表達式語法<%= %>,否則對someOutput的調用將不會放入out.write語句中。當您使用scriptlet語法時,它只是內嵌插入。 – 2008-09-26 17:00:21

8

我想這會幫助:

<%! 
    String someOutput() { 
    return "Some Output"; 
    } 
%> 
... 
<%= someOutput() %> 

無論如何,這是不是有代碼視圖中的一個好主意。

9

所有你需要做的是傳遞的JspWriter對象插入方法作爲參數,即

void someOutput(JspWriter stream) 

然後通過調用它:

<% someOutput(out) %> 

筆者對象是內部_jspService一個局部變量等等你需要將它傳遞給你的實用方法。這同樣適用於所有其他內置參考(例如請求,響應,會話)。

瞭解發生了什麼的一個好方法是使用Tomcat作爲服務器,並深入到'jsp'頁面生成的'.java'文件的'work'目錄。或者,在weblogic中,您可以使用'weblogic.jspc'頁面編譯器查看在請求頁面時將生成的Java。

12
<%! 
private void myFunc(String Bits, javax.servlet.jsp.JspWriter myOut) 
{ 
    try{ myOut.println("<div>"+Bits+"</div>"); } 
    catch(Exception eek) { } 
} 
%> 
... 
<% 
    myFunc("more difficult than it should be",out); 
%> 

試試這個,它適合我!

-1

你可以做這樣的事情:

<% 

out.print("<p>Hola</p>");  
out.print("<p>Cómo estás?</p>"); 

%> 
3

你可以做這樣的事情:

<%! 
String myMethod(String input) { 
    return "test " + input; 
} 
%> 

<%= myMethod("1 2 3") %> 

這將輸出test 1 2 3頁面。

6

一個簡單的替代方法是如下:

<%! 
    String myVariable = "Test"; 
    pageContext.setAttribute("myVariable", myVariable); 
%> 

<c:out value="myVariable"/> 
<h1>${myVariable}</h1> 

的,你可以簡單地使用變量以任何方式JSP代碼

1

太晚內回答,但這種幫助他人

<%! 
    public void printChild(Categories cat, HttpServletResponse res){ 
     try{ 
      if(cat.getCategoriesSet().size() >0){ 
       res.getWriter().write("") ; 
      } 
     }catch(Exception exp){ 

     } 
    } 

%> 
相關問題