SSH小框架笔记

1.加入spring
   --加入jar包
   --配置web.xml
   --生成applicationContext.xml
2.加入Hibernate
  --加入jar包
  --配置hibernate.cfg,xml(配置基本的数据库信息  方言 格式化sql  显示sql 数据更新方式 hbm--update)
  --编写db.properties (写入数据库基本信息 user。password。。)
  --创建实体化类 并生成hbm.xml文件
  --配置applicationContext.xml
       --导入db.properties文件
       --配置dataSource     ${jdbc.user}
       --配置sessionfactory    LocalSessionFactoryBean( hibernate4)     
           {    <property name="dataSource" ref="dataSource"></property>
               <property name="configLocation" value="classpath:hibernate.cfg.xml" ></property>
               <property name=" mappingLocations" value="classpath:entities/*.hbm.xml"></property>

       --配置spring声明式事务  HibernateTransactionManager  管理sessionFactory
       --配置事务属性:   <tx:advice id="txAdvice" transaction-manager="transactionManager">
      <tx:attributes>
      <tx:method name="get*" read-only="true"/>
      <tx:method name="*"/>
      </tx:attributes>
</tx:advice>
       --配置事务切入点:<aop:config>
      <aop:pointcut expression=" execution( * service.*.*(..) )" id="txPointcut"/>          注意execution拼写
      <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>
3.加入Struts2
  --加入jar包
  --在web.xml文件中配置Struts2的filter
  --加入Struts2配置文件
    --整合
①加入Struts整合spring的jar包
②在spring的配置文件applicationContext-beans.xml中正常配置Action,注意Action的scope为prototype
③在Struts2的配置文件struts.xml中配置Action时,class属性指向该Action在IOC中的id

4.创建applicationContext-beans.xml 配置employeeDao 指向sessionFactory
                                                          配置employeeService  、 employeeAction

5.完成功能
①获取所有员工信息:若在dao中只查询employee的信息,而且Employee和Department还是使用的懒加载,页面上还需要显示员工部门信息,此时会出现懒加载异常,代理对象不能被初始化。
   解决办法:使用迫切左外链接  FROM Employee e LEFT OUTER JOIN FETCH e.department

②删除员工信息(使用Ajax)
    ①正常删除:返回值需要是redirect类型,而且重定向到emp-list
     --hql:String hql="DELETE FROM Employee WHERE id = ?";
       getSession().createQuery(hql).setInteger(0, id).executeUpdate();
     ---action中需要传入id值
             private Integer id ; setid。。。。
   ②确定要删除吗?确认
     使用JQuery时发生异常: Struts has detected an unhandled exception:
Messages: No result defined for action actions.EmployeeAction and result input
                           发现错误: <a href="emp-delete?id=${id} " class="delete">DELETE</a>  中少了红色的引号
   ③Ajax的使用参见file:///G:/BaiduYunDownload/opensourse/SSH 框架/struts-2.3.15.3/docs/WW/docs/home.html
          tips:return值改为delete 并在struts2配置文件中<result type="stream" name="delete">
                        <param name="contentType">text/html</param>
                         <param name="inputName">inputStream</param>
                                </result>
public String delete()  {
try {
employeeService.delete(id);
inputStream = new ByteArrayInputStream("1".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
try {
inputStream = new ByteArrayInputStream("0".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
}
return "delete";
}
③增加员工信息
    ①Action中用到了ModelDriven 、prepareble接口
    ②输入的字符串类型的birth必须要用类型转换器转换为Date型
             遇到问题:birth仍然为空!
                       发现Employee(util.Date)跟类型转换类(sql.Date)导入的Date包不一致      ---解决办法 统一为util.Date
    ③Ajax验证人员是否存在
            Action中写一个ValidatebyLastName的方法
                 --Dao中写通过姓名查询的方法
                 --Service中判断是否找到该员工 返回boolean值
                -- Action中的验证方法 判断Boolean值  并设置对应的data数据
            input.jsp写入相应的Ajax代码
            tips:写完之后发现添加员工 Ajax并没有起卵用??????
           -----<script type="text/javascript" src=" / scripts/jquery-1.7.2.min.js"></script>     多了个/ wtf????
                  但这并没有什么卵用 还是不能验证????
             $this.after("<font color='green'>Lastname可用! ></font>"); 再次粗心多了个>
                  然而用户名可用可以验证 ,不可用的情况还是不可以??????????
                 }else if(date=="0"){              ============data写成了date !!!!!!!!!!!!!!!
     终于好了O(∩_∩)O

④修改员工信息
    --带上Id  通过id查找对应的employee   Dao--Service--Action
    --表单回显               public String input(){
request.put("departments", departmentService.getAll());
return "input";
}
public void prepareInput(){
if(id!=null){
model=employeeService.get(id);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值