SSH框架编写页面保存实例

来源:互联网 发布:大数据存储公司 编辑:程序博客网 时间:2024/05/19 20:21

1.引入所需的jar包,网上很多。注意的是除了spring,struts和hibernate所需的jar包,还需引入数据库连接的jar包。


2.总体介绍(将此页面的信息保存到数据库中)注:本例使用的是Mysql数据库。



3.搭建hibernate层

   (1)创建po对象

         

 public class SysUserGroup implements java.io.Serializable {private Integer id;private String remark; // 备注private String name; // 部门名称private String principal; // 部门负责人private String incumbent; // 部门职能     }

   (2)创建数据表和po之间的映射文件SysUserGroup.hbm.xml 

         

<hibernate-mapping>   <class name="com.cql.crm.domain.SysUserGroup" table="sys_user_group">      <id name="id" type="integer"><column name="id" /><generator class="identity" /></id><property name="name" type="string"><column name="name" sql-type="varchar(100)" /></property><property name="principal" type="string"><column name="principal" sql-type="varchar(50)" /></property><property name="incumbent" type="string"><column name="incumbent" sql-type="varchar(200)" /></property><property name="remark" type="text"><column name="remark" /></property>   </class></hibernate-mapping>

    (3)创建hibernate.cfg.xml文件连接数据库,加载SysUserGroup.hbm.xml文件,放置全局包目录下

               

<hibernate-configuration><session-factory>    <property name="hibernate.connection.username">root</property>    <property name="hibernate.connection.password">root</property>    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/cql1</property>    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>    <property name="hibernate.connection.autocommit">true</property><property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property><property name="hibernate.hbm2ddl.auto">update</property><property name="hibernate.show_sql">true</property><mapping resource="com/cql/crm/domain/SysUserGroup.hbm.xml"/></session-factory></hibernate-configuration>

4.搭建spring和hibernate

     (1)创建并配置beans.xml文件,放置在src下
     (2)创建dao层共同的接口

public interface ICommonDao<T> {public void save(T entity); }
    (3)创建dao层共同的接口的实现类

   public class CommonDaoImpl<T> extends HibernateDaoSupport implements ICommonDao<T> {public void save(T entity) {this.getHibernateTemplate().save(entity);}@Resource(name="sessionFactory") //注入sessionFactorypublic  void setSessionFactoryDI(SessionFactory sessionFactory) {System.out.println("sessionFactory  "+sessionFactory);    //调用父类的setSessionFactory方法,注入sessionFactorysuper.setSessionFactory(sessionFactory);}   }   
    (4) 创建部门的dao接口 SysUserGroupDao接口

            public interface ISysUserGroupDao extends ICommonDao<SysUserGroup> {public final static String  SERVICE_NAME="com.cql.crm.dao.impl.SysUserGroupDaoImpl";}
    (5)创建部门的dao接口的实现类

           @Repository(ISysUserGroupDao.SERVICE_NAME)public class SysUserGroupDaoImpl extends CommonDaoImpl<SysUserGroup> implements ISysUserGroupDao {}
   (6)创建部门的业务层接口
<pre name="code" class="java">    public interface ISysUserGroupService {public final static String  SERVICE_NAME="cn.itcast.crm.service.impl.SysUserGroupServiceImpl";public void saveSysUserGroup(SysUserGroup sysUserGroup);}


    (7)创建部门的业务层接口实现类

    @Transactional(readOnly=true)@Service(ISysUserGroupService.SERVICE_NAME)public class SysUserGroupServiceImpl implements ISysUserGroupService {@Resource(name=ISysUserGroupDao.SERVICE_NAME)private ISysUserGroupDao sysUserGroupDao; @Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)public void saveSysUserGroup(SysUserGroup sysUserGroup) {sysUserGroupDao.save(sysUserGroup);}}
5.搭建struts2(保存部门信息)

   (1) 根据jsp页面的请求,构建SysUserGroupAction类,在类中添加save方法

public class SysUserGroupAction extends ActionSupportpublic String save() throws IllegalAccessException, InvocationTargetException{System.out.println("xxxxxxxxxxxxxxxxxxxxxxxx ");return null;}}
    (2)建立请求路径和action之间的关联,创建struts.xml文件,放置在全局包目录下
          
<struts>   <!-- 配置请求后缀名.do -->   <constant name="struts.action.extension" value="do"/>      <!-- 配置主题为简单主题 -->   <constant name="struts.ui.theme" value="simple"/>      <!--配置struts2的模式为开发模式-->   <constant name="struts.devMode" value="true"/>      <package name="sys" namespace="/sys" extends="struts-default">      <action name="sysUserGroupAction_*" class="com.cql.crm.web.action.SysUserGroupAction" method="{1}">         <result name="add">/sys/group/add.jsp</result>            </action>   </package></struts> 
    (3) 在web.xml文件配置struts2的过滤器

<filter><filter-name>StrutsPrepareAndExecuteFilter</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>StrutsPrepareAndExecuteFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
    (4)定义javaBean(vo对象)接收表单数据
      public class SysUserGroupForm implements java.io.Serializable {private String id;private String name;private String principal;private String incumbent;private String remark;  }
    (5)action要实现模型驱动接口

       public class SysUserGroupAction extends ActionSupport implements ModelDriven<SysUserGroupForm>{   private SysUserGroupForm sysUserGroupForm=new SysUserGroupForm();public String save() throws IllegalAccessException, InvocationTargetException{System.out.println("xxxxxxxxxxxxxxxxxxxxxxxx ");return null;}public SysUserGroupForm getModel() {return sysUserGroupForm;}}
    (6)在struts2的SysUserGroupAction中的save方法中,获取spring容器中bean节点的对象

          创建ServiceProvinderCore,加载beans.xml文件

       public class ServiceProvinderCore {protected ApplicationContext ctx;/** * @param filename  beans.xml */public void load(String filename){ctx=new ClassPathXmlApplicationContext(filename);}}
      创建ServiceProvinder类,获取获取spring容器中bean节点的对象

      public class ServiceProvinder {private static ServiceProvinderCore sc;static{sc=new ServiceProvinderCore();sc.load("beans.xml");}public static Object getService(String beanName){System.err.println("ppppppppppppppppppppppp");if(StringUtils.isBlank(beanName)){throw new RuntimeException("您要访问的服务名称不能为空");}Object bean=null;//如果spring容器中包含beanNameif(sc.ctx.containsBean(beanName)){bean=sc.ctx.getBean(beanName);}//如果spring容器中不包含beanNameif(bean==null){throw new RuntimeException("您要访问的服务名称["+beanName+"]不存在");}return bean;}}
    (7)在 SysUserGroupAction中的save方法中增加如下代码

  public String save() throws IllegalAccessException, InvocationTargetException{System.out.println("sysUserGroupForm.getName()  "+sysUserGroupForm.getName());//实例化po对象SysUserGroup sysUserGroup=new SysUserGroup();//赋值vo对象的值到po中BeanUtils.copyProperties(sysUserGroup, sysUserGroupForm);//获取业务层的对象(本项目struts2和spring是分离的)ISysUserGroupService sysUserGroupService=   (ISysUserGroupService)ServiceProvinder.getService(ISysUserGroupService.SERVICE_NAME);//调用业务层保存po对象sysUserGroupService.saveSysUserGroup(sysUserGroup);return null;}



至此,我们可以将页面数据保存到数据库中  








0 0
原创粉丝点击