JAVA_WEB Struts2学习:struts2自定义类型转换类
来源:互联网 发布:男朋友生日钱包 知乎 编辑:程序博客网 时间:2024/06/10 17:48
OGNL学习:
JSP跟servlet的区别,JSP是不是客户端?
浏览器能看到的只有HTML。
拦截器的执行顺序为 :
在Action的控制方法执行之前,位于拦截器链前面的拦截器将先起作用
,在action的控制方法执行之后,位于拦截器链前面的拦截器将后起作用。自定义拦截器的实现,应该实现com.opensymphony.xwork2.interceptor.Interceptor接口,该接口有三个方法
1.void destory();//销毁该拦截器之前的回调方法
2.void init(); //初始化该拦截器的回调方法
3.String intercept(ActionInvocation invocation);//拦截器实现拦截的逻辑方法,如果该方法直接返回一个字符串,系统将会跳转到该逻辑视图对应的实际视图资源,不会调用被拦截的action。
以下是源代码演示:
1.index:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%@ taglib uri="/struts-tags" prefix="s" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><link rel="stylesheet" type="text/css" href="<%=path%>/js/My97DatePicker/skin/WdatePicker.css"><script language="javascript" type="text/javascript" src="<%=path%>/js/My97DatePicker/WdatePicker.js"></script> </head> <body> <s:form action="myAction!init"> <table><caption>OGNL练习</caption> <s:textfield name="age" label="年龄"></s:textfield> <s:fielderror fieldName="age"></s:fielderror> <s:textfield name="birthday" label="生日" onfocus="WdatePicker({isShowClear:false,readOnly:true,doubleCalendar:false,dateFmt:'yyyy-MM-dd HH:mm:ss'})" class="Wdate"> </s:textfield> <s:fielderror fieldName="birthday"></s:fielderror> <s:textfield name="point" label="坐标"></s:textfield> <s:fielderror fieldName="point"></s:fielderror> <s:submit value=" 提 交 "></s:submit> </table> </s:form> </body></html>
2.struts.xml:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><package name="testPage" extends="struts-default"><!-- 配置拦截器 --><interceptors><interceptor name="myInterceptor" class="com.zuxia.yc42.myinterceptor.MyInterceptor"></interceptor><!-- 自定义拦截器 --><interceptor-stack name="myStack"><!-- 把struts默认的拦截器组加上 --><interceptor-ref name="defaultStack"></interceptor-ref><!-- 加上我们自定义的拦截器 --><interceptor-ref name="myInterceptor"></interceptor-ref></interceptor-stack></interceptors><!--设置我们自己组装成的myStack拦截器组为默认拦截器 --><!--<default-action-ref name="myStack"></default-action-ref>--><action name="testAction1" class="com.zuxia.yc42.action.TestAction1"><result name="success">/jsp/success.jsp</result><!-- 调用我们自己组装成的myStack拦截器组 --><interceptor-ref name="myStack"></interceptor-ref></action><action name="testAction2" class="com.zuxia.yc42.action.MyTestaAction1"><result name="success">/jsp/success.jsp</result><result name="input">/jsp/point.jsp</result></action></package></struts>
3.xwork-conversion.properties:
com.zuxia.yc42.util.PointConverter=com.zuxia.yc42.util.DateConverter
4.com.zuxia.yc42.util.DateConverter extends StrutsTypeConverter :
package com.zuxia.yc42.util;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Map;import org.apache.struts2.util.StrutsTypeConverter;public class DateConverter extends StrutsTypeConverter {private SimpleDateFormat[] sm = {new SimpleDateFormat("yyyy年MM月dd日"), new SimpleDateFormat("yyyy-MM-dd"), new SimpleDateFormat("yyyy.MM.dd"), new SimpleDateFormat("yyyy/MM/dd"), new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"), new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"),};@Overridepublic Object convertFromString(Map context, String[] values, Class toType) {String time = values[0];Date date = null;for(SimpleDateFormat simple:sm){try {date = simple.parse(time);} catch (ParseException e) {continue;}}return date;}@Overridepublic String convertToString(Map context, Object obj) {SimpleDateFormat simple = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");return simple.format((Date)obj);}}
5.com.zuxia.yc42.util.PointConverter extends StrutsTypeConverter:
package com.zuxia.yc42.util;import java.util.Map;import org.apache.struts2.util.StrutsTypeConverter;import com.zuxia.yc42.po.Point;/** * 自定义类型转换类 * @author wang_hao * */public class PointConverter extends StrutsTypeConverter {/** * 将客户端数据转换成服务器端数据类型 */@Overridepublic Object convertFromString(Map context, String[] values, Class toType) {String value = values[0];String[] vals = value.split(",");Integer x = Integer.parseInt(vals[0]);Integer y = Integer.parseInt(vals[1]);Point point = new Point();point.setX(x);point.setY(y);return point;}/** * 将服务器端数据类型转换为String类型 */@Overridepublic String convertToString(Map context, Object obj){return obj.toString();}}
6.com.zuxia.yc42.action.MyTestAction extends ActionSupport:
package com.zuxia.yc42.action.MyTestAction extends ActionSupport;import java.util.Date;import com.opensymphony.xwork2.ActionSupport;import com.zuxia.yc42.po.Point;public class MyTestAction extends ActionSupport{private Integer age;private Date birthday;private Point point;public String init(){Object o = new Object();System.out.println(age.toString()+"\t"+birthday+"\t"+point);return SUCCESS;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public Point getPoint() {return point;}public void setPoint(Point point) {this.point = point;}}
- JAVA_WEB Struts2学习:struts2自定义类型转换类
- JAVA_WEB Struts2学习:struts2自定义拦截器
- Struts2自定义类型转换
- struts2自定义类型转换
- Struts2自定义类型转换
- Struts2自定义类型转换
- struts2自定义类型转换
- struts2自定义类型转换
- JAVA_WEB Struts2学习:Struts2标签学习
- JAVA_WEB Struts2学习:Struts2标签学习2
- struts2.x自定义类型转换
- Struts2 自定义日期类型转换
- JAVA_WEB Struts2学习:Struts2常用标签
- JAVA_WEB Struts2学习:struts2标签详解
- Struts2类型转换和自定义类型
- Struts2学习:类型转换篇
- struts2入门学习--类型转换
- Struts2学习之类型转换
- 学习计算机,学习电脑,打字是一个开始,下面,分享一下个人收集的技巧!
- Java Programming: simple Polymorphic programming
- [poj 1364]King[差分约束详解(续篇)][超级源点][SPFA][Bellman-Ford]
- Struts2的属性驱动与模型驱动的区别
- JSP/Servlet 中的汉字编码问题
- JAVA_WEB Struts2学习:struts2自定义类型转换类
- hdu1869解题报告--六度分离
- linux select 多路复用机制
- wikioi p1076 排序
- [matlab]mathworks上的cody challenge题解及一些常用函数的总结(4)
- iOS 用UIScrollView不能获取到touchesBegan
- 对C/C++中的static与extern关键字的使用
- JSP/Servlet的编码原理
- structs 8-23