tips: 启动项目后,welcome-file的链接即为测试用例

  1. 部署maven web项目

 <dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
  • 配置jdk版本,在build->plugins节点中添加:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
  1. 整合spring
  2. 整合log4j
  3. 配置dataSource

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.7</version>
</dependency>

“`

* [编程方式取得Spring上下文的Properties]

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&amp;characterEncoding=utf-8
  1. 整合mybatis

  2. 整合mybatis-generator
  3. 用junit测试一下
  4. 整合springMVC

    一、[编程方式取得Spring上下文的Properties]

    在Spring初始化时,可以使用Properties配置器把properties文件装载到Spring的上下文中。

xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=“http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd”
<context:property-placeholder location="classpath:dataSource.properties" />

这样在Spring的配置文件中可以用表达式来获得load进来的properties内容,例如:

<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />

有时候我们在程序中也需要用到这些配置,那么如何取值,显然不能使用${}方式的。

这时要决定用什么方式来获取properties了,最方便的当然是直接读取文件,此处省略。

如果程序一定要用通过Spring加载的properties,那么我们首先要得到Context了。

1、FileSystemXmlApplicationContext——从指定的目录中加载:

ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml"); 

2、ClassPathXmlApplicationContext——从classpath路径加载:

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  

3、参照http://blog.csdn.net/dqatsh/article/details/3469278, 通过web.xml来获取Context。

4、在servlet中获取。

ServletContext servletContext = servlet.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

然后可以通过相应的bean去访问需要的properties(spring配置文件中${}方式设置到bean里)的值,这里不记录。

用PropertyPlaceholderConfigurer在加载上下文的时候暴露properties

<bean id="configBean"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>hello.properties</value>
</property>
</bean>

表明PropertyPlaceholderConfigurer是承担properties读取任务的类。

下面的类继承PropertyPlaceholderConfigurer,通过重写processProperties方法把properties暴露出去了。

import java.util.HashMap;
import java.util.Map;
import java.util.Properties; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; public class CustomizedPropertyConfigurer extends PropertyPlaceholderConfigurer { private static Map<String, Object> ctxPropertiesMap; @Override
protected void processProperties(ConfigurableListableBeanFactory beanFactory,
Properties props)throws BeansException { super.processProperties(beanFactory, props);
//load properties to ctxPropertiesMap
ctxPropertiesMap = new HashMap<String, Object>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
ctxPropertiesMap.put(keyStr, value);
}
} //static method for accessing context properties
public static Object getContextProperty(String name) {
return ctxPropertiesMap.get(name);
}
}

这样此类即完成了PropertyPlaceholderConfigurer的任务,同时又提供了上下文properties访问的功能。

于是在Spring配置文件中把PropertyPlaceholderConfigurer改成CustomizedPropertyConfigurer

<!-- use customized properties configurer to expose properties to program -->
<bean id="configBean"
class="com.payment.taobaoNavigator.util.CustomizedPropertyConfigurer">
<property name="location" value="classpath:dataSource.properties" />
</bean>

最后在程序中我们便可以使用CustomizedPropertyConfigurer.getContextProperty()来取得上下文中的properties的值了。

JAVA Eclipse使用Maven构建web项目详解(SSM框架)的更多相关文章

  1. Maven学习:Eclipse使用maven构建web项目(转)

    Maven学习:Eclipse使用maven构建web项目(转) 8.更改class路径:右键项目,Java Build Path -> Source 下面应该有4个文件夹.src/main/j ...

  2. 采用Eclipse中间Maven构建Web项目错误(一)

    采用Eclipse中间Maven构建Web工程 1.在进行上述操作时.pom.xml一直报错 <project xmlns="http://maven.apache.org/POM/4 ...

  3. eclipse通过maven构建web项目步骤说明

    1.  File -> New -> Other ,搜索maven,选择Maven Project,点击Next 2.这里不需要改继续Next 3.这里需要注意,需要选择maven-arc ...

  4. Eclipse使用Maven构建web项目

    1.创建Maven项目: 点击“File”菜单,或者通过工具栏的“New”创建Project,如下图所示: 选择Maven->Maven Project,弹出向导对话框,如下图所示: 选中Cre ...

  5. 在eclipse中maven构建Web项目,tomcat插件在maven中的运用

    1.选中maven,构建maven  web 2.项目构建好之后发现一个错误如图,是因为没有servlet的API,我们需要在pom.xml 中对servlet的API进行依赖 pom.xml如下即可 ...

  6. eclipse 用maven创建web项目

    Eclipse 用maven构建web项目 (2013-01-27 11:05:31) 转载▼ 标签: it eclipse maven spring web 杂谈   一.背景介绍 对于初学者,用m ...

  7. 利用Eclipse中的Maven构建Web项目(三)

    利用Eclipse中的Maven构建Web项目 1.将Maven Project转换成动态Web项目,鼠标右键项目,输入"Project Facets" 2.依据Dynamic W ...

  8. 利用Eclipse中的Maven构建Web项目(二)

    利用Eclipse中的Maven构建Web项目 1.新建源文件夹,Java Resources鼠标右键,"New-->Source Folder" 2.新建src/main/ ...

  9. 利用Eclipse中的Maven构建Web项目报错(二)

    利用Eclipse中的Maven构建Web项目 1.错误描述 [INFO] Scanning for projects... [INFO] [INFO] Using the builder org.a ...

随机推荐

  1. SQL*Plus工具使用 sqlplus / as sysdba登录

    A: 正常情况下 [oracle@hukou admin]$ sqlplus / as sysdba Copyright (c) 1982, 2013, Oracle.  All rights res ...

  2. 最新版Charles破解方法(Mac+Windows).md

    Charles 破解 去网站 http://charles.iiilab.com/ 下载相对应的版本 下载破解文件 charles.jar http://charles.iiilab.com/ 替换掉 ...

  3. Eclipse代码块折叠插件,安装使用

    在代码编写中经常会遇到一些很长的set(xxx)的代码,非常影响体验. 而Eclipse的folding插件可以自定义的将代码块进行折叠. 效果如下图所示: 可以根据代码块的功能来进行折叠,从而保证代 ...

  4. [LeetCode] Find Largest Value in Each Tree Row 找树每行最大的结点值

    You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...

  5. 规约模式(Specification Pattern)

    一.引言 最近在看一个项目的源码时(DDD),对里面的一些设计思想和设计思路有了一些疑问.当看到(Repository层)中使用了 spec.SatisfiedBy() 时,感觉有点懵.于是在项目中搜 ...

  6. Codeforces Round #452 E. New Year and Old Subsequence

    Description A string t is called nice if a string "2017" occurs in t as a subsequence but ...

  7. ●2301 [HAOI2011] Problem b

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2301 题解: 莫比乌斯反演,入门题. 类似●HDU 1695 GCD 只是多了一个下界,(另 ...

  8. Hdu2680 最短路

    给定一个有向图,多个起点,一个终点,求起点到终点的最短路. 1.可以加一个点,使其与那些起点的距离为0 2.将图反着来建,然后在所有点找出最小的 方案一: #include <iostream& ...

  9. 2015 多校联赛 ——HDU5302(构造)

    Connect the Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  10. 【HNOI2004】L语言

    题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是由若干小写字母构成.一个单词W也是由若干小写字母构成.一个字典D是若干个单词的 ...