繁体   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