JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(2)
来源:互联网 发布:centos终端快捷键 编辑:程序博客网 时间:2024/06/10 20:09
应用View Helper模式
下面的Helper可能对你在某些方面特别有用,但是最起码,他们要告诉你怎么在你的应用中使用View Helper模式。下面是一个定制标签的实现并且被声明在helpers.tld文件里。这个文件在web.xml 文件里做为一个条目和/helpers标签uri相关联。如下所示:
格式化文本
在下面的章节里,我将以一个用来格式化日期和货币的View Helper开始。虽然这个需求在Model里实现可能比较简单,但是你有很多原因使得你在View里实现它们,而不是在Model里。例如,你可能使用不同的格式来显示它们,或者可能内容因为要被不同的设置访问而必须提供不同的访问方式。
你可以在定制标签体内封装很多格式用来格式化标签所取得的数据,然后你给标签一个属性来取得使用者所需要的格式类型,根据这个属性选择相应的格式并将其输出出来。如下所示,你可以在标签库表述符里描述你的标签:
Listing 2. helpers.tld
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
web-jsptaglibrary_2_0.xsd"
version="2.0" >
Tag library to support the examples in Chapter 8
这个标签的Model能够无所不包,在这个例子中,你将创建一个静态的JavaBeans包含两个String类型的属性,一个用来保存Date,一个用来保存货币。你将通过使用标准的JSP setProperty标签在页面里设置这些值。为了达到这样的目的,你的JavaBeans必须为这两个属性提供方法入门。如下所示,是用来产生JavaBeans的Java代码:
Listing 3. FormattingModel.java
package jspbook.ch08.beans;
import java.io.Serializable;
public class FormattingModel implements Serializable {
private String dateValue;
private String currencyValue;
public FormattingModel () {}
/* Accessor Methods */
public void setDateValue (String _date)
{
this.dateValue = _date;
}
public String getDateValue ()
{
return this.dateValue;
}
public void setCurrencyValue (String _currency)
{
this.currencyValue = _currency;
}
public String getCurrencyValue ()
{
return this.currencyValue;
}
}
标签是一个简单的body标签,由BodyTagSupport 类继承而来。所有的格式化代码都在formatValue()方法里。在doAfterBody()里,一旦获取了数据,这个方法就会被调用。调用formatValue()方法的结果被写回了页面标签所在的位置。你可以使用java.text包的类来格式化日期或货币,而且,你可以使用SimpleDateFormat 和 DecimalFormat类。
标签处理者也提供了Locale 对象,通过相应的set方法,能够完成对内容的国际化。因为这个标签的职责是格式化日期和货币,那么它必然在不同的地区有不同的格式化要求。看看下面的代码,特别是要注意formatValue()方法:
Listing 4. FormatTag.java
package jspbook.ch08;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.BodyContent;
import java.io.IOException;
import java.util.Locale;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.NumberFormat;
import java.text.DecimalFormat;
public class FormatTag extends BodyTagSupport {
/* Locale object for internationalization of content */
private Locale locale;
/* Tag Attributes */
protected String format;
/* Static Constants */
private final static String DATE_LONG = "date";
private final static String NUMERIC_DECIMAL = "decimal";
private final static String NUMERIC_ROUNDED = "rounded";
private final static String NUMERIC_CURRENCY = "currency";
public FormatTag() {
locale = Locale.getDefault();
}
public void setLocale(Locale locale) {
this.locale = locale;
}
/* Process Tag Body */
public int doAfterBody() throws JspTagException {
try {
BodyContent body = getBodyContent();
JspWriter out = body.getEnclosingWriter();
/* Get Input Value */
String textValue = body.getString().trim();
/* Output Formatted Value */
out.println(formatValue(textValue));
}
catch (IOException e) {
throw new JspTagException(e.toString());
}
return SKIP_BODY;
}
/* Process End Tag */
public int doEndTag() throws JspTagException {
return EVAL_PAGE;
}
private String formatValue (String _input)
{
String formattedValue = "";
try {
if(format.equals(DATE_LONG)) {
Calendar cal = Calendar.getInstance();
cal.setTime(DateFormat.getDateInstance(
DateFormat.SHORT).parse(_input));
SimpleDateFormat df = new SimpleDateFormat("EEE, MMM d, yyyy");
formattedValue = df.format(cal.getTime());
} else if(format.equals(NUMERIC_DECIMAL)) {
DecimalFormat dcf = (DecimalFormat) NumberFormat.getInstance(locale);
dcf.setMinimumFractionDigits(2);
dcf.setMaximumFractionDigits(2);
formattedValue = dcf.format(dcf.parse(_input));
} else if(format.equals(NUMERIC_ROUNDED)) {
DecimalFormat dcf = (DecimalFormat) NumberFormat.getInstance(locale);
dcf.setMinimumFractionDigits(0);
dcf.setMaximumFractionDigits(0);
formattedValue = dcf.format(dcf.parse(_input));
} else if(format.equals(NUMERIC_CURRENCY)) {
float num = Float.parseFloat(_input);
DecimalFormat dcf = (DecimalFormat)
NumberFormat.getCurrencyInstance();
formattedValue = dcf.format(num);
}
}
catch (Exception e) {
System.out.println(e.toString());
}
return formattedValue;
}
/* Attribute Accessor Methods */
public String getFormat ()
{
return this.format;
}
public void setFormat (String _format)
{
this.format = _format;
}
}
最后,你将在JSP页面使用该标签,这里实在是没有什么新东西。页面声明一个JavaBeans来作为Model使用,在这个Model里设置值,使用不同的格式来显示这些值。这些格式化的动作通过FormatTag实现,该标签在helpers.tld里给定,并且在JSP页面使用taglib 指示符来声明。注意:你需要通过标签的一个属性来设置格式类型。format 属性就是用来指定一个格式类型的值,而这个值必须依赖于标签里设定的那些常量来确定。如下所示,是JSP代码:
Listing 5. formatHelper.jsp
<%-- Declare tag that we'll use as our helper --%>
<%@ taglib uri="/helpers" prefix="helpers" %>
<%-- Declare bean that will act as our model --%>
<%-- Display Formatted Values (using helper) --%>
Various Date and Currency Formats
Long Date
Decimal (NN.NN)
${myBean.currencyValue}
${myBean.currencyValue}
Integer (N,NNN)
${myBean.currencyValue}
${myBean.currencyValue}
Currency (N,NNN.NN)
${myBean.currencyValue}
${myBean.currencyValue}
下图是显示结果:
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(2)
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(2)
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(2)
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(2)
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(2)
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(1)
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(3)
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(3)
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(1)
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(3)
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(1)
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(3)
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(1)
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(3)
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(1)
- jsp的设计模式基础:view helper模式
- iOS设计模式——MVC(Model-View-Controller)
- iOS设计模式——MVC(Model View Controller)
- IntelliFlow.Net性能评测
- Java字符串的“朝花夕拾”
- 用户体验式UI设计
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(3)
- .Net平台开发的技术规范与实践精华总结
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(2)
- .Net vs. J2EE-----面对SOA的荒谬与误解
- JSP设计模式基础:View Helper模式——学习如何使用View Helper模式使得Model数据适应表现层的需要(1)
- 基于.Net的SAP Portal开发
- 幕后英雄的用武之地——浅谈Java内部类的四个应用场景
- 基于.Net 的工作流驱动平台intelliFlow.Net研制成功
- 开发保留标准的浏览器功能的AJAX应用
- 统计分析与数据挖掘所涉及的应用领域探讨
- 代码的“门面”——模式系列谈之Fa ade模式
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
桐生樱
桐字组词
桐组词有哪些
桐怎么组词
桐的组词是什么
桐的组词有那些
桐组词两个字
桐花菜
桐茶
桐谷华
桐谷茉莉手机在线播放全部
桐谷茉莉在线播放
凌剑帝尊 桐谷凌天
桑非白作品
我非你不可 暮桑梧
野生桑黄多少钱一斤
一世之尊顾小桑
桑一非
为所欲为 叶桑霍瑾廷
叶桑霍瑾廷全文阅读
桑代克
桑代克的猫
桑代克的试误说
桑代克的饿猫实验
桑代克的三大定律
桑佩尔
铁桑兰茶叶价格
神话三国之系统为王 空桑剑圣
桑切斯
桑切斯和贝索斯
贝索斯
桑博士汗蒸房价格
桑叶猪肝汤
桑叶粉的功效与作用
野菊花粉
芥辣的功效
红桑叶图片
桑叶茶的作用与功效与作用
桑叶茶制作方法步骤
女性喝桑叶茶有什么好处
蒲公英桑叶茶的功效