整合SSH思路:
1. 确定顺序:spring->structs->hibernate,或者structs->spring->hibernate,原因我认为,spring的出现影响到hibernate生成的文件,比如sessionFactory,所以一般,包括网上其他很多的教程,都是采用把hibernate放在spring后面。
2. 版本:structs 2.1+spring 3.0 + hibernate 3.3
具体步骤:
1. 创建一个web project:SSH
2. 添加structs
选择structs的jar包
2. 添加spring
选择版本和要添加的jar包
生成applicationContext.xml文件
3.添加hibernate
选择版本,并且添加相关jar包
使用前面的spring文件
使用之前配好的数据源
注意:不生成SessionFactory文件
hibernate反向工程生成DAO和DTO
4.配置web.xml
添加spring监听器,添加以下代码
<!--spring配置 start --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 指定spring的配置文件路径方式有2种 一种配置到classpath路径,把applicationContext.xml放在src中,classpath:applicationContext.xml 一种配置到web-info路径,把applicationContext.xml放在WEB-INF中,/WEB-INF/applicationContext.xml --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> <!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> --> </context-param> <!--spring配置 end-->
5. 测试,CRUD简单操作
配置struct.xml,设置action,添加以下代码:
<package name="SSH" extends="struts-default"> <action name="List" class="List" method="list"> <result>List.jsp</result> </action> <action name="Edit" class="List" method="load"> <result>Edit.jsp</result> </action> <action name="Store" class="List" method="store"> <result type="redirect">List.action</result> </action> <action name="Remove" class="List" method="remove"> <result type="redirect">List.action</result> </action> </package>
配置applicationContext.xml
<!-- action的处理类 --> <bean id="List" class="action.BookAction" scope="prototype"> <property name="dao" ref="BookDAO"></property> </bean>
List. jsp页面
<s:form action="Remove" theme="simple"> <table cellspacing="0"> <thead> <tr> <th>Select</th> <th>ISBN</th> <th>Title</th> <th>Price</th> <th>Operation</th> </tr> </thead> <tbody> <s:iterator value="books"> <tr> <td><input type="checkbox" name="isbns" value='<s:property value="isbn" />' /></td> <td><s:property value="isbn" /></td> <td><s:property value="title" /></td> <td>$<s:property value="price" /></td> <td> <a href='<s:url action="Edit"><s:param name="isbn" value="isbn" /></s:url>'> Edit </a> <a href='<s:url action="Remove"><s:param name="isbn" value="isbn" /></s:url>'> Delete </a> </td> </tr> </s:iterator> </tbody> </table> <s:submit value="Remove" /><a href="Edit.jsp">Add Book</a> </s:form>