我在JSP boolean isPresent = false
中定义了一个变量,然后在包含的JSP中编写代码来检查该变量是否存在,但得到NoSuchFieldException。
Class thisClass = this.getClass();
try {
Field field = thisClass.getDeclaredField("isPresent");
}
catch (Exception ex) {
//ToDo
}
不知道怎么了
如果您使用:
<%
boolean isPresent = false;
%>
变量在从JSP生成的servlet的_jspService方法中定义,如下所示:
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;
%>
它会工作!!
Servlet已生成:
public final class testing_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
boolean isPresent = false;
正如你现在看到的变量是作用域类而不是方法作用域