簡體   English   中英

在JSP中使用'getProperty'標記時獲取Null值

[英]Getting Null value when using 'getProperty' tag in JSP

我的jsp代碼:

    <jsp:useBean id="studentBean" beanName="StudentBean" type="StudentBean" />
     <%
       StudentBean sb=new StudentBean();
       sb.setName("My Name");
       studentBean=sb;
     %>
     <%=studentBean.getName()%>// display: My Name
     <jsp:getProperty name="studentBean" property="name" />// display: null
     <jsp:setProperty name="studentBean" property="name" value="My Name" />
     <jsp:getProperty name="studentBean" property="name" />// display: My Name

這是我的StudentBean類:

public class StudentBean{
      private String name;
      public String getName(){
            return name;
      }
      public void setName(String name){
            this.name=name;
      }
}

為什么在使用'getProperty'標簽時得到NULL值?

問題似乎是,一旦使用useBean標記來維護bean,就必須使用setter標記方法來設置值並將其放回各自的范圍。

否則,要解決該問題,您將需要手動將其放回示波器:

<jsp:useBean id="studentBean" beanName="com.zakimak.StudentBean" type="com.zakimak.StudentBean"/>
     <%
       StudentBean sb=new StudentBean();
       sb.setName("My Name111");
       studentBean=sb;
       // put the bean back into the page context for page scope as it is the default scope unless specified in tag
       pageContext.setAttribute("studentBean", studentBean);
     %>
     <%=studentBean.getName()%>
     <jsp:getProperty name="studentBean" property="name" />
     <jsp:setProperty name="studentBean" property="name" value="My Namec2" />
     <jsp:getProperty name="studentBean" property="name" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM