你好,游客 登录 注册 搜索
背景:
阅读新闻

MyEclipse10 开发 SSH2(Struts2.1+Spring+Hibernate)

[日期:2012-07-10] 来源:Linux社区  作者:zhdjbabygo [字体:大 中 小]

首先看<s:iterator>标签:它的作用是遍历由 value 指定的“集合型”对象(一般为 Map或 List 类型);在我们的例子中, 我们要遍历的是“list”对象(该对象由 action 传递过来,对应于 action 内的一个类属性)。其次要注意<s:property>标签,它的作用是将 iterator 中的对象的属性取出来,即<s:property value="name"/>的作用 相当于调用 list 的 getFirstname()方法(action 中的 getter 在这里得到了“隐形的”调用)。 从上面的代码中可以看出,除了使用<s:property>之外,我们还尝试了另外两种标签方法,一是<s:text>,它可以生成一个国际化的信息文本;另一个是${department},用的是 OGNL 表达式语言。网上有争论说,既然是 struts2,就尽量用 struts2 的标签;也有人说:OGNL 是主流,应该和<s:>标签配合使用;请自行深入学习后选择。

16.Spring与struts结合

(1)复制文件。复制struts2-spring-plugin-x-x-x.jar到WEB-INF/lib目录下。其中的x对应了Spring的版本号。还需要复制commons-logging.jar文件到WEB-INF/lib目录下。

(2)配置struts.objectFactory属性值。在struts.properties中设置struts.objectFactory属性值:

struts.objectFactory = spring

或者在XML文件中进行常量配置:
  1. <struts>  
  2.        <constant name="struts.objectFactory" value="spring" />  
  3. </struts>  
(3)修改Spring配置文件applicationcontext.xml:
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  7.   
  8.   
  9.     <bean id="dataSource"  
  10.         class="org.apache.commons.dbcp.BasicDataSource">  
  11.         <property name="driverClassName"  
  12.             value="com.microsoft.sqlserver.jdbc.SQLServerDriver">  
  13.         </property>  
  14.         <property name="url"  
  15.             value="jdbc:sqlserver://localhost:1433;DatabaseName=student">  
  16.         </property>  
  17.         <property name="username" value="sa"></property>  
  18.         <property name="password" value="123456"></property>  
  19.     </bean>  
  20.     <bean id="sessionFactory"  
  21.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  22.         <property name="dataSource">  
  23.             <ref bean="dataSource" />  
  24.         </property>  
  25.         <property name="hibernateProperties">  
  26.             <props>  
  27.                 <prop key="hibernate.dialect">  
  28.                     org.hibernate.dialect.SQLServerDialect  
  29.                 </prop>  
  30.             </props>  
  31.         </property>  
  32.         <property name="mappingResources">  
  33.             <list>  
  34.                 <value>com/neuq/common/Student.hbm.xml</value></list>  
  35.         </property></bean>  
  36.     <bean id="studentDAO" class="com.neuq.common.StudentDAO">  
  37.         <property name="sessionFactory">  
  38.             <ref bean="sessionFactory" />  
  39.         </property>  
  40.     </bean>  
  41.     <bean id="studentList" class="com.neuq.list.StudentList">  
  42.         <property name="studentDAO">  
  43.             <ref bean="studentDAO" />  
  44.         </property>  
  45.     </bean>  
  46.     </beans>  

注意将原来的bean id="StudentDAO"改成了bean id="studentDAO",并且要将对应的StudentDAO.java中最后的getFromApplicationContext函数改为

  1. public static StudentDAO getFromApplicationContext(ApplicationContext ctx) {  
  2.         return (StudentDAO) ctx.getBean("studentDAO");  
  3.     }  
再次部署运行,成功!

关于Spring如何与struts2结合,参见 http://www.linuxidc.com/Linux/2012-07/64860.htm 

至此,关于简单的ssh开发就介绍到这里,笔者小白刚接触此架构不久,愿意与大家共同讨论,第一次写博文,希望错误之处大家指出啦~~害羞

linux
相关资讯       myeclipse  SSH2 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款