当前位置: 代码迷 >> J2EE >> 求处理含有占位符(Placeholder)的字符串的工具类库!解决方案
  详细解决方案

求处理含有占位符(Placeholder)的字符串的工具类库!解决方案

热度:94   发布时间:2016-04-22 02:15:11.0
求处理含有占位符(Placeholder)的字符串的工具类库!
本人想未来把自己的产品进行国际化,所以把程序给外部系统做出的所有提示信息都存放到文件messages.properties。
Example:
messages.properties
Java code
web.services.members.response.msg.notfound=\u5bf9\u4e0d\u8d77\uff0c\u6ca1\u6709\u53d1\u73b0\u4e0e[${username}]\u76f8\u5339\u914d\u7528\u6237\uff01web.services.members.response.msg.found=\u5171\u627e\u5230\u4e0e[${username}]\u76f8\u5339\u914d\u7684\u7528\u6237${count}\u4e2a\uff01web.services.members.response.msg.exception=\u670d\u52a1\u5668\u51fa\u73b0\u5f02\u5e38\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5\uff01web.services.members.response.msg.error=\u670d\u52a1\u5668\u51fa\u73b0\u5185\u90e8\u9519\u8bef\uff01

messages.properties实际内容如下:
web.services.members.response.msg.notfound=没有发现与[${username}]相匹配用户!
web.services.members.response.msg.found=共找到与[${username}]相匹配的用户${count}个!
web.services.members.response.msg.exception=服务器出现异常,请稍后再试!
web.services.members.response.msg.error=服务器出现内部错误!

稍后,我新创建一个类MessageBundle:
Java code
package com.dongantech.eshop.web.util;import java.util.*;import java.io.*;public class MessageBundle {    public static Properties PROPS = null ;        private Map bundle =  null;        static {        InputStream propertiesInputStream = MessageBundle.class.getResourceAsStream("/messages.properties");        PROPS = new Properties();        try {            PROPS.load(propertiesInputStream);        } catch (IOException e) {            e.printStackTrace();        }    }    public MessageBundle(){        bundle = new HashMap();    }    public void setBundle(Map theBundle){        bundle = theBundle;    }    public Map getBundle(){        return bundle ;    }    public String getMessage(String theMsgKey){        return (String)bundle.get(theMsgKey);    }    public void putIntoBundle(String theMsgKey){        String msgFrame = MessageBundle.PROPS.getProperty(theMsgKey);        bundle.put(theMsgKey,msgFrame);    }}

这样,我的Servlet就自己去打包所需要的信息成一个MessageBundle对象,由于信息反馈对于系统来说是很频繁的活动,如果每一个这样的对象(Servlet)都包含一个MessageBundle对象,就会避免了多个Servlet间对某信息集合对象(使用单例模式方式)请求占用所带来的执行延时。
Java code
public class MemberManagerServlet extends HttpServlet{    public static final String MSG_KEY_NOT_FOUND = "web.services.members.response.msg.notfound" ;     public static final String MSG_KEY_FOUND = "web.services.members.response.msg.found" ;    public static final String MSG_KEY_EXCEPTION = "web.services.members.response.msg.exception" ;    public static final String MSG_KEY_ERROR = "web.services.members.response.msg.error" ;        private Map requestHandlers = null ;    private PageLocator pageLocator = null ;    private MessageBundle messageBundle = null ;        public MemberManagerServlet(){        requestHandlers = new HashMap();        pageLocator = new PageLocator();                pageLocator.registerPage(SimpleResponse.TYPE_FOUND,MemberManagerServlet.PAGE_FOUND );        pageLocator.registerPage(SimpleResponse.TYPE_NOT_FOUND,MemberManagerServlet.PAGE_NOT_FOUND);        pageLocator.registerPage(SimpleResponse.TYPE_EXCEPTION,MemberManagerServlet.PAGE_EXCEPTION);        pageLocator.registerPage(SimpleResponse.TYPE_ERROR,MemberManagerServlet.PAGE_ERROR);                messageBundle.putIntoBundle(MemberManagerServlet.MSG_KEY_NOT_FOUND);        messageBundle.putIntoBundle(MemberManagerServlet.MSG_KEY_FOUND);        messageBundle.putIntoBundle(MemberManagerServlet.MSG_KEY_EXCEPTION);        messageBundle.putIntoBundle(MemberManagerServlet.MSG_KEY_ERROR);    }    public void init(ServletConfig config){        try {            super.init(config);        } catch (ServletException e) {            e.printStackTrace();        }    }    public void doGet(HttpServletRequest httpReq,HttpServletResponse httpResp){        Response response = processRequest(httpReq,httpResp);        try {            deliver(response);        } catch (Exception e) {            e.printStackTrace();        }    }        public void doPost(HttpServletRequest httpReq,HttpServletResponse httpResp){        Response response = processRequest(httpReq,httpResp);        try {            deliver(response);        } catch (Exception e) {            e.printStackTrace();        }    }        public Response processRequest(HttpServletRequest httpReq,HttpServletResponse httpResp){        Response response = null ;        String handlerName = httpReq.getParameter("request");        RequestHandler handler = getHandler(handlerName);        Request request = buildRequest(httpReq);        try {            response = handler.process(request);            response.getContext().put("page",getPage(response.getType()));        } catch (Exception e) {            response = new SimpleResponse(SimpleResponse.TYPE_ERROR);            e.printStackTrace();        }        return response ;    }}
  相关解决方案