整合SSH框架及常见问题解决
来源:互联网 发布:mysql安装后连接不上 编辑:程序博客网 时间:2024/06/05 05:56
如何快速的搭建javaweb应用框架是每一个java程序员必做的事情,通过自己的学习也是初步的可以搭建好ssh框架。下面是详细搭建过程及一些途中遇到的问题和解决方案,博主使用的是myeclipse,方便框架导入
- 新建工程
- 导入spring框架
- 导入Struts框架
- 导入hibernate框架
- 整合配置文件
- spring配置常用bean
- 常见问题
spring 框架导入
1首先新建一个javaweb工程,之后右击项目选中MyEclipse ->project facets->install spring facets
版本选择3.1,之后一路next,之后再导入包的时候选中spring presistence,点击finish,导入完成,如下图:
之后会发现工程src下多了applicationContext.xml 和spring library 。xml文件是spring配置文件。
导入Struts
右击项目选中MyEclipse ->project facets->install Struts2.x facets,引入Struts2 版本选择2.1 ,之后一路next,导包的时候注意要选择导入spring-plugin包来支持spring框架,最后选择url时推荐使用.do,这表示Struts 会过滤请求后缀是.do的请求并进行相应处理,之后点击finish 。如下图:
导入 spring
右击项目选中MyEclipse ->project facets->install spring facets来引入spring 框架 ,版本推荐3.1,之后一路next 。导包的时候注意导入spring presistence包来获得持久化支持(比如c3p0连接池等)同时也要点击获得注解支持,之后finish,如下图:
这里ssh框架基本导入,但还需要进行配置才能使用。
整合配置
主要是配置applicationContext.xml文件,其中主要分为以下几个部分:
1.Struts action class依赖bean配置
2.项目中需要依赖注入的bean配置
3.hibernatesessionfactory配置
以下是一个基础的spring配置文件代码
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- struts action 依赖配置--> <bean name="useraction" class="com.woniuxy.logis.action.UserAction"> <property name="dto" ref="userdto"></property> <property name="us" ref="userservice"></property> </bean> <bean name="userdto" class="com.wo.logis.entity.impl.UserDTO"></bean> <bean name="userservice" class="com.wo.logis.service.impl.Userserviceimpl"> <property name="ud" ref="UserinfoDAO"></property> <property name="ui" ref="Userinfo"></property> </bean> <bean name="Userinfo" class="com.wo.logis.dao.po.Userinfo"></bean> <!-- hibernate sessionfactory 配置--> <bean name="UserinfoDAO" class="com.wo.logis.dao.UserinfoDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!-- 声明式事务 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" rollback-for="Exception" read-only="false" /> <tx:method name="del*" rollback-for="Exception" read-only="false" /> <tx:method name="update*" rollback-for="Exception" read-only="false" /> <tx:method name="save*" rollback-for="Exception" read-only="false" /> <tx:method name="*" read-only="false" /> </tx:attributes> </tx:advice> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.wo.logis.service.impl.*.*(..))" /> </aop:config> <!-- hibernate 配置文件注入--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> </beans>
常见问题
1.read-only 错误
在提交事务的时候可能回出现不能读写的错误,可能是因为设置某些方法只能读取,那么在配置文件里修改只读属性 read-only=true;
<tx:method name="*" read-only="false" />
2.不能insert into 数据库
可能原因:Hibernate 实体类属性自增长属性为increment 。保证数据库同名字段为自增长。且数据库表名不能是user等关键字。
3.在进行数据库添加时(调用dao的save方法)不能进行保存。
Hibernate 会在设置字段时自动进行数据库更新。折中办法是由service层的方法进行调用dao的save方法。或者是没有启动事务,确保声明式事务被加载(spring配置即可)
this.save();//ud.save(ui); hibernate dao save方法private void save() { // TODO Auto-generated method stub ud.save(ui);}//service层的实现类
4.在使用依赖注入的类方法里,注入的字段为空 或不能使用
确保已配置了类相应bean的property子节点,并确保映射的bean已配置。
<property name="ui" ref="Userinfo"></property>
5.hibernate.cfg.xml 实体类配置是否正确
<mapping resource="com/wo/logis/dao/po/Userinfo.hbm.xml" />
6.确保Struts 配置文件 action class 和spring 对应bean id或name属性一样
- 整合SSH框架及常见问题解决
- SSH框架常见问题及解决
- SSH框架常见问题及解决
- SSH框架常见问题及解决
- ssh整合常见问题及解决方案
- 框架SSH常见问题及解决方案
- ssh整合常见问题及解决方案1
- DWZ框架常见问题及解决
- J2EE进阶(十)SSH框架整合常见问题汇总(一)
- J2EE进阶(十一)SSH框架整合常见问题汇总(二)
- J2EE进阶(十二)SSH框架整合常见问题汇总(三)
- ssh整合常见问题总结
- SSH整合框架的错误信息及解决办法
- 整合SSH框架的思路及方法
- SSH框架常见问题
- SSH框架整合笔记
- ssh框架整合
- SSH框架整合
- Qt 使用treeview
- Spring学习
- RBAC权限管理
- MyBatis中sql片段的定义与引用
- android加密解密算法
- 整合SSH框架及常见问题解决
- JSON的使用
- Ubuntu安装git
- java处理高并发高负载类网站的优化方法
- jstack和线程dump分析
- 新AlphaGo首度揭秘:单机运行,4个TPU,算法更强
- java.lang.AbstractMethodError二级缓存redis小问题1
- 阶乘之和
- CSS基础