ztf 2019-06-29
web.xml-->Struts2 过滤器配置
<!-- Struts2 过滤器的配置--> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
struts2.xml -->src
>struts-2.3.36-all.zip\struts-2.3.36\apps\struts2-blank-2.3.36.war\WEB-INF\classes\struts2.xml
web.xml -->Spring 核心监听器
> ContextLoaderListener--> ContextLoader --> 105:contextConfigLocation
<!-- Spring 核心监听器 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
- 包结构: - com.ssh.action - com.ssh.dao- - com.ssh.domain - 实体 product - com.ssh.service
<s:form action="" method="post" namespace="/" theme="simple"> <table border="1" width="400"> <tr> <td>商品名称:</td> <td><s:textfield name="pname"/></td> </tr> <tr> <td>商品价格:</td> <td><s:textfield name="price"/></td> </tr> <tr> <td colspan="2"><input type="submit" value="添加" /></td> </tr> </table> </s:form>
com.ssh.action
ProductAction //商品管理的Actionpublic class ProductAction extends ActionSupport implements ModelDriven<Product>{ //^extends ActionSupport implements ModelDriven<Product> (模型驱动的接口) //模型驱动使用的类 private Product product = new Product(); @Override public Product getModel() { // TODO Auto-generated method stub return product; } /*注入Service struts2-spring-plugin-2.3.36.jar > struts-plugin.xml > struts.objectFactory >struts.objectFactory.spring.autoWire = name 自动装配*/ /*Struts 和Spring 整合过程中按名字自动注入业务层的类*/ private ProductService productService; public void setProductService(ProductService productService) { this.productService = productService; } }
com.ssh.dao
ProductDao //商品管理的DAOcom.ssh.domain
实体 productcom.ssh.service
ProductService //商品管理的业务层//业务层 注入Dao的类 private ProductDao productDao; public void setProductDao(ProductDao productDao) { this.productDao = productDao; }
aplicationContext.xml 配置业务层的类 *
<!-- 配置业务层的类 --> <bean id="productService" class="com.epoint.ssh.service.ProductService"> <property name="productDao" ref="productDao"/> </bean> <!-- 配置DAO的类 --> <bean id="productDao" class="com.epoint.ssh.dao.ProductDao"> </bean>
Struts2和Spring整合的两种方式
(1) Action 类由Struts2 自身创建
struts2.xml————><s:form action="product_save" method="post" namespace="/" theme="simple">
(2)actio 类交给Spring创建
<!--配置action类 --> <bean id="productAction" class="com.epoint.ssh.action.ProductAction" scope="prototype"> <!--手动注入service --> <property name="productService" ref="productService"></property> </bean> <package name="ssh" extends="struts-default" namespace="/"> <action name="product_*" class="productAction" method="{1}" > </action> </package>
create database myssh
/ssh/src/com/epoint/ssh/domain/Product.hbm.xml
--> /org/hibernate/hibernate-mapping-3.0.dtd
<hibernate-mapping> <class name="com.epoint.ssh.domain.Product" table="product"> <id name="pid" column="pid"> <generator class="native"/><!-- native表示自增 --> </id> <property name="pname" column="pname" length="20"/> <property name="price" column="price"/> </class> </hibernate-mapping>
applicationContext.xml
<!-- 引入外部配置文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/myssh?useSSL=false"/> <property name="user" value="root"/> <property name="password" value="Gepoint"/> </bean> <!-- 配置Hibernate --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!--注入连接池 --> <property name="dataSource" ref="dataSource"></property> <!--配置Hibernate属性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><!--方言 --> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!--加载Hibernate配置文件 --> <property name="mappingResources"> <list> <value>com/epoint/ssh/domain/Product.hbm.xml</value> </list> </property> </bean>
注入SessionFactory:
<bean id="productDao" class="com.epoint.ssh.dao.ProductDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
在Dao中调用模板完成保存操作
this.getHibernateTemplate().save(product)
<!-- 配置事务管理 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 开启注解事务 --> <tx:annotation-driven transaction-manager="transactionManager"/>
错误1: Causedby:java.lang.ClassNotFoundException:com.mchange.v2.c3p0.ComboPooledDataSource
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><bean>
中的class不能直接复制粘贴,要通过“提示”敲出来,如果直接复制或者不按提示敲出来可能还会继续报同样的错误错误2:Loading class 'com.mysql.jdbc.Driver'. This is deprecated.
错误3:MYSQL:WARN: Establishing SSL connection without server's identity verification is not recommended.
错误4: java.sql.SQLException: Access denied for user 'root '@'localhost' (using password: YES)