<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.
<!&#8722;&#8722; Set the scripting language to java &#8722;&#8722;>
<%@ page language="java" %>
<HTML>
<HEAD>
<TITLE>Bean Example</TITLE>
</HEAD>
<BODY>
<!&#8722;&#8722; Instantiate the Counter bean with an id of "counter" &#8722;&#8722;>
<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>");
%>
<!&#8722;&#8722; Get the the bean’s count property, &#8722;&#8722;>
<!&#8722;&#8722; using the jsp:getProperty action. &#8722;&#8722;>
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:
<!&#8722;&#8722; Get the bean’s count property, &#8722;&#8722;>
<!&#8722;&#8722; using the jsp:getProperty action. &#8722;&#8722;>
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.

解决方案 »

  1.   

    这是默认的啊,
    初始化你的bean的属性,
    利用传过来的参数表
    这样可以掩盖一些细节
      

  2.   

    这是设置bean的属性值,但要注意,bean中的属性名要和表单中的name属性名相同,它会制动调用bean的set的方法设置属性值.
      

  3.   

    那就对了,设置全部属性,也就是username和password属性就可以被设置上了。
      

  4.   

    to zhuxiaopeng:
        但是提交按钮也有值,也会被处理?
        还是说bean只自动处理名字和属性一样的表单元素?
      

  5.   

    对的bean只自动处理名字和属性一样的表单元素
      

  6.   

    其次,给你讲讲<jsp:setProperty>的用法:<jsp:setProperty name="" property="" [value=""] [param=""]/>
    其中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")); %>