标签:util submit pack .com 输入 getc 目标 sof 求和
这里只讲日期类型转换器的自定义
Struts2中有一些自定义类型转化器位置:
但是此处的日期类型转换器只是将yyyy-MM-dd日期格式转化成为Tue Jun 06 00:00:00 CST 2017
而对于产品来说可能涉及到yyyy/MM/dd和yyyyMMdd等,接下来就看例子吧!
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib uri="/struts-tags" prefix="s"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>My First Struts2 Project</title> 9 </head> 10 <body> 11 12 <!-- struts2的标签能够实现数据的回显,变返回那条数据出现了错误 --> 13 <s:form action="system/login1_login.action" method="post"> 14 <s:textfield name="birthday" label="生日"/> 15 <s:textfield name="age" label="年龄"/> 16 <s:submit value="登录"></s:submit> 17 </s:form> 18 <!-- 19 <form action="system/login1_login.action" method="post"> 20 生日<input type="text" name="birthday"><br> 21 年龄<input type="text" name="age"><br> 22 <input type="submit" value="登录"> 23 </form> --> 24 </body> 25 </html>
1 package com.bjyinfu.struts.actions; 2 3 import java.util.Date; 4 5 import com.opensymphony.xwork2.ActionSupport; 6 7 //当Action方法继承这个ActionSupport类的时候,若输入的值不符合规定会自动跳到视图结果为input的视图上 8 //即在struts.xml中在定义一个<result name="input">/输入视图.jsp</result> 9 //这样做是为了当数据出现失误的时候能够进行数据回显 10 11 public class LoginAction1 extends ActionSupport{ 12 13 private Date birthday; 14 private int age; 15 16 17 public Date getBirthday() { 18 return birthday; 19 } 20 21 public void setBirthday(Date birthday) { 22 this.birthday = birthday; 23 } 24 25 public int getAge() { 26 return age; 27 } 28 29 public void setAge(int age) { 30 this.age = age; 31 } 32 33 public String login(){ 34 System.out.println("执行了登录方法"); 35 return "success"; 36 } 37 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 7 <package name="login" namespace="/system" extends="struts-default"> 8 <action name="login1_*" class="com.bjyinfu.struts.actions.LoginAction1" method="login"> 9 <result name="success">/welcome04.jsp</result> 10 <result name="input">/login1.jsp</result> 11 </action> 12 </package> 13 </struts>
1 package com.bjyinfu.typeConvertr; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 import java.util.regex.Pattern; 7 8 import com.opensymphony.xwork2.ActionContext; 9 import com.opensymphony.xwork2.conversion.TypeConversionException; 10 import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter; 11 12 public class MyTypeConverter extends DefaultTypeConverter { 13 14 //在局部类型转换定义完后要进行注册,在Action类的同包下创建后缀为properties的文件 15 //文件命名格式:ActionClassName-conversion.properties 16 //ActionClassName是Action方法的类名,conversion.properties为固定写法 17 // value:将要被转化的数据 18 // toType:被转换成的目标类型 19 // 页面--->服务器端 String[]-->Date,为了兼顾复选框 20 // 服务器端-->页面 Date-->String 21 @Override 22 public Object convertValue(Object value, Class toType) { 23 24 SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd"); 25 try { 26 if(toType == Date.class){//实现从页面到服务器端的String-->Date 27 String dateStr=((String[])value)[0]; 28 sdf=getSimpleDateFormat(dateStr); 29 //把数据放到session中然后会显得时候再从session中去取 30 ActionContext.getContext().getSession().put("sdf", sdf); 31 /*if(!Pattern.matches("^\\d{4}/\\d{2}/\\d{2}", dateStr)){ 32 throw new TypeConversionException(); 33 }*/ 34 35 return sdf.parse(dateStr); 36 }else {//实现服务端到页面的转换 Date-->String 37 Date date=(Date)value; 38 sdf=(SimpleDateFormat)ActionContext.getContext().getSession().get("sdf"); 39 return sdf.format(date); 40 } 41 42 } catch (ParseException e) { 43 44 e.printStackTrace(); 45 } 46 47 return super.convertValue(value, toType); 48 } 49 50 private SimpleDateFormat getSimpleDateFormat(String dateStr) { 51 SimpleDateFormat sdf=null; 52 if(Pattern.matches("^\\d{4}/\\d{2}/\\d{2}$", dateStr)){ 53 sdf=new SimpleDateFormat("yyyy/MM/dd"); 54 }else if(Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$", dateStr)){ 55 sdf=new SimpleDateFormat("yyyy-MM-dd"); 56 }else if(Pattern.matches("^\\d{4}\\d{2}\\d{2}$", dateStr)){ 57 sdf=new SimpleDateFormat("yyyyMMdd"); 58 }else{ 59 throw new TypeConversionException(); 60 } 61 return sdf; 62 } 63 64 65 }
1 birthday = com.bjyinfu.typeConvertr.MyTypeConverter
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib uri="/struts-tags" prefix="s" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>My First Struts2 Project</title> 9 </head> 10 <body> 11 <s:debug></s:debug><br> 12 birthday=<s:property value="birthday"/><br> 13 age=<s:property value="age"/><br> 14 <!-- 下面显示的日期是Tue Jun 06 00:00:00 CST 2017这种格式,上面显示的日期是输入的原值 --> 15 birthday=${birthday }<br> 16 age=${age }<br> 17 </body> 18 </html>
1 invalid.fieldvalue.age=\u5E74\u9F84\u5E94\u8BE5\u4E3A\u6574\u6570 2 invalid.fieldvalue.birthday=\u65E5\u671F\u683C\u5F0F\u4E0D\u6B63\u786E
标签:util submit pack .com 输入 getc 目标 sof 求和
原文地址:http://www.cnblogs.com/lubolin/p/7264463.html