SSH开发环境详细搭建(配置文件)
网上都说SSH是当今国内最流行Web开发框架,上大学那会我也学过用过,参加工作后,头两公司都还挺时毛,都不用它。一来到现在的公司,我靠——还在用SSH,尤其是Struts1,还有Hibernate,以前的公司都用Struts2,Ibatis,我就郁闷了,多久没搞这些玩意儿了,得了趁这个机会把它们都捡起来吧,那就从搭建开发环境开始:
开发环境
win7+Jdk1.6+Myeclipse6.5+Tomcat6+Oracle10
软件环境
Struts1.3+Spring2.5+Hibernate3+Jsp
一、新建Web工程
目录结构为:
二、添加Struts支持,在项目名SSH上点右键->MyEclipse->添加Struts支持:
在弹出框选项中选择Struts版本:
三、添加Spring支持,同上添加Struts一样,在弹出框中选择Spring的jar包:
Spring版本为2.5,jar包选择如下,包括Hibernate的也选中:
四、添加数据库支持,打开Myeclipse数据库视图,如下图:
五、添加Hibernate支持:
至此SSH的jar包都添加完毕,接下来配置配置文件:
六、配置文件修改
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> </beans>
hibernate.cfg.xml也是自动生成的,其实可以不要这个文件,将它的内容归并到applicationContext.xml中,但分开更清晰明了:
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="connection.username">yuwl</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <property name="myeclipse.connection.profile">MyOracle</property> <property name="connection.password">yuwlpwd</property> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> </session-factory> </hibernate-configuration>
Web.xml启动Spring和Struts,
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- 启动Spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
其实Spring的启动也可以放入Struts中启动:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans > <form-bean name="loginForm" type="org.ssh.struts.form.LoginForm" /> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings > <action attribute="loginForm" input="/form/login.jsp" name="loginForm" path="/login" scope="request" type="org.springframework.web.struts.DelegatingActionProxy"> <forward name="success" path="/form/success.jsp" /> <forward name="failure" path="/form/failure.jsp" /> </action> </action-mappings> <message-resources parameter="org.ssh.struts.ApplicationResources" /> <!-- 启动Spring --> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml"/> </plug-in> </struts-config>
Spring的两种启动方式看个人喜好,一般都是放在web.xml中的
至此SSH开发环境差不多了,发布到tomcat,启动看下是否报错,如果按上述步骤来的,应该没有问题。
需要说明的是单纯的启动服务不走数据库查询的话,程序是不会检查数据源连接的对错的。
七、补充Struts与Spring的配置访问请求两种方式:
第一种:Struts-config.xml的action类指向Spring代理类
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans > <form-bean name="loginForm" type="org.ssh.struts.form.LoginForm" /> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings > <action attribute="loginForm" input="/form/login.jsp" name="loginForm" path="/login" scope="request" type="org.springframework.web.struts.DelegatingActionProxy"> <forward name="success" path="/form/success.jsp" /> <forward name="failure" path="/form/failure.jsp" /> </action> </action-mappings> <message-resources parameter="org.ssh.struts.ApplicationResources" /> </struts-config>
对应Spring配置文件的action请求,这里name与上面的path相对应:
<!-- 定义Action层,并将Biz层注入进来 --> <bean name="/login" class="org.ssh.struts.action.LoginAction"> <property name="userBiz"> <ref bean="userBiz"/> </property> </bean>
第二种方式:Struts-config.xml: