天天看点

JAVA入门[12]-JavaBean

JavaBean是特殊的Java类,使用Java语言书写,并且遵守规范:

提供一个默认的无参构造函数。

需要被序列化并且实现了Serializable接口。

可能有一系列可读写属性。

可能有一系列的"getter"或"setter"方法。

<code>package</code> <code>com.cathy.domain;</code>

<code>public</code> <code>class</code> <code>Category </code><code>implements</code> <code>java.io.Serializable{</code>

<code>    </code><code>public</code> <code>Category(){}</code>

<code>    </code><code>private</code> <code>int</code> <code>cateId;</code>

<code>    </code><code>private</code> <code>String cateName;</code>

<code>    </code><code>public</code> <code>int</code> <code>getCateId() {</code>

<code>        </code><code>return</code> <code>cateId;</code>

<code>    </code><code>}</code>

<code>    </code><code>public</code> <code>void</code> <code>setCateId(</code><code>int</code> <code>cateId) {</code>

<code>        </code><code>this</code><code>.cateId = cateId;</code>

<code>    </code><code>public</code> <code>String getCateName() {</code>

<code>        </code><code>return</code> <code>cateName;</code>

<code>    </code><code>public</code> <code>void</code> <code>setCateName(String cateName) {</code>

<code>        </code><code>this</code><code>.cateName = cateName;</code>

<code>}</code>

  

1. &lt;jsp:useBean&gt; 标签可以在JSP中声明一个JavaBean,语法格式如下:

<code>&lt;jsp:useBean id=</code><code>"bean 的名字"</code> <code>scope=</code><code>"bean 的作用域"</code> <code>/&gt;</code>

其中scope的值可以是page,request,session或application

2.设置和获取JavaBean属性

在 &lt;jsp:useBean&gt; 标签主体中使用 &lt;jsp:getProperty/&gt; 标签来调用 getter 方法获取属性,使用 &lt;jsp:setProperty/&gt; 标签调用 setter 方法设置属性。语法格式:

<code>&lt;jsp:useBean id=</code><code>"id"</code> <code>class</code><code>=</code><code>"bean 类"</code> <code>scope=</code><code>"bean 作用域"</code><code>&gt;</code>

<code>&lt;jsp:setProperty name=</code><code>"bean 的 id"</code> <code>property=</code><code>"属性名"</code>

<code>value=</code><code>"value"</code><code>/&gt;</code>

<code>&lt;jsp:getProperty name=</code><code>"bean 的 id"</code> <code>property=</code><code>"属性名"</code><code>/&gt;</code>

<code>   </code><code>...........</code>

<code>&lt;/jsp:useBean&gt;</code>

其中name属性指的是Bean的id属性,property属性指的是想要调用的getter或setter方法

1.示例:在当前jsp页面设置和获取javabean属性

<code>&lt;jsp:useBean id=</code><code>"category"</code> <code>class</code><code>=</code><code>"com.cathy.domain.Category"</code><code>&gt;</code>

<code>    </code><code>&lt;jsp:setProperty name=</code><code>"category"</code> <code>property=</code><code>"cateId"</code> <code>value=</code><code>"2"</code><code>&gt;&lt;/jsp:setProperty&gt;</code>

<code>    </code><code>&lt;jsp:setProperty name=</code><code>"category"</code> <code>property=</code><code>"cateName"</code> <code>value=</code><code>"女装"</code><code>&gt;&lt;/jsp:setProperty&gt;</code>

<code>&lt;div&gt;</code>

<code>    </code><code>id:&lt;jsp:getProperty name=</code><code>"category"</code> <code>property=</code><code>"cateId"</code><code>&gt;&lt;/jsp:getProperty&gt;</code>

<code>&lt;/div&gt;</code>

<code>    </code><code>name:&lt;jsp:getProperty name=</code><code>"category"</code> <code>property=</code><code>"cateName"</code><code>&gt;&lt;/jsp:getProperty&gt;</code>

2.示例:在edit.jsp 页面form表单提交信息,在detail.jsp页面中显示。

edit.jsp

<code>&lt;form action=</code><code>"/category/detail"</code> <code>method=</code><code>"post"</code><code>&gt;</code>

<code>    </code><code>商品id:&lt;input type=</code><code>"number"</code> <code>name=</code><code>"cateId"</code><code>&gt;</code>

<code>    </code><code>商品名称:&lt;input type=</code><code>"text"</code> <code>name=</code><code>"cateName"</code><code>&gt;</code>

<code>    </code><code>&lt;input type=</code><code>"submit"</code> <code>value=</code><code>"提交"</code><code>&gt;</code>

<code>&lt;/form&gt;</code>

detail.jsp

如果表单中的属性名称和JavaBean中属性对应,那么可以简化jsp:setProperty写法,直接property="*"

<code>    </code><code>&lt;jsp:setProperty name=</code><code>"category"</code> <code>property=</code><code>"*"</code><code>&gt;&lt;/jsp:setProperty&gt;</code>

<code>&lt;table&gt;</code>

<code>    </code><code>&lt;tr&gt;</code>

<code>        </code><code>&lt;td&gt;id:&lt;/td&gt;&lt;td&gt;&lt;jsp:getProperty name=</code><code>"category"</code> <code>property=</code><code>"cateId"</code><code>&gt;&lt;/jsp:getProperty&gt;&lt;/td&gt;</code>

<code>    </code><code>&lt;/tr&gt;</code>

<code>    </code><code>&lt;tr&gt;&lt;td&gt;名称:&lt;/td&gt;&lt;td&gt;&lt;jsp:getProperty name=</code><code>"category"</code> <code>property=</code><code>"cateName"</code><code>&gt;&lt;/jsp:getProperty&gt;&lt;/td&gt;&lt;/tr&gt;</code>

<code>&lt;/table&gt;</code>

问题:通过表单提交中文数据时遇到乱码问题:

<code>&lt;%request.setCharacterEncoding(</code><code>"UTF-8"</code><code>);%&gt;</code>

效果:

JAVA入门[12]-JavaBean
JAVA入门[12]-JavaBean

3.示例:JavaBean实现访问计数

首先创建计数JavaBean:

<code>public</code> <code>class</code> <code>Counter {</code>

<code>    </code><code>private</code>  <code>int</code> <code>total=</code><code>0</code><code>;</code>

<code>    </code><code>public</code> <code>int</code> <code>getTotal() {</code>

<code>        </code><code>return</code> <code>total++;</code>

在index.jsp和edit.jsp文件中调用该JavaBean,注意将scope设置为application。

<code>&lt;jsp:useBean id=</code><code>"counter"</code> <code>class</code><code>=</code><code>"com.cathy.domain.Counter"</code> <code>scope=</code><code>"application"</code><code>&gt;&lt;/jsp:useBean&gt;</code>

<code>    </code><code>访问计数:&lt;jsp:getProperty name=</code><code>"counter"</code> <code>property=</code><code>"total"</code><code>&gt;&lt;/jsp:getProperty&gt;</code>

刷新这两个页面时,都能实现计数器累加。

    本文转自 陈敬(Cathy) 博客园博客,原文链接:http://www.cnblogs.com/janes/p/6518322.html,如需转载请自行联系原作者