SSH整合案列

问题

使用Spring整合Hibernate及Struts2,实现资费查询功能。

 

方案

SSH整合步骤

 

步骤

实现此案例需要按照如下步骤进行。

步骤一:导包

创建WEB项目NETCTOSS-SSH,并导入数据库驱动包、Struts2开发包、Hibernate开发包以及Spring开发包,完成后项目中包结构如下图

 

注意,Hibernate开发包中的freemarker.jar,与Struts2中的包重复,且版本较低,去掉。Spring开发包中的commons-logging.jar,与Hibernate中的包重复,且版本较低,去掉。

步骤二:使用Spring整合Hibernate

在src根路径下引入并配置applicationContext.xml,并在该文件中配置数据源、SessionFactory、开启组件扫描以及声明式事务,代码如下

<?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:tx="http://www.springframework.org/schema/tx" 
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:jee="http://www.springframework.org/schema/jee"
        xsi:schemaLocation="
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
    <!-- 配置数据源 -->
    <bean id="ds" 
        class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <!-- 配置连接参数 -->
        <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe"/>
        <property name="driverClass" value="oracle.jdbc.OracleDriver"/>
        <property name="user" value="lhh"/>
        <property name="password" value="123456"/>
        <!-- 配置连接池 -->
        <property name="initialPoolSize" value="3"/>
        <property name="maxPoolSize" value="10"/>
        <property name="minPoolSize" value="1"/>
        <property name="acquireIncrement" value="3"/>
        <property name="maxIdleTime" value="60"/>
    </bean>
    
    <!-- 配置SessionFactory -->
    <bean id="sessionFactory" 
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!-- 依赖数据源 -->
        <property name="dataSource" ref="ds"/>
        <!-- Hibernate框架相关配置 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.OracleDialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.formate_sql">true</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>com/tarena/entity/Cost.hbm.xml</value>
            </list>
        </property>
    </bean>
    
    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.tarena"/>
    <!-- 声明式事务管理,采用AOP形式切入 -->
    <bean id="txManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="load*" read-only="true" />
            <tx:method name="execute" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    <aop:config proxy-target-class="true">
        <aop:advisor advice-ref="txAdvice" 
            pointcut="within(com.tarena.action.*)" />
    </aop:config>
</beans>

在com.tarena.entity包下创建实体类Cost,代码如下

package com.tarena.entity;
import java.sql.Date;
/**
 * 资费实体类
 */
public class Cost {
    private Integer id;// 主键
    private String name;// 资费名称
    private Integer baseDuration;// 在线时长
    private Double baseCost;// 基本费用
    private Double unitCost;// 单位费用
    private String status;// 状态
    private String descr;// 资费说明
    private Date createTime;// 创建日期
    private Date startTime;// 启用日期
    private String costType;// 资费类型
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getBaseDuration() {
        return baseDuration;
    }
    public void setBaseDuration(Integer baseDuration) {
        this.baseDuration = baseDuration;
    }
    public Double getBaseCost() {
        return baseCost;
    }
    public void setBaseCost(Double baseCost) {
        this.baseCost = baseCost;
    }
    public Double getUnitCost() {
        return unitCost;
    }
    public void setUnitCost(Double unitCost) {
        this.unitCost = unitCost;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getDescr() {
        return descr;
    }
    public void setDescr(String descr) {
        this.descr = descr;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public Date getStartTime() {
        return startTime;
    }
    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    public String getCostType() {
        return costType;
    }
    public void setCostType(String costType) {
        this.costType = costType;
    }
}

在com.tarena.entity包下创建映射关系文件Cost.hbm.xml,代码如下

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.tarena.entity.Cost" table="cost">
        <id name="id" type="integer" column="id">
            <!-- 用来指明主键的生成方式 -->
            <generator class="sequence">
                <param name="sequence">cost_seq</param>
            </generator>
        </id>
        
        <property name="name" 
            type="string" column="name" />
        <property name="baseDuration" 
            type="integer" column="base_duration" />
        <property name="baseCost" 
            type="double" column="base_cost" />
        <property name="unitCost" 
            type="double" column="unit_cost" />
        <property name="status" 
            type="string" column="status" />
        <property name="descr" 
            type="string" column="descr" />
        <property name="createTime" 
            type="date" column="creatime" />
        <property name="startTime" 
            type="date" column="startime" />
        <property name="costType" 
            type="string" column="cost_type" />
    </class>
</hibernate-mapping>

在com.tarena.dao包下创建DAO接口ICostDao,并在接口中声明查询全部资费数据的方法,代码如下

package com.tarena.dao;
import java.util.List;
import com.tarena.entity.Cost;
public interface ICostDao {
    List<Cost> findAll();
}

在com.tarena.dao包下创建DAO实现类CostDaoImpl,继承于HibernateDaoSupport,并实现接口ICostDao,代码如下

package com.tarena.dao;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.tarena.entity.Cost;
@Repository
public class CostDaoImpl 
    extends HibernateDaoSupport implements ICostDao {
    @Resource
    public void setSF(SessionFactory sf) {
        super.setSessionFactory(sf);
    }
    @Override
    public List<Cost> findAll() {
        String hql = "from Cost";
        return getHibernateTemplate().find(hql);
    }
}

步骤三:使用Spring整合Struts2

配置web.xml,配置一个listener,使tomcat启动时自动加载Spring。另外再配置Struts2的前端控制器,代码如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置listener,使tomcat启动时自动加载Spring -->
  <listener>
    <listener-class>
          org.springframework.web.context.ContextLoaderListener
      </listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <!-- 配置前端控制器 -->
  <filter>
    <filter-name>Struts2</filter-name>
    <filter-class>
          org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
      </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>Struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

在com.tarena.action包下创建Action类FindCostAction,调用DAO实现对资费的查询,代码如下

package com.tarena.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import com.tarena.dao.ICostDao;
import com.tarena.entity.Cost;
@Controller
@Scope("prototype")
public class FindCostAction {
    @Resource
    private ICostDao costDao;
    private List<Cost> costs;
    public String load() {
        costs = costDao.findAll();
        return "success";
    }
    public List<Cost> getCosts() {
        return costs;
    }
    public void setCosts(List<Cost> costs) {
        this.costs = costs;
    }
}

在src根路径下引入struts.xml并配置Action,代码如下

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
    
    <package name="cost" 
        namespace="/cost" extends="json-default">
        <action name="findCost" 
            class="findCostAction" method="load">
            <result name="success">
                /WEB-INF/cost/find_cost.jsp
            </result>
        </action>
    </package>
</struts>

在WEB-INF/cost下创建页面find_cost.jsp,实现资费数据的显示,代码如下

<%@page pageEncoding="utf-8" isELIgnored="false"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>达内-NetCTOSS</title>
        <link type="text/css" rel="stylesheet" media="all" href="../styles/global.css" />
        <link type="text/css" rel="stylesheet" media="all" href="../styles/global_color.css" />
        <script language="javascript" type="text/javascript">
            //排序按钮的点击事件
            function sort(btnObj) {
                if (btnObj.className == "sort_desc")
                    btnObj.className = "sort_asc";
                else
                    btnObj.className = "sort_desc";
            }
            //启用
            function startFee() {
                var r = window.confirm("确定要启用此资费吗?资费启用后将不能修改和删除。");
                document.getElementById("operate_result_info").style.display = "block";
            }
            //删除
            function deleteFee() {
                var r = window.confirm("确定要删除此资费吗?");
                document.getElementById("operate_result_info").style.display = "block";            }
        </script>        
    </head>
    <body>
        <!--Logo区域开始-->
        <div id="header">
            <img src="../images/logo.png" alt="logo" class="left"/>
            <a href="#">[退出]</a>            
        </div>
        <!--Logo区域结束-->
        <!--导航区域开始-->
        <div id="navi">                        
            <ul id="menu">
                <li><a href="../index.html" class="index_off"></a></li>
                <li><a href="../role/role_list.html" class="role_off"></a></li>
                <li><a href="../admin/admin_list.html" class="admin_off"></a></li>
                <li><a href="../fee/fee_list.html" class="fee_on"></a></li>
                <li><a href="../account/account_list.html" class="account_off"></a></li>
                <li><a href="../service/service_list.html" class="service_off"></a></li>
                <li><a href="../bill/bill_list.html" class="bill_off"></a></li>
                <li><a href="../report/report_list.html" class="report_off"></a></li>
                <li><a href="../user/user_info.html" class="information_off"></a></li>
                <li><a href="../user/user_modi_pwd.html" class="password_off"></a></li>
            </ul>            
        </div>
        <!--导航区域结束-->
        <!--主要区域开始-->
        <div id="main">
            <form action="" method="">
                <!--排序-->
                <div class="search_add">
                    <div>
                        <!--<input type="button" value="月租" class="sort_asc" onclick="sort(this);" />-->
                        <input type="button" value="基费" class="sort_asc" onclick="sort(this);" />
                        <input type="button" value="时长" class="sort_asc" onclick="sort(this);" />
                    </div>
                    <input type="button" value="增加" class="btn_add" onclick="location.href='fee_add.html';" />
                </div> 
                <!--启用操作的操作提示-->
                <div id="operate_result_info" class="operate_success">
                    <img src="../images/close.png" onclick="this.parentNode.style.display='none';" />
                    删除成功!
                </div>    
                <!--数据区域:用表格展示数据-->     
                <div id="data">            
                    <table id="datalist">
                        <tr>
                            <th>资费ID</th>
                            <th class="width100">资费名称</th>
                            <th>基本时长</th>
                            <th>基本费用</th>
                            <th>单位费用</th>
                            <th>创建时间</th>
                            <th>开通时间</th>
                            <th class="width50">状态</th>
                            <th class="width200"></th>
                        </tr>       
                        
                        <!-- 使用Struts2标签遍历集合,使用OGNL表达式输出内容。 -->               
                        <s:iterator value="costs">                 
                            <tr>
                                <td><s:property value="id"/></td>
                                <td><a href="fee_detail.html"><s:property value="name"/></a></td>
                                <td><s:property value="baseDuration"/></td>
                                <td><s:property value="baseCost"/></td>
                                <td><s:property value="unitCost"/></td>
                                <td><s:property value="createTime"/></td>
                                <td><s:property value="startTime"/></td>
                                <td>
                                    <s:if test="status==0">开通</s:if>
                                    <s:else>暂停</s:else>
                                </td>
                                <td>                                
                                    <input type="button" value="启用" class="btn_start" onclick="startFee();" />
                                    <input type="button" value="修改" class="btn_modify" onclick="location.href='fee_modi.html';" />
                                    <input type="button" value="删除" class="btn_delete" onclick="deleteFee();" />
                                </td>
                            </tr>
                       </s:iterator>
                       
                    </table>
                    <p>业务说明:<br />
                    1、创建资费时,状态为暂停,记载创建时间;<br />
                    2、暂停状态下,可修改,可删除;<br />
                    3、开通后,记载开通时间,且开通后不能修改、不能再停用、也不能删除;<br />
                    4、业务账号修改资费时,在下月底统一触发,修改其关联的资费ID(此触发动作由程序处理)
                    </p>
                </div>
                <!--分页-->
                <div id="pages">
                    <a href="#">上一页</a>
                    <a href="#" class="current_page">1</a>
                    <a href="#">2</a>
                    <a href="#">3</a>
                    <a href="#">4</a>
                    <a href="#">5</a>
                    <a href="#">下一页</a>
                </div>
            </form>
        </div>
        <!--主要区域结束-->
        <div id="footer">
            <p>[源自北美的技术,最优秀的师资,最真实的企业环境,最适用的实战项目]</p>
            <p>版权所有(C)加拿大达内IT培训集团公司 </p>
        </div>
    </body>
</html>

步骤四:测试

部署项目并启动tomcat,访问资费列表,效果如下图

 

完整代码

本案例的完整代码如下所示:

applicationContext.xml完整代码如下

<?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:tx="http://www.springframework.org/schema/tx" 
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:jee="http://www.springframework.org/schema/jee"
        xsi:schemaLocation="
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
    <!-- 配置数据源 -->
    <bean id="ds" 
        class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <!-- 配置连接参数 -->
        <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe"/>
        <property name="driverClass" value="oracle.jdbc.OracleDriver"/>
        <property name="user" value="lhh"/>
        <property name="password" value="123456"/>
        <!-- 配置连接池 -->
        <property name="initialPoolSize" value="3"/>
        <property name="maxPoolSize" value="10"/>
        <property name="minPoolSize" value="1"/>
        <property name="acquireIncrement" value="3"/>
        <property name="maxIdleTime" value="60"/>
    </bean>
    
    <!-- 配置SessionFactory -->
    <bean id="sessionFactory" 
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!-- 依赖数据源 -->
        <property name="dataSource" ref="ds"/>
        <!-- Hibernate框架相关配置 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.OracleDialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.formate_sql">true</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>com/tarena/entity/Cost.hbm.xml</value>
            </list>
        </property>
    </bean>
    
    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.tarena"/>
    <!-- 声明式事务管理,采用AOP形式切入 -->
    <bean id="txManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="load*" read-only="true" />
            <tx:method name="execute" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    <aop:config proxy-target-class="true">
        <aop:advisor advice-ref="txAdvice" 
            pointcut="within(com.tarena.action.*)" />
    </aop:config>
</beans>

Cost完整代码如下

package com.tarena.entity;
import java.sql.Date;
/**
 * 资费实体类
 */
public class Cost {
    private Integer id;// 主键
    private String name;// 资费名称
    private Integer baseDuration;// 在线时长
    private Double baseCost;// 基本费用
    private Double unitCost;// 单位费用
    private String status;// 状态
    private String descr;// 资费说明
    private Date createTime;// 创建日期
    private Date startTime;// 启用日期
    private String costType;// 资费类型
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getBaseDuration() {
        return baseDuration;
    }
    public void setBaseDuration(Integer baseDuration) {
        this.baseDuration = baseDuration;
    }
    public Double getBaseCost() {
        return baseCost;
    }
    public void setBaseCost(Double baseCost) {
        this.baseCost = baseCost;
    }
    public Double getUnitCost() {
        return unitCost;
    }
    public void setUnitCost(Double unitCost) {
        this.unitCost = unitCost;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getDescr() {
        return descr;
    }
    public void setDescr(String descr) {
        this.descr = descr;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public Date getStartTime() {
        return startTime;
    }
    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    public String getCostType() {
        return costType;
    }
    public void setCostType(String costType) {
        this.costType = costType;
    }
}

Cost.hbm.xml完整代码如下

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.tarena.entity.Cost" table="cost">
        <id name="id" type="integer" column="id">
            <!-- 用来指明主键的生成方式 -->
            <generator class="sequence">
                <param name="sequence">cost_seq</param>
            </generator>
        </id>
        
        <property name="name" 
            type="string" column="name" />
        <property name="baseDuration" 
            type="integer" column="base_duration" />
        <property name="baseCost" 
            type="double" column="base_cost" />
        <property name="unitCost" 
            type="double" column="unit_cost" />
        <property name="status" 
            type="string" column="status" />
        <property name="descr" 
            type="string" column="descr" />
        <property name="createTime" 
            type="date" column="creatime" />
        <property name="startTime" 
            type="date" column="startime" />
        <property name="costType" 
            type="string" column="cost_type" />
    </class>
</hibernate-mapping>

ICostDao完整代码如下

package com.tarena.dao;
import java.util.List;
import com.tarena.entity.Cost;
public interface ICostDao {
    List<Cost> findAll();
}

CostDaoImpl完整代码如下

package com.tarena.dao;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.tarena.entity.Cost;
@Repository
public class CostDaoImpl 
    extends HibernateDaoSupport implements ICostDao {
    @Resource
    public void setSF(SessionFactory sf) {
        super.setSessionFactory(sf);
    }
    @Override
    public List<Cost> findAll() {
        String hql = "from Cost";
        return getHibernateTemplate().find(hql);
    }
}

web.xml完整代码如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置listener,使tomcat启动时自动加载Spring -->
  <listener>
    <listener-class>
          org.springframework.web.context.ContextLoaderListener
      </listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <!-- 配置前端控制器 -->
  <filter>
    <filter-name>Struts2</filter-name>
    <filter-class>
          org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
      </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>Struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

FindCostAction完整代码如下

package com.tarena.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import com.tarena.dao.ICostDao;
import com.tarena.entity.Cost;
@Controller
@Scope("prototype")
public class FindCostAction {
    @Resource
    private ICostDao costDao;
    private List<Cost> costs;
    public String load() {
        costs = costDao.findAll();
        return "success";
    }
    public List<Cost> getCosts() {
        return costs;
    }
    public void setCosts(List<Cost> costs) {
        this.costs = costs;
    }
}

struts.xml完整代码如下

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
    
    <package name="cost" 
        namespace="/cost" extends="json-default">
        <action name="findCost" 
            class="findCostAction" method="load">
            <result name="success">
                /WEB-INF/cost/find_cost.jsp
            </result>
        </action>
    </package>
</struts>

find_cost.jsp完整代码如下

<%@page pageEncoding="utf-8" isELIgnored="false"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>达内-NetCTOSS</title>
        <link type="text/css" rel="stylesheet" media="all" href="../styles/global.css" />
        <link type="text/css" rel="stylesheet" media="all" href="../styles/global_color.css" />
        <script language="javascript" type="text/javascript">
            //排序按钮的点击事件
            function sort(btnObj) {
                if (btnObj.className == "sort_desc")
                    btnObj.className = "sort_asc";
                else
                    btnObj.className = "sort_desc";
            }
            //启用
            function startFee() {
                var r = window.confirm("确定要启用此资费吗?资费启用后将不能修改和删除。");
                document.getElementById("operate_result_info").style.display = "block";
            }
            //删除
            function deleteFee() {
                var r = window.confirm("确定要删除此资费吗?");
                document.getElementById("operate_result_info").style.display = "block";            }
        </script>        
    </head>
    <body>
        <!--Logo区域开始-->
        <div id="header">
            <img src="../images/logo.png" alt="logo" class="left"/>
            <a href="#">[退出]</a>            
        </div>
        <!--Logo区域结束-->
        <!--导航区域开始-->
        <div id="navi">                        
            <ul id="menu">
                <li><a href="../index.html" class="index_off"></a></li>
                <li><a href="../role/role_list.html" class="role_off"></a></li>
                <li><a href="../admin/admin_list.html" class="admin_off"></a></li>
                <li><a href="../fee/fee_list.html" class="fee_on"></a></li>
                <li><a href="../account/account_list.html" class="account_off"></a></li>
                <li><a href="../service/service_list.html" class="service_off"></a></li>
                <li><a href="../bill/bill_list.html" class="bill_off"></a></li>
                <li><a href="../report/report_list.html" class="report_off"></a></li>
                <li><a href="../user/user_info.html" class="information_off"></a></li>
                <li><a href="../user/user_modi_pwd.html" class="password_off"></a></li>
            </ul>            
        </div>
        <!--导航区域结束-->
        <!--主要区域开始-->
        <div id="main">
            <form action="" method="">
                <!--排序-->
                <div class="search_add">
                    <div>
                        <!--<input type="button" value="月租" class="sort_asc" onclick="sort(this);" />-->
                        <input type="button" value="基费" class="sort_asc" onclick="sort(this);" />
                        <input type="button" value="时长" class="sort_asc" onclick="sort(this);" />
                    </div>
                    <input type="button" value="增加" class="btn_add" onclick="location.href='fee_add.html';" />
                </div> 
                <!--启用操作的操作提示-->
                <div id="operate_result_info" class="operate_success">
                    <img src="../images/close.png" onclick="this.parentNode.style.display='none';" />
                    删除成功!
                </div>    
                <!--数据区域:用表格展示数据-->     
                <div id="data">            
                    <table id="datalist">
                        <tr>
                            <th>资费ID</th>
                            <th class="width100">资费名称</th>
                            <th>基本时长</th>
                            <th>基本费用</th>
                            <th>单位费用</th>
                            <th>创建时间</th>
                            <th>开通时间</th>
                            <th class="width50">状态</th>
                            <th class="width200"></th>
                        </tr>       
                        
                        <!-- 使用Struts2标签遍历集合,使用OGNL表达式输出内容。 -->               
                        <s:iterator value="costs">                 
                            <tr>
                                <td><s:property value="id"/></td>
                                <td><a href="fee_detail.html"><s:property value="name"/></a></td>
                                <td><s:property value="baseDuration"/></td>
                                <td><s:property value="baseCost"/></td>
                                <td><s:property value="unitCost"/></td>
                                <td><s:property value="createTime"/></td>
                                <td><s:property value="startTime"/></td>
                                <td>
                                    <s:if test="status==0">开通</s:if>
                                    <s:else>暂停</s:else>
                                </td>
                                <td>                                
                                    <input type="button" value="启用" class="btn_start" onclick="startFee();" />
                                    <input type="button" value="修改" class="btn_modify" onclick="location.href='fee_modi.html';" />
                                    <input type="button" value="删除" class="btn_delete" onclick="deleteFee();" />
                                </td>
                            </tr>
                       </s:iterator>
                       
                    </table>
                    <p>业务说明:<br />
                    1、创建资费时,状态为暂停,记载创建时间;<br />
                    2、暂停状态下,可修改,可删除;<br />
                    3、开通后,记载开通时间,且开通后不能修改、不能再停用、也不能删除;<br />
                    4、业务账号修改资费时,在下月底统一触发,修改其关联的资费ID(此触发动作由程序处理)
                    </p>
                </div>
                <!--分页-->
                <div id="pages">
                    <a href="#">上一页</a>
                    <a href="#" class="current_page">1</a>
                    <a href="#">2</a>
                    <a href="#">3</a>
                    <a href="#">4</a>
                    <a href="#">5</a>
                    <a href="#">下一页</a>
                </div>
            </form>
        </div>
        <!--主要区域结束-->
        <div id="footer">
            <p>[源自北美的技术,最优秀的师资,最真实的企业环境,最适用的实战项目]</p>
            <p>版权所有(C)加拿大达内IT培训集团公司 </p>
        </div>
    </body>
</html>
posted @ 2018-01-12 13:53  journeyIT  阅读(34)  评论(0编辑  收藏  举报