Spring MVC - 从配置文件获得URL,消除硬编码
2016-07-08 11:05
387 查看
博主的环境:
Spring 3.2.4
Spring MVC 3.2.4
JDK 7
Tomcat 7.0.70
比如:
你写了一个软件,有菜单栏,你如果把菜单的标题全部写在代码里,那如果现在要换英文的,你就不得不改变代码。
现在换一种方式,你把菜单标题全部写在一个文本里,比如叫title.txt,现在你要英文,那么只要把title.txt里面相对应的值换成英文就可以了。
不用在去该代码本身。
这就是 使用与配置相分离的概念
Spring MVC 的 URL 不支持变量,所以下面的代码是错误的
Controller中:
在Spring MVC 的 WEB-INF/x-servlet.xml 中添加
顺便说一句, WEB-INF/x-servlet.xml 会覆盖 src/applicationContext.xml 的配置
完整的 WEB-INF/x-servlet.xml 如下:
Spring 3.2.4
Spring MVC 3.2.4
JDK 7
Tomcat 7.0.70
硬编码问题
硬编码 - 字符串或数字随便分散在代码各个位置,不容易修改比如:
你写了一个软件,有菜单栏,你如果把菜单的标题全部写在代码里,那如果现在要换英文的,你就不得不改变代码。
现在换一种方式,你把菜单标题全部写在一个文本里,比如叫title.txt,现在你要英文,那么只要把title.txt里面相对应的值换成英文就可以了。
不用在去该代码本身。
这就是 使用与配置相分离的概念
Spring MVC 的 URL 不支持变量,所以下面的代码是错误的
private static final String MY_URL="/city"; @ResponseBody @RequestMapping(value=MY_URL) public String getCity(){ String result=""; //to do return result; }
解决
配置文件名.properties 放在src目录下, 内容如下:city=/city
Controller中:
@ResponseBody @RequestMapping(value="${city}") public String getCity(){ String result=""; //to do return result; }
在Spring MVC 的 WEB-INF/x-servlet.xml 中添加
<context:component-scan base-package="你的包名" /> <context:property-placeholder location="classpath:配置文件名.properties"/>
顺便说一句, WEB-INF/x-servlet.xml 会覆盖 src/applicationContext.xml 的配置
完整的 WEB-INF/x-servlet.xml 如下:
<?xml version="1.0" encoding="UTF-8"?> <beans default-autowire="byName" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <context:component-scan base-package="com.pingan" /> <context:property-placeholder location="classpath:url.properties"/> <mvc:annotation-driven > <!-- 消息转换器, 解决返回值中文乱码问题 --> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/> </bean> </mvc:message-converters> </mvc:annotation-driven> </beans>
在@RequestMapping以外的地方使用PropertyPlaceHolder
类的属性@Value("${chengmi_crawl_timer_enable}") private Boolean timerEnabled; //不支持static变量
相关文章推荐
- java-用HttpURLConnection发送Http请求.
- 分享微信开发Html5轻游戏中的几个坑
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring AOP动态代理-切面
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- VBScript 剪贴板抓取URL并在浏览器中打开
- 用vbs 实现从剪贴板中抓取一个 URL 然后在浏览器中打开该 Web 站点
- asp获取URL参数的几种方法分析总结[原创]_应用技巧_脚本之家
- C# MVC模式下商品抽奖功能实现
- C#实现在网页中根据url截图并输出到网页的方法
- zend framework框架中url大小写问题解决方法
- Zend的MVC机制使用分析(二)
- url decode problem 解决方法
- php url地址栏传中文乱码解决方法集合