參考代码例如以下:

com.atguigu.struts2.app.converters.DateConverter.java

public DateFormat getDateFormat(){

        if(dateFormat == null){

            //获取当前 WEB 应用的初始化參数 pattern(该參数在WEB-INF下的web.xml文件里)

            //第一次用的时候获取,并载入相关的信息

            //这样的方法是使用/struts2-6/src/xwork-conversion.properties配置文件

            //xwork-conversion.properties配置文件的内容:

            //java.util.Date=com.atguigu.struts2.app.converters.DateConverter

            ServletContext servletContext = ServletActionContext.getServletContext();

            System.out.println(servletContext);

            String pattern = servletContext.getInitParameter("pattern");

            dateFormat = new SimpleDateFormat(pattern);

        }

        return dateFormat;

    }

 

 

posted on 2017-07-24 20:10  yutingliuyl  阅读(201)  评论(0编辑  收藏  举报