<jsp:getProperty>
The last standard action that relates to integrating JavaBeans into JSPs is <jsp:getProperty>. It takes the value
of the referenced bean’s instance property, converts it to a java.lang.String, and places it on the output stream.
The referenced bean instance must be defined and in scope before this action can be used. The syntax for the
<jsp:getProperty> action is as follows:
<jsp:getProperty name="name" property="propertyName" />
Table 2.7 contains the attributes and their descriptions for the <jsp:getProperty> action.
Table 2.7: Attributes for the <jsp:getProperty> Standard Action
Attribute Definition
name The name of the bean instance from which the
property is obtained, defined by a <jsp:useBean>
action or some other action.
property The bean property for which you want to get a value.
A JavaBean Standard Action Example
To learn how to use the JavaBean standard actions, let’s create an example. This example uses a simple
JavaBean that acts as a counter. The Counter bean has a single int property, count, that holds the current
number of times the bean’s property has been accessed. It also contains the appropriate methods for getting
and setting this property. Listing 2.11 contains the source code for the Counter bean.
Listing 2.11: Example of a Counter bean: Counter.java.
package chapter2;
public class Counter {
int count = 0;
The Components of a JavaServer Page
39
public Counter() {
}
public int getCount() {
count++;
return count;
}
public void setCount(int count) {
this.count = count;
}
}
Let’s look at integrating this sample JavaBean into a JSP, using the JavaBean standard actions. Listing 2.12
contains the JSP that leverages the Counter bean.
Listing 2.12: A JSP that uses the Counter bean: counter.jsp.
<!−− Set the scripting language to java −−>
<%@ page language="java" %>
<HTML>
<HEAD>
<TITLE>Bean Example</TITLE>
</HEAD>
<BODY>
<!−− Instantiate the Counter bean with an id of "counter" −−>
<jsp:useBean id="counter" scope="session"
class="chapter2.Counter" />
<%
// write the current value of the property count
out.println("Count from scriptlet code : "
+ counter.getCount() + "<BR>");
%>
<!−− Get the the bean’s count property, −−>
<!−− using the jsp:getProperty action. −−>
Count from jsp:getProperty :
<jsp:getProperty name="counter" property="count" /><BR>
</BODY>
</HTML>
Counter.jsp has four JSP components. The first component tells the JSP container that the scripting language
is Java:
<%@ page language="java" %>
The Components of a JavaServer Page
40
The next step uses the standard action <jsp:useBean> to create an instance of the class Counter with a scope
of session and ID of counter. Now you can reference this bean using the name counter throughout the rest of
the JSP. The code snippet that creates the bean is as follows:
<jsp:useBean id="counter" scope="session"
class="chapter2.Counter" />
The final two actions demonstrate how to get the current value of a bean’s property. The first of these two
actions uses a scriptlet to access the bean’s property, using an explicit method call. It simply accesses the bean
by its ID, counter, and calls the getCount() method. The scriptlet snippet is listed here:
<%
// write the current value of the property count
out.println("Count from scriptlet code : "
+ counter.getCount() + "<BR>");
%>
The second example uses the <jsp:getProperty> standard action, which requires the ID of the bean and the
property to be accessed. The action takes the attribute, calls the appropriate accessor, and embeds the results
directly into the resulting HTML document, as follows:
<!−− Get the bean’s count property, −−>
<!−− using the jsp:getProperty action. −−>
Count from jsp:getProperty :
<jsp:getProperty name="counter" property="count" /><BR>
When you execute the Counter.jsp, notice that the second reference to the count property results in a value
that is one greater than the first reference. This is the case because both methods of accessing the count
property result in a call to the getCount() method, which increments the value of count.
The last standard action that relates to integrating JavaBeans into JSPs is <jsp:getProperty>. It takes the value
of the referenced bean’s instance property, converts it to a java.lang.String, and places it on the output stream.
The referenced bean instance must be defined and in scope before this action can be used. The syntax for the
<jsp:getProperty> action is as follows:
<jsp:getProperty name="name" property="propertyName" />
Table 2.7 contains the attributes and their descriptions for the <jsp:getProperty> action.
Table 2.7: Attributes for the <jsp:getProperty> Standard Action
Attribute Definition
name The name of the bean instance from which the
property is obtained, defined by a <jsp:useBean>
action or some other action.
property The bean property for which you want to get a value.
A JavaBean Standard Action Example
To learn how to use the JavaBean standard actions, let’s create an example. This example uses a simple
JavaBean that acts as a counter. The Counter bean has a single int property, count, that holds the current
number of times the bean’s property has been accessed. It also contains the appropriate methods for getting
and setting this property. Listing 2.11 contains the source code for the Counter bean.
Listing 2.11: Example of a Counter bean: Counter.java.
package chapter2;
public class Counter {
int count = 0;
The Components of a JavaServer Page
39
public Counter() {
}
public int getCount() {
count++;
return count;
}
public void setCount(int count) {
this.count = count;
}
}
Let’s look at integrating this sample JavaBean into a JSP, using the JavaBean standard actions. Listing 2.12
contains the JSP that leverages the Counter bean.
Listing 2.12: A JSP that uses the Counter bean: counter.jsp.
<!−− Set the scripting language to java −−>
<%@ page language="java" %>
<HTML>
<HEAD>
<TITLE>Bean Example</TITLE>
</HEAD>
<BODY>
<!−− Instantiate the Counter bean with an id of "counter" −−>
<jsp:useBean id="counter" scope="session"
class="chapter2.Counter" />
<%
// write the current value of the property count
out.println("Count from scriptlet code : "
+ counter.getCount() + "<BR>");
%>
<!−− Get the the bean’s count property, −−>
<!−− using the jsp:getProperty action. −−>
Count from jsp:getProperty :
<jsp:getProperty name="counter" property="count" /><BR>
</BODY>
</HTML>
Counter.jsp has four JSP components. The first component tells the JSP container that the scripting language
is Java:
<%@ page language="java" %>
The Components of a JavaServer Page
40
The next step uses the standard action <jsp:useBean> to create an instance of the class Counter with a scope
of session and ID of counter. Now you can reference this bean using the name counter throughout the rest of
the JSP. The code snippet that creates the bean is as follows:
<jsp:useBean id="counter" scope="session"
class="chapter2.Counter" />
The final two actions demonstrate how to get the current value of a bean’s property. The first of these two
actions uses a scriptlet to access the bean’s property, using an explicit method call. It simply accesses the bean
by its ID, counter, and calls the getCount() method. The scriptlet snippet is listed here:
<%
// write the current value of the property count
out.println("Count from scriptlet code : "
+ counter.getCount() + "<BR>");
%>
The second example uses the <jsp:getProperty> standard action, which requires the ID of the bean and the
property to be accessed. The action takes the attribute, calls the appropriate accessor, and embeds the results
directly into the resulting HTML document, as follows:
<!−− Get the bean’s count property, −−>
<!−− using the jsp:getProperty action. −−>
Count from jsp:getProperty :
<jsp:getProperty name="counter" property="count" /><BR>
When you execute the Counter.jsp, notice that the second reference to the count property results in a value
that is one greater than the first reference. This is the case because both methods of accessing the count
property result in a call to the getCount() method, which increments the value of count.
解决方案 »
- 通过拦截器拦截后,根据返回值如何只是iframe进行刷新呢
- eclipse 3.0 Jetty-4.2.19 freemarker error
- 想把list放到request再在页面上用标显示出来,可是老是出现空指针,request取不到值 真心求解!!不想用action的方法取值,
- 求:正则表达式 4位数字,前两位不大于24,后两位不大于59
- 请问我们公司想自己搞一套cms,但是由于公司刚刚起步技术力量还不是很够有啥解决得方法?
- jsp页面的按钮报出页面错误,同时不予响应??为什么呢?
- 关于<intput type="text"> 中文本内容验证的问题
- 学JSP要学CSS吗?初学者迷茫中......希望大家给点学习意见。分不够再加,在线等
- java 压缩的问题:中文的文件名 压缩后乱码。
- request.getParameter()的参数可不可以是一个字符串变量?而不是一个字符串常量。
- <jsp:setProperty name="user" property="*"/>
- java程序调试,都快一个星期了,我都没有解决
初始化你的bean的属性,
利用传过来的参数表
这样可以掩盖一些细节
但是提交按钮也有值,也会被处理?
还是说bean只自动处理名字和属性一样的表单元素?
其中name就是你先前在<jsp:useBean>中给你的JavaBean起的名字,property是该Bean定义的一个字段,如entname,value是要赋给property的值,而param是用户请求该JSP页面时所带的参数,如果这个参数存在,则把它的值作为value赋给property。如果param的名字和property的名字相同,则value和param都可以省略。
拿你的例子来说,你要设置entname,则可以这样写:<jsp:useBean id="addUser" scope="page" class="manage.inner.UserManage"/>
<jsp:setProperty name="addUser" property="entname"/>你要确认请求该JSP的时候传入的param名称和property的名称一致。如果request的param名字和Bean的property名字不一样,比方说param叫做"REQentname",那么上面一句就要改成:<jsp:setProperty name="addUser" property="entname" param="REQentname"/>还有就是如果想一次设定所有参数,可以这样写<jsp:setProperty name="addUser" property="*"/>这样就会把Request中的所有addUser用得到的参数一次性传给Bean。
如果你感兴趣,可以把<jsp:setProperty>语句改为用Java表达,效果是一样的:<% addUser.setEntname(request.getParameter("entname")); %>