首先看<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文件中进行常量配置:- <struts>
- <constant name="struts.objectFactory" value="spring" />
- </struts>
- <?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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName"
- value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
- </property>
- <property name="url"
- value="jdbc:sqlserver://localhost:1433;DatabaseName=student">
- </property>
- <property name="username" value="sa"></property>
- <property name="password" value="123456"></property>
- </bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref bean="dataSource" />
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.SQLServerDialect
- </prop>
- </props>
- </property>
- <property name="mappingResources">
- <list>
- <value>com/neuq/common/Student.hbm.xml</value></list>
- </property></bean>
- <bean id="studentDAO" class="com.neuq.common.StudentDAO">
- <property name="sessionFactory">
- <ref bean="sessionFactory" />
- </property>
- </bean>
- <bean id="studentList" class="com.neuq.list.StudentList">
- <property name="studentDAO">
- <ref bean="studentDAO" />
- </property>
- </bean>
- </beans>
注意将原来的bean id="StudentDAO"改成了bean id="studentDAO",并且要将对应的StudentDAO.java中最后的getFromApplicationContext函数改为
- public static StudentDAO getFromApplicationContext(ApplicationContext ctx) {
- return (StudentDAO) ctx.getBean("studentDAO");
- }
关于Spring如何与struts2结合,参见 http://www.linuxidc.com/Linux/2012-07/64860.htm
至此,关于简单的ssh开发就介绍到这里,笔者小白刚接触此架构不久,愿意与大家共同讨论,第一次写博文,希望错误之处大家指出啦~~
