springmvc.xml(Springmvc的核心配置文件,读取properties文件):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置@Controller扫描 -->
<context:component-scan base-package="com.xxx.crm.controller" />
<!-- 读取.properties文件中的内容。 通过${键}的方式可以获取properties中对应的值。 -->
<context:property-placeholder location="classpath:resource.properties" />
<!-- 配置注解驱动; 指定处理器映射器、处理器适配器; -->
<mvc:annotation-driven />
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
CustomerController.java(Controller后端控制器,@Value注解获取properties文件中的内容):
package com.xxx.crm.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.xxx.crm.pojo.BaseDict;
import com.xxx.crm.pojo.QueryVo;
import com.xxx.crm.service.BaseDictService;
//客户管理
@Controller
public class CustomerController {
@Autowired
private BaseDictService baseDictService;
//注解在成员变量上
//通过@Value注解,将springmvc.xml文件中加载的xxx.properties中的内容赋给成员变量。
@Value("${fromType.code}")
private String fromTypeCode;
@RequestMapping(value = "/customer/list")
public String list(QueryVo vo,Model model){
List<BaseDict> fromType = baseDictService.selectBaseDictListByCode(fromTypeCode);
model.addAttribute("fromType", fromType);
//..................
return "customer";
}
}
resource.properties(properties文件):
fromType.code=002