2016-08-10 16 views
-1

JSP boolean isPresent = falseで変数を定義し、インクルードされたJSPでこの変数が存在するかどうかを確認するコードを書いてNoSuchFieldExceptionを取得しました。JSPのReflectionによって変数が存在するかどうかを確認します

Class thisClass = this.getClass(); 
try { 
    Field field = thisClass.getDeclaredField("isPresent"); 
} 
catch (Exception ex) { 
    //ToDo 
} 

何が間違っているのだろうか。このように、変数はあなたのJSPから生成されたサーブレットのメソッド_jspServiceに定義されている

<% 
    boolean isPresent = false; 
%> 

:あなたがそれを定義する場合

答えて

1

public void _jspService(HttpServletRequest request, HttpServletResponse response) 
     throws java.io.IOException, ServletException { 

    PageContext pageContext = null; 
    HttpSession session = null; 
    ServletContext application = null; 
    ServletConfig config = null; 
    JspWriter out = null; 
    Object page = this; 
    JspWriter _jspx_out = null; 
    PageContext _jspx_page_context = null; 


    try { 
     /// more code 

    boolean isPresent = false; 

をしかし、あなたはの宣言を変更した場合これまでの変数:

<%! 
    boolean isPresent = false; 
%> 

サーブレットは今、生成された:

public final class testing_jsp extends org.apache.jasper.runtime.HttpJspBase 
    implements org.apache.jasper.runtime.JspSourceDependent { 

    boolean isPresent = false; 

あなたが見ることができるようになりまし変数がスコープクラスおよびNOTメソッドのスコープ

です
関連する問題