2025年list变为字符串(list字符串转化为list 对象)

list变为字符串(list字符串转化为list 对象)span id Label3 p span style font family Microsoft YaHei strong 1 strong Struts2 的类型转换 对于 8 个原生数据类型以及 Date String 等常见类型 span p span

大家好,我是讯享网,很高兴认识大家。




讯享网

 <span id="Label3"><p><span style="font-family: ‘Microsoft YaHei‘;"><strong>1.</strong> Struts2的类型转换,对于8个原生数据类型以及Date,String等常见类型,Struts2可以使用内建的类型转换器实现自动的转换;但对于自定义的类型转换来说,</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; 就需要我们自己指定类型转器的方式。</span></p><p><span style="font-family: ‘Microsoft YaHei‘;"><strong>2.</strong> 类型转换器必须继承DefaultTypeConverter,一般继承它的子类StrutsTypeConverter。action中调用set和get方法时,调用类型转换方法。</span></p><p><span style="font-family: ‘Microsoft YaHei‘;"><strong>3.</strong> 自定义类型转换器的实现步骤:</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; 1). 继承StrutsTypeConverter</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; 2). 覆盖convertFromString和convertToString</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;范例:</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public class UtilDateConverter extends StrutsTypeConverter {</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private static final String PATTERN = "yyyy/MM/dd";</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //浏览器提交到服务器是调用,将字符串转换成对象</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public Object convertFromString(Map context, String[] values, Class toClass) {</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //values为要转换属性的数组,可能有多个名字相同,所以取第一个。</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String dateString = values[0];</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SimpleDateFormat sdf = new SimpleDateFormat(PATTERN);</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Date date = null;</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try {</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//字符串,转换成日期类型。</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;date = sdf.parse(dateString);</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} catch (ParseException e) {</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e.printStackTrace();</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return date;</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span></p><p><span style="font-family: ‘Microsoft YaHei‘;"> <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//服务器向浏览器输出时调用,将某种类型转换成字符串。o是要转换成字符串的对象。</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public String convertToString(Map context, Object o) {</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Date date = (Date)o;</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new SimpleDateFormat("yyyy年MM月dd日").format(date);</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br /> <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; 3).注册类型转换器</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a).局部类型转换器,局部类型转换器只对当前Action起作用,需要提供如下配置文件:</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MyActionName-conversion.properties,MyActionName指需要使用转换器的Action名称&rdquo;-conversion.properties&ldquo;固定字符串,不能修改。</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 我们AddUserAction类型转换器的配置文件名称为:AddUserAction-conversion.properties,该配置文件必须和Action放到同一个目录中,该配</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 置文件的格式为:Action中的属性名称=转换器的完整路径,如:birthday=com.bjpowernode.struts2.UtilDateConverter&nbsp;也可以转换数组,</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 同样需要有set和get方法。 </span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b).全局类型转换器,全局类型转换器可以对所有的Action起作用(同Struts1的类型转换器),需要提供如下配置文件:</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xwork-conversion.properties(该名字固定),该配置文件需要放到src下,该配置文件的格式:需要转换的类型完整路径=转换器的完整路径,</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;如:java.util.Date=com.bjpowernode.struts2.UtilDateConverter自定义类型也可以转换。</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 补:请求页面:</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;出生日期:&lt;input type="text" name="birthday"&gt;&lt;br&gt;</span></p><p><span style="font-family: ‘Microsoft YaHei‘;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;显示页面:&lt;s:property value="http://www.mamicode.com/birthday"/&gt; 或 &lt;s:text name="date"&gt;&lt;/s:text&gt;</span></p><p><span style="font-family: ‘Microsoft YaHei‘;"><strong>4.</strong> 如果全局类型转换器和局部类型转换器同时存在,局部优先。</span></p><p><span style="font-family: ‘Microsoft YaHei‘;"><strong>5.</strong> 采用struts2标签读取属性get方法时可以调用转换器的convertToString。采用JSTL不会调用,显示的是默认格式的时间。</span></p><p><span style="font-family: ‘Microsoft YaHei‘;"><strong>6.</strong> 类型转换时,若发生异常,fieldError中会自动把异常加上,英文的。 </span></p><p><span style="font-family: ‘Microsoft YaHei‘;"><strong>7.</strong> 使用类型转换器须要在Action中提供属性的set和get方法</span></p><p>struts(类型转换器)</p></span> 

讯享网
小讯
上一篇 2025-06-16 19:58
下一篇 2025-04-15 08:50

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/146925.html