如何获得(读取)web.xml配置文件的參数


參考代码例如以下:

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;

    }

原文地址:https://www.cnblogs.com/yutingliuyl/p/7230810.html