第一步:創建資源文件java
1.Messages.propertiesweb
vo.edit.msg = {0} \u4fe1\u606f\u7f16\u8bd1\u5b8c\u6210\uff01
2.Pages.propertiesspring
emp.add.page=/pages/back/emp/emp_add.jsp
3.Validations.propertiesapache
emp.add.rules=empno:int|ename:string|sal:double|hiredate:date
第二步:配置applicationContext-mvc.xmlmvc
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <array> <value>Messages</value> <value>Pages</value> <value>Validations</value> </array> </property> </bean>
第三步:父類中定義讀取方法app
package group.esperanto.action.util; import java.text.SimpleDateFormat; import java.util.Locale; import javax.annotation.Resource; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.context.MessageSource; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; public class AbstractAction { @Resource //經過容器注入msgSource private MessageSource msgSource; // 定義讀取資源文件內容 msgKey指properties文件中的鍵,args指傳過來的參數,替代properties中的佔位符 public String getValue(String msgKey,Object...args){ return this.msgSource.getMessage(msgKey, args, Locale.getDefault()); } }
第四步:控制器中讀取jsp
package group.esperanto.action; import java.io.IOException; import java.util.Date; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.sound.midi.MidiDevice.Info; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import group.esperanto.action.util.AbstractAction; @Controller // 定義該Action的映射路徑 // 該路徑絕對不能重複 @RequestMapping("/pages/emp/*") public class EmpAction extends AbstractAction { private Logger log = Logger.getLogger(EmpAction.class); @RequestMapping("info") public ModelAndView Info(){ log.info(super.getValue("vo.edit.msg", "info ")); log.info(super.getValue("emp.add.page")); log.info(super.getValue("emp.add.rules")); return null; } }