`
stinge
  • 浏览: 149843 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

SSH环境搭建

 
阅读更多

一、配置Struts2 
(1)添加Struts2架包: 
①commons-logging-1.0.4.jar 
②commons-fileupload-1.2.1.jar 
③freemarker-2.3.13.jar 
④ognl-2.6.11.jar 
⑤struts2-core-2.1.6.jar 
⑥xwork-2.1.2.jar 
(2)创建struts.xml文件(注意:一定要在src根目录下) 
代码如下: 

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
"http://struts.apache.org/dtds/struts-2.1.dtd"> 

<struts> 
<package name="struts2" extends="struts-default"> 

</package> 
</struts> 

 
(3)配置web.xml文件,添加Struts2的核心控制器 
代码如下:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
<welcome-file-list> 
<welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 
<filter> 
<filter-name>struts2</filter-name> 
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 
</filter> 
<filter-mapping> 
<filter-name>struts2</filter-name> 
<url-pattern>/*</url-pattern> 
</filter-mapping> 
</web-app> 

 
(注:org.apache.struts2.dispatcher.FilterDispatcher路径在struts-core-2.1.6.jar包下)

 

 

二、导入Spring

   添加Spring包,MyEclipse下右击项目文件夹,点击Add Spring capitilities,选择copy到项目文件夹的lib目录下,

设置配置文件路径,WebRoot/WEB-INF/下,applicationContext.xml

 

设置web.xml文件内容

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
  <param-name>contentConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContent.xml</param-value>
  </context-param>
</web-app>

 

     applicationContext.xml内容

 

 

 <?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


</beans>
 

 

三、配置Hibernate 
在此之前首先要配置一下数据库连接池,如果已有的话一下步骤就不需要了 
创建数据库连接池步骤如下: 
(我用的是MySql) 
(1)打开DB Browser, 创建数据源,配置如下: 
Driver template:选择“MySql Connector/J”
Driver name:创建的数据源名称(自定义) 
Connection URL:连接数据库用到的驱动URL(或者说是数据库连接字符串) 
User name:数据库登录用户名 
Password:数据库登陆密码 
Driver JARS:导入连接JDBC驱动包(数据库驱动类) 
mysql-connector-java-5.1.7-bin.jar
Driver classname:JDBC驱动程序(当选择Driver template时,被自动选中) 

如果什么都没有的的话,打开DB Browser,应该只有一个默认的连接就是MyEclipse自带的数据库,“MyEclipse Derby” 

右击,选择“New” 
打开新建数据源驱动连接“Database Driver” 

SQLServer2000如下配置:这里Driver Name的名字取得的he Driver Classname相同,便于理解,点击“Finish”完成。 

(2)创建完成之后,打开“Open...”,测试连接。 
数据库连接池配置完毕,开始添加hibernate支持,如上: 
选择菜单“MyEclipse”选项,指向“Project Capabilities”; 
选择“Add Hibernate Capabilities”; 

选择Hibernate版本为Hibernate3.2; 
JAR Libraay Installation项选“Copy...”; 
点击“Next”, 

点击“Next”,在“Hibernate Configueration”中, 
选中“Spring configuration file(applicationContext.xml)”;使用spring中的applicationContext.xml文件来管理hibernateBean 

点击“Next”,选中“Exiting Spring Contfiguration file”, 
在“SessionFactory ID”选项中输入“sessionFactory”; 


点击“Next”,配置数据源,在“Bean Id”中输入dataSource  
选中use JDBC Driver 
DB Driver中选择数据库的一个连接,此处即是我们上面建立的Driver Name名字,选择它即可。(此数据库的连接可以打开myeclipse中的DB Browser视图进行创建); 

点“Next”,取消选中的Create SessionFactory class选项 ,点击完成。 

此时会在applicationContext.xml文件中生成对应的驱动及数据源信息,如下: 

 

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/wmltest">
</property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean>
 
4、此时会在“applicationContext.xml”文件中报错,class="org.apache.commons.dbcp.BasicDataSource">这个类找不到, 

可选中项目,右键选择属性, 

选中“java build path”,选择”Libraries”,点击 “add library”按钮,选中“ MyEclipse Libraries”,点击“下一步” ; 


选择“Spring 2.0 Persistence JDBC Libraries”,此时系统会自动的选中其它的选项, 
点击”Finish”,点击”OK”; 

此时错误就会消失。 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics