您的位置:首页 > 其它

SSH小框架笔记

2017-01-06 20:23 260 查看
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

       --配置事务属性:   <t
4000
x: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);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ssh