测试MessageFomat

package TestI18N;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.MessageFormat;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestMessageFormat extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        ResourceBundle resource =ResourceBundle.getBundle("resouce_ch2");
        String requiredMessage =resource.getString("error.requiredfield");
        Object messageAgs[]=new Object[2];
        messageAgs[0]=resource.getString("label.phone");
        messageAgs[1]=resource.getString("label.name");
        
        String formattedNameMessage=MessageFormat.format(requiredMessage, messageAgs[0]);
        System.out.println(formattedNameMessage);
        System.out.println("--------------------");
        String formattedNameMessage1=MessageFormat.format(requiredMessage, messageAgs[1]);
	    System.out.println(formattedNameMessage1);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        
	   doGet(request, response);
	}

}

error.requiredfield=The {0} field is required to save
label.phone=Phone
label.name=Name


输出结果:
The Phone field is required to save
--------------------
The Name field is required to save

你可能感兴趣的:(java,servlet)