32-35. Struts2_类型转换错误消息的显示和定制&自定义类型转换器&类型转换器与复杂属性&集合协同使用
来源:互联网 发布:苹果手机蓝牙软件 编辑:程序博客网 时间:2024/05/22 10:37
类型转换错误消息的显示和定制:
ConversionAction
package com.hgh.struts2.conversion;import com.hgh.struts2.pojo.Customer;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;public class ConversionAction extends ActionSupport implements ModelDriven<Customer>{ /** * */ private static final long serialVersionUID = 1L; private Customer customer; @Override public String execute() throws Exception { System.out.println("model:"+customer); return super.execute(); } @Override public Customer getModel() { // TODO Auto-generated method stub customer = new Customer(); return customer; }}
<?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> <!-- 配置struts2的默认主题 simple: 把 UI 标签翻译成最简单的 HTML 对应元素, 而且会忽视行标属性xhtml: xhtml 是默认的主题. 这个主题的模板通过使用一个布局表格提供了一种自动化的排版机制. css_xhtml: 这个主题里的模板与 xhtml 主题里的模板很相似, 但它们将使用 css 来进行布局和排版ajax: 这个主题里的模板以 xhtml 主题里德模板为基础, 但增加了一些 Ajax 功能. --> <constant name="struts.ui.theme" value="xhtml"></constant> <!-- 配置 Struts 可以受理的请求的扩展名 --> <constant name="struts.action.extension" value="action,do"></constant> <!-- 配置 <constant name="struts.custom.i18n.resources" value="i18n"></constant>--> <package name="conversion" extends="struts-default"> <action name="testconversion" class="com.hgh.struts2.conversion.ConversionAction"> <result >/success.jsp</result> <result name="input">/conversion.jsp</result> </action> </package></struts>
conversion.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %><!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> <!-- 问题1: 如何覆盖默认的错误消息? 1). 在对应的 Action 类所在的包中新建 ActionClassName.properties 文件, ActionClassName 即为包含着输入字段的 Action 类的类名 2). 在属性文件中添加如下键值对: invalid.fieldvalue.fieldName=xxx invalid.fieldvalue.age=xxx 问题2: 如果是 simple 主题, 还会自动显示错误消息吗? 如果不会显示, 怎么办 ? 1). 通过 debug 标签, 可知若转换出错, 则在值栈的 Action(实现了 ValidationAware 接口) 对象中有一个 fieldErrors 属性. 该属性的类型为 Map<String, List<String>> 键: 字段(属性名), 值: 错误消息组成的 List. 所以可以使用 LE 或 OGNL 的方式 来显示错误消息: ${fieldErrors.age[0]} 2). 还可以使用 s:fielderror 标签来显示. 可以通过 fieldName 属性显示指定字段的错误. 问题3. 若是 simple 主题, 且使用 <s:fielderror fieldName="age"></s:fielderror> 来显示错误消息, 则该消息在一个 ul, li, span 中. 如何去除 ul, li, span 呢 ? 在 template.simple 下面的 fielderror.ftl 定义了 simple 主题下, s:fielderror 标签显示错误消息的样式. 所以修改该 配置文件即可. 在 src 下新建 template.simple 包, 新建 fielderror.ftl 文件, 把原生的 fielderror.ftl 中的内容 复制到新建的 fielderror.ftl 中, 然后剔除 ul, li, span 部分即可. 问题4. 如何自定义类型转换器 ? 1). 为什么需要自定义的类型转换器 ? 因为 Struts 不能自动完成 字符串 到 引用类型 的 转换. 2). 如何定义类型转换器: I. 开发类型转换器的类: 扩展 StrutsTypeConverter 类. II. 配置类型转换器: 有两种方式 ①. 基于字段的配置: > 在字段所在的 Model(可能是 Action, 可能是一个 JavaBean) 的包下, 新建一个 ModelClassName-conversion.properties 文件 > 在该文件中输入键值对: fieldName=类型转换器的全类名. > 第一次使用该转换器时创建实例. > 类型转换器是单实例的! ②. 基于类型的配置: > 在 src 下新建 xwork-conversion.properties > 键入: 待转换的类型=类型转换器的全类名. > 在当前 Struts2 应用被加载时创建实例. <s:textfield name="birth" label="Birth"></s:textfield> --><s:debug></s:debug><s:form action="testconversion"> <s:textfield name="age" label="Age"></s:textfield> <s:textfield name="birth" label="Birth"></s:textfield> <s:submit name="submit"></s:submit></s:form></body></html>
ConversionAction.properties
invalid.fieldvalue.age=\u9519\u8BEF\u7684\u5E74\u9F84\u683C\u5F0F
i18n.properties
这里写代码片
自定义类型转换器
DateConverter
package com.hgh.struts2.converters;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Map;import javax.servlet.ServletContext;import org.apache.struts2.ServletActionContext;import org.apache.struts2.util.StrutsTypeConverter;public class DateConverter extends StrutsTypeConverter { private DateFormat dateFormat; public DateConverter() { //dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); System.out.println("DateConverter's constructor..."); } public DateFormat getDateFormat(){ if(dateFormat == null){ //获取当前 WEB 应用的初始化参数 pattern ServletContext servletContext = ServletActionContext.getServletContext(); System.out.println(servletContext); String pattern = servletContext.getInitParameter("datePattern"); dateFormat = new SimpleDateFormat(pattern); } return dateFormat; } @Override public Object convertFromString(Map context, String[] values, Class toClass) { System.out.println("convertFromString..."); if(toClass == Date.class){ if(values != null && values.length > 0){ String value = values[0]; try { return getDateFormat().parseObject(value); } catch (ParseException e) { e.printStackTrace(); } } } //若没有转换成功, 则返回 values return values; } @Override public String convertToString(Map context, Object o) { System.out.println("convertToString..."); if(o instanceof Date){ Date date = (Date) o; return getDateFormat().format(date); } //若转换失败返回 null return null; }}
xwork-conversion.properties
java.util.Date=com.hgh.struts2.converters.DateConverter
类型转换器与复杂属性&集合协同使用
复杂属性主要是使用:
类似部门包含管理者,则使用部门中的管理者属性名称.管理者对象的属性名称的方式为其赋值
/**
* 1. Department 是模型, 实际录入的 Department. deptName 可以直接写到
* s:textfield 的 name 属性中. 那 mgr 属性如何处理呢 ?
*
* struts2 表单标签的 name 值可以被赋为 属性的属性: name=mgr.name, name=mgr.birth
*
* 2. mgr 中有一个 Date 类型的 birth 属性, Struts2 可以完成自动的类型转换吗 ?
*
* 全局的类型转换器可以正常工作!
*
*/
集合协同使用
主要是对集合对象,使用[0],[1]的方式赋值
complext-property.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %> <!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:form action="testComplextProperty"> <s:textfield name="deptName" label="DeptName"></s:textfield> <!-- 映射属性的属性 --> <s:textfield name="mgr.name" label="MgrName"></s:textfield> <s:textfield name="mgr.birth" label="MgrBirth"></s:textfield> <s:submit></s:submit> </s:form></body></html>
<action name="testComplextProperty" class="com.hgh.struts2.conversion.TestComplexPropertyAction"> <result >/success.jsp</result> <result name="input">/complext-property.jsp</result> </action> <action name="testConversion2" class="com.hgh.struts2.conversion.TestCollectionAction"> <result >/success.jsp</result> <result name="input">/manager-input.jsp</result> </action>
TestComplexPropertyAction
package com.hgh.struts2.conversion;import com.hgh.struts2.pojo.Department;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;public class TestComplexPropertyAction extends ActionSupport implements ModelDriven<Department>{ /** * */ private static final long serialVersionUID = 1L; private Department dept; public Department getDept() { return dept; } @Override public String execute() throws Exception { // TODO Auto-generated method stub System.out.println(dept); return super.execute(); } @Override public Department getModel() { // TODO Auto-generated method stub dept = new Department(); return dept; }}
manager-input.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %> <!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><script type="text/javascript" src="scripts/jquery-1.4.2.js"></script><script type="text/javascript"> $(function(){ var count = 0; $("#add").click(function(){ count++; $("#button").before("<tr><td>Mgrs[" + count + "].Name:</td><td><input name='mgrs[" + count + "].name'/></td></tr>") .before("<tr><td>Mgrs[" + count + "].Birth:</td><td><input name='mgrs[" + count + "].birth'/></td></tr>"); return false; }); $("#remove").click(function(){ count--; $("#button").prev("tr").remove(); $("#button").prev("tr").remove(); return false; }); });</script></head><body> <form action="testConversion2.action"> <table> <tbody> <tr> <td>Mgrs[0].Name:</td> <td><input name="mgrs[0].name"/></td> </tr> <tr> <td>Mgrs[0].Birth:</td> <td><input name="mgrs[0].birth"/></td> </tr> <tr id="button"> <td><button id="add">新加一个</button></td> <td><button id="remove">删除一个</button></td> </tr> <tr> <td colspan="2" align="right"> <input type="submit" value="Submit"/> </td> </tr> </tbody> </table> </form></body></html>
TestCollectionAction
package com.hgh.struts2.conversion;import java.util.Collection;import com.hgh.struts2.pojo.Manager;import com.opensymphony.xwork2.ActionSupport;public class TestCollectionAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private Collection<Manager> mgrs = null; public Collection<Manager> getMgrs() { return mgrs; } public void setMgrs(Collection<Manager> mgrs) { this.mgrs = mgrs; } public String execute() throws Exception { System.out.println(mgrs); return SUCCESS; }}
阅读全文
0 0
- 32-35. Struts2_类型转换错误消息的显示和定制&自定义类型转换器&类型转换器与复杂属性&集合协同使用
- Struts2_类型转换错误消息的显示和定制
- 18.struts2_类型转换,错误消息的显示和定制
- 26.Struts2_类型转换错误消息的显示和定制
- Struts2_局部类型转换器
- Struts2_全局类型转换器
- Struts2_类型转换器
- Struts2的类型转换及输入校验(自定义类型转换器)
- 数组属性的类型转换器
- Struts2的自定义类型转换器
- Struts2的自定义类型转换器
- 自定义类型转换器的步骤
- springmvc的自定义类型转换器
- springMVC的自定义类型转换器
- 集合类型转换器实例
- 27.Struts2_自定义类型转换器(承接26)
- 自定义类型转换器
- Struts2自定义类型转换器
- 运行mvn help:describe -Dplugin=tomcat7,build failure
- gradle outputfile
- 2017年五月Python学习疑问
- 测试工具小记
- 我开通博客啦
- 32-35. Struts2_类型转换错误消息的显示和定制&自定义类型转换器&类型转换器与复杂属性&集合协同使用
- git在本地创建的仓库无法提交到远程仓库解决办法
- 关于android自定义控件的学习心得小结
- jQuery Mobile 按钮图标
- BZOJ1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛
- “集体智慧编程”之第五章:“求最优解”的算法
- VUE
- 后缀表达式
- ubuntu16.04下安装opencv3.1+anaconda2+caffe