- 浏览: 344719 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (97)
- 搜索引擎 (9)
- JAVA基础知识 (7)
- Struts (13)
- Spring (1)
- Hibernate (0)
- iBATIS (2)
- ExtJs (10)
- AJAX (0)
- Oracle (1)
- MSSQLSERVER (1)
- MySQL (0)
- Prototype (0)
- DWR (0)
- EJB (0)
- J2EE综合 (0)
- 算法 (1)
- WebServices (0)
- JMS (0)
- JavaMail (0)
- JMX (0)
- UML (0)
- Dojo (0)
- Acegi (2)
- 其他Html-XML等 (3)
- CSS (1)
- 其他开源项目 (0)
- 英语 (0)
- Web3D (24)
- FCKeditor (1)
- javascript 特效 (2)
最新评论
-
荷戟者:
如何实现的,是否可以想象一点
extjs实现左侧tab -
coconut_zhang:
怎么没人评论,吼吼。。
HTMLParser使用详解(4)- 通过Visitor访问内容 -
Sorry':
JDBC中处理存储过程的结果集的通用流程 -
Sorry':
计划将客户
JDBC中处理存储过程的结果集的通用流程 -
renhongchao:
very good!在csdn也看到相同的文章,莫非是同一人? ...
HTMLParser使用详解(3)- 通过Filter访问内容
在我已往的Struts 1.x项目经验中,有个问题不时的出现——在创建FormBean时,对于某个属性到底应该用String还是其它类型?
开发Web应用程序与开发传统桌面应用程序不同,Web应用程序实际上是分布个不同的主机(当然也可以同一个主机,不过比较少见)上的两个进程之间互交。这种互交建立在HTTP之上,它们互相传递是都是字符串。换句话说, 服务器可以的接收到的来自用户的数据只能是字符串或字符数组,而在服务器上的对象中,这些数据往往有多种不同的类型,如日期(Date),整数(int),浮点数(float)或自定义类型(UDT)等,如图1所示。因此,我们需要服务器端将字符串转换为适合的类型。
图1 UI与服务器对象关系
同样的问题也发生在使用UI展示服务器数据的情况。HTML的Form控件不同于桌面应用程序可以表示对象,其值只能为字符串类型,所以我们需要通过某种方式将特定对象转换成字符串。
要实现上述转换,Struts 2.0中有位魔术师可以帮到你——Converter。有了它,你不用一遍又一遍的重复编写诸如此类代码:
Date birthday = DateFormat.getInstance(DateFormat.SHORT).parse(strDate);
<input type="text" value="<%= DateFormat.getInstance(DateFormat.SHORT).format(birthday) %>" />
好了,现在让我们来看一个例子。
转换器——Hello World
在我的上一篇文章《在Struts 2.0中国际化(i18n)您的应用程序》的最后我举了一个可以让用户方便地切换语言的例子,下面例子与其相似,但实现方法不同。
首先,如《在Struts 2.0中国际化(i18n)您的应用程序》的第一个例子一样,创建和配置默认的资源文件;
接着,新建源代码文件夹下的tutorial包创建HelloWorld.java文件,代码如下:
import java.util.Locale;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.LocalizedTextUtil;
public class HelloWorld extends ActionSupport {
private String msg;
private Locale loc = Locale.US;
public String getMsg() {
return msg;
}
public Locale getLoc() {
return loc;
}
public void setLoc(Locale loc) {
this .loc = loc;
}
@Override
public String execute() {
// LocalizedTextUtil是Struts 2.0中国际化的工具类,<s:text>标志就是通过调用它实现国际化的
msg = LocalizedTextUtil.findDefaultText( " HelloWorld " , loc);
return SUCCESS;
}
}
然后,在源代码文件夹下的struts.xml加入如下代码新建Action:
< action name ="HelloWorld" class ="tutorial.HelloWorld" >
< result > /HelloWorld.jsp </ result >
</ action >
</ package >
再在Web文件夹下,新建 HelloWorld.jsp,代码如下:
< %@taglib prefix ="s" uri ="/struts-tags" % >
< html >
< head >
< title > Hello World </ title >
</ head >
< body >
< s:form action ="HelloWorld" theme ="simple" >
Locale: < s:textfield name ="loc" /> < s:submit />
</ s:form >
< h2 >< s:property value ="msg" /></ h2 >
</ body >
</ html >
接下来,在源代码文件夹的tutorial包中新建LocaleConverter.java文件,代码如下:
import java.util.Locale;
import java.util.Map;
public class LocaleConverter extends ognl.DefaultTypeConverter {
@Override
public Object convertValue(Map context, Object value, Class toType) {
if (toType == Locale. class ) {
String locale = ((String[]) value)[ 0 ];
return new Locale(locale.substring( 0 , 2 ), locale.substring( 3 ));
} else if (toType == String. class ) {
Locale locale = (Locale) value;
return locale.toString();
}
return null ;
}
}
再接下来,在源代码文件夹下新建xwork-conversion.properties,并在其中添加如下代码:
发布运行应用程序,在浏览器中键入http://localhost:8080/Struts2_Converter/HelloWorld.action,输出页面如图2所示:
图2 HelloWorld英文输出
在Locale输入框中输入“zh_CN”,按“Submit”提交,出现如图3所示页面:
图3 HelloWorld中文输出
上述例子中,Locale文本输入框对应是Action中的类型为java.util.Locale的属性loc,所以需要创建一个自定义转变器实现两者间的转换。所有的Struts 2.0中的转换器都必须实现ognl.TypeConverter接口。 为了简单起见,OGNL包也为你提供了ognl.DefaultTypeConverter类去帮助您实现转换器。在例子中,LocaleConverter继承了ognl.DefaultTypeConverter,重载了其方法原型为“public Object convertValue(Map context, Object value, Class toType)”的方法。下面简单地介绍一下函数的参数:
- context——用于获取当前的ActionContext
- value——需要转换的值
- toType——需要转换成的目标类型
- 配置全局的类型转换器,也即是上例的做法——在源代码文件夹下,新建一个名为“xwork-conversion.properties”的配置文件,并在文件中加入“待转换的类型的全名(包括包路径和类名)=转换器类的全名”对;
- 应用于某个特定类的类型转换器,做法为在该类的包中添加一个格式为“类名-conversion.properties”的配置文件,并在文件中加入“待转换的属性的名字=转换器类的全名”对。上面的例子也可以这样配置——在源代码文件夹的tutorial包下新建名为“HelloWorld-conversion.properties”文件,并在其中加入“loc=tutorial.LocaleConverter”。
在继承DefaultTypeConverter时,如果是要将value转换成其它非字符串类型时,要记住value是String[]类型,而不是String类型。它是通过request.getParameterValues(String arg)来获得的,所以不要试图将其强行转换为String类型。 |
已有的转换器
对于一此经常用到的转换器,如日期、整数或浮点数等类型,Struts 2.0已经为您实现了。下面列出已经实现的转换器。
- 预定义类型,例如int、boolean、double等;
- 日期类型, 使用当前区域(Locale)的短格式转换,即DateFormat.getInstance(DateFormat.SHORT);
- 集合(Collection)类型, 将request.getParameterValues(String arg)返回的字符串数据与java.util.Collection转换;
- 集合(Set)类型, 与List的转换相似,去掉相同的值;
- 数组(Array)类型, 将字符串数组的每一个元素转换成特定的类型,并组成一个数组。
批量封装对象(Bean)
不知道大家是否遇过这种情况,在一个页面里同时提交几个对象。例如,在发布产品的页面,同时发布几个产品。我在之前一个项目就遇到过这种需求,当时用的是Struts 1.x。那是一个痛苦的经历,我在Google搜了很久都没有理想的结果。幸运的是,在Struts 2.0中这种痛苦将一去不复返。下面我就演示一下如何实现这个需求。
首先,在源代码文件夹下的tutorial包中新建Product.java文件,内容如下:
import java.util.Date;
publicclass Product {
private String name;
privatedouble price;
private Date dateOfProduction;
public Date getDateOfProduction() {
return dateOfProduction;
}
publicvoid setDateOfProduction(Date dateOfProduction) {
this.dateOfProduction = dateOfProduction;
}
public String getName() {
return name;
}
publicvoid setName(String name) {
this.name = name;
}
publicdouble getPrice() {
return price;
}
publicvoid setPrice(double price) {
this.price = price;
}
}
然后,在同上的包下添加ProductConfirm.java类,代码如下:
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
publicclass ProductConfirm extends ActionSupport {
public List<Product> products;
public List<Product> getProducts() {
return products;
}
publicvoid setProducts(List<Product> products) {
this.products = products;
}
@Override
public String execute() {
for(Product p : products) {
System.out.println(p.getName() + " | "+ p.getPrice() +" | " + p.getDateOfProduction());
}
return SUCCESS;
}
}
接看,在同上的包中加入ProductConfirm-conversion.properties,代码如下:
再在struts.xml文件中配置ProductConfirm Action,代码片段如下:
<result>/ShowProducts.jsp</result>
</action>
在WEB文件夹下新建AddProducts.jsp,内容如下:
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Hello World</title>
</head>
<
发表评论
-
详解struts2中struts.properties
2007-10-25 00:17 2726Stru ... -
struts2的struts.properties配置文件详解
2007-10-24 23:55 2200struts.action.extension ... -
Struts 2 间接实现零配置
2007-10-19 11:41 1778所谓间接实现零配置,是指只要做些初始化的配置之后,在以后的开发 ... -
Parameters in configuration results
2007-10-10 17:12 1222Parameters in configura ... -
How do we upload files
2007-10-10 16:47 1493xml 代码 How do we upload ... -
display dynamic or static images
2007-10-10 16:38 1637How can we display dynamic or s ... -
Struts2 验证器
2007-10-08 22:30 4371<validators> ... -
初涉Struts 2.0
2007-10-06 09:43 1201初涉Struts 2.0... -
Apache Struts 2 Plugin Registry
2007-10-03 17:25 2155Apache Struts 2 Plugin Registry ... -
Struts 2 官方
2007-10-03 17:22 11417Struts 2 官方: http://struts.apac ... -
struts2与spring2集成
2007-10-03 17:12 1248在Eclipse下建立一个Dynamic Web Applic ... -
struts2的struts.properties配置文件详解
2007-10-03 17:02 1174struts.action.extension ... -
Struts 2与AJAX(第三部分)
2007-10-03 16:53 1511Struts 2与AJAX(第三部分 ... -
Struts 2与AJAX(第二部分)
2007-10-03 16:52 1652在上一篇文章《Struts 2与AJAX(第一部分)》,我已经 ... -
Struts 2与AJAX(第一部分)
2007-10-03 16:51 1671Struts 2与AJAX(第一部分) 在当今——Web 2 ... -
struts2表单标签
2007-10-03 16:48 1635Form Tags ... -
Struts2 with logon
2007-10-03 16:45 1167关于此工程中的所有设置是承接前一篇文章Struts2介绍之使用 ... -
Struts2介绍之使用链接标签
2007-10-03 16:42 1872Web应用区别于普通的站点之处在于Web应用可以创建一个动态的 ... -
Strus 2的新表单标志的使用
2007-10-03 16:35 1170Struts 2为大家提供了不少常用的很酷的表单标志,简化了我 ... -
在Struts 2中实现文件上传
2007-10-03 16:33 1242实现原理 Struts 2是通过Commons FileUpl ...
相关推荐
05 转换器(Converter)——Struts 2.0中的魔术师 06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts...
转换器(Converter)——Struts 2.0中的魔术师 在Struts 2.0中实现表单数据校验(Validation) Struts 2的基石——拦截器(Interceptor) 在Struts 2中实现IoC 在Struts 2中实现文件上传 在Struts 2中实现CRUD ...
05 转换器(Converter)——Struts 2.0中的魔术师 06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts...
05 转换器(Converter)——Struts 2.0中的魔术师 06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts...
1.为Struts 2.0做好准备 2.常用的Struts 2.0的标志(Tag)介绍 3.Struts 2.0的Action讲解 ...5.转换器(Converter)——Struts 2.0中的魔术师 在6.Struts 2.0中实现表单数据校验(Validation) 7....... 8.......
"转换器(Converter)——Struts 2_0中的魔术师" 可能解释了Converter的角色,它负责将HTTP请求参数转换为Action类中的属性,或者将Action的属性转换为适合视图展示的格式。 5. **标签(Tag)使用**: "常用的...
转换器是Struts2框架中的“魔术师”,因为它可以魔术般地将用户界面提交的不同格式的数据转换为Action类期望的类型。默认情况下,Struts2提供了一些基本类型的转换器,如字符串到整数、浮点数等。但当遇到自定义类型...
- 转换器是Struts2中的魔术师,它允许开发者自定义类型转换,将HTTP请求中的参数转换为Action类属性的合适类型。当模型对象的属性类型与请求参数不匹配时,Converter发挥作用,保证数据绑定的正确性。 3. **CRUD...