首页 技术 正文
技术 2022年11月8日
0 收藏 488 点赞 1,556 浏览 2501 个字

为什么进行类型转换

在基于HTTP协议的Web应用中

客户端请求的所有内容都以文本编码方式传输到服务器端

服务器端的编程语言却有着丰富的数据类型

继承StrutsTypeConverter抽象类

继承org.apache.struts2.util. StrutsTypeConverter类

应用于全局范围的类型转换器

在src目录创建xwork-conversion.properties

转换类全名=类型转换器类全名

应用于特定类的类型转换器

在特定类的相同目录下创建一个名为ClassName-conversion.properties的属性文件

     特定类的属性名=类型转换器类全名

向用户输出类型转换错误的前提条件

启动StrutsConversionErrorInterceptor拦截器 

拦截器已经包含在defaultStack拦截器栈中 

Action要继承ActionSupport类 

其实是要实现ValidationAware接口 

配置input结果映射

页面使用Struts 2表单标签或<s:fielderror>标签

Struts 2表单标签内嵌了输出错误信息功能

普通HTML标签需使用<s:fielderror>标签输出转换错误

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:fielderror/>
<s:form action="demo4Action">
<s:textfield name="birthday" label="出生日期"/><br>
<s:textfield name="tel" label="电话号码"/><br>
<s:submit value="submit"/>
</s:form></body>
</html>

转换器类:

public class DateConvert extends StrutsTypeConverter {//将字符串转换为对象类型
public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
Date dt=null;
// 获取页面要转换的值
if(arg1[0]!=null&&!"".equals(arg1[0])){String dateStr =arg1[0];//封装一个转的目标类型
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
//将字符串转换为日期
dt= sdf.parse(dateStr);
} catch (ParseException e) {e.printStackTrace();
}
}
return dt;
}//将对象转换为页面所需要的类型
public String convertToString(Map arg0, Object arg1) {
String str="";
if(arg1!=null){
if(arg1 instanceof Date){
Date date = (Date)arg1;
//date 对象转换为字符串
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
str = sdf.format(date);
}}return str;
}}

配置文件:

java.util.Date = com.org.converter.util.DateConvert

com.org.entity.Telephone = com.org.converter.util.TelConverter

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="false"/>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<constant name="struts.configuration.xml.reload" value="true"/><constant name="struts.custom.i18n.resources" value="abc"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="demo1action" class="com.org.ognl.Demo1Action">
<result>/success.jsp</result>
</action><action name="demo3Action" class="com.org.ognl.Demo3Action">
<result name="result">/result.jsp</result>
</action>
</package>
<include file="com/org/converter/action/struts.xml"></include>
</struts>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919