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. hdu2062 Subset sequence----递推

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2062 题目大意: 给出n和m,集合{1,2,,,,n}的非空子集,按照一定方式排列,例如n==3时, ...

  2. JavaScript的屏幕对象

    screen 屏幕对象 反映了当前用户的屏幕设置. width 返回屏幕的宽度(像素数). height 返回屏幕的高度. availWidth 返回屏幕的可用宽度(除去了一些不自动隐藏的类似任务栏的 ...

  3. The first week CorelDRAW 课总结:

    1.这节课学到了什么知识? 答:(1)认识了CorelDRAW X4的工作界面(由标题栏 菜单栏 工具栏 属性栏 工具箱 页面控制栏 状态栏 绘图区和调色板组成): (2)CorelDRAW X4的基 ...

  4. Flume报 Space for commit to queue couldn't be acquired. Sinks are likely not keeping up with sources, or the buffer size is too tight

    报这个错误 需要一个是flume堆内存不够.还有一个就是把channel的容器调大 在channel加配置 type - 组件类型名称必须是memory capacity 100 存储在 Channe ...

  5. dnslog搭建

    为什么想重写这个呢,想说后面扫描ssrf和命令执行的时候,能快速改成自己想要的api,更容易修改一些. 工具改自:https://github.com/bugScanTeam/DNSLog 需要两个域 ...

  6. Python super使用

    一 基础使用 在类的继承中,如果重定义某个方法,该方法会覆盖父类的同名方法,但有时,我们希望能同时实现父类的功能,这时,我们就需要调用父类的方法了,可通过使用 super 来实现,比如: #!/usr ...

  7. [Codeforces 176B]Word Cut

    Description 题库链接 给你两个字符串 \(S\) 和 \(T\) ,准许你 \(k\) 次操作,每次将字符串左右分成两个非空的部分,再交换位置,问你有多少种不同的操作方法将 \(S\) 串 ...

  8. [Sdoi2009]Elaxia的路线

    Description 最近,Elaxia和w**的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们 必须合理地安排两个人在一起的时间.Elaxia和w**每天都要奔波于宿舍和实验室之间, ...

  9. 【BZOJ1059】【ZJOI2007】矩阵游戏

    Description 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏――矩阵游戏.矩阵游戏在一个N*N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两 ...

  10. ●BZOJ 2694 Lcm

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2694 题解: 莫比乌斯反演 不难看出,造成贡献的(i,j)满足gcd(i,j)无平方因子. ...