在 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 class:
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" />
我的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 class:
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" />