资源读取配置

第一步:建立资源文件

1.Messages.properties

vo.edit.msg = {0} \u4fe1\u606f\u7f16\u8bd1\u5b8c\u6210\uff01

2.Pages.properties

emp.add.page=/pages/back/emp/emp_add.jsp

3.Validations.properties

emp.add.rules=empno:int|ename:string|sal:double|hiredate:date

第二步:配置applicationContext-mvc.xml

<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>

第三步:父类中定义读取方法

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());
	}
}

第四步:控制器中读取

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;
	}
	
}

 

转载于:https://my.oschina.net/u/3647851/blog/1507359

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值