关闭

Struts2+Spring+hibernate 小demo

标签: web
325人阅读 评论(1) 收藏 举报
分类:

搭个架子,说实话,没啥大用,拷贝点jar包,改改配置文件,建立几个类,就完事了,能学到啥呢?

设计模式,算法,多线程,经验,层次要高点,闲话不多说了,开配:


1 准备jar包,还挺多的,下一篇直接贴源码。


2  三个重要配置文件 web.xml applicationContext.xml struts.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <!-- 配置spring的监听器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext*.xml</param-value>
    </context-param>
    <!-- 服务器启动时,监听Spring的配置环境,加载spring配置文件 -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    
    
    <!-- 配置OpenSessionInViewFilter,必须在struts2监听之前 -->
    <filter>
        <filter-name>lazyLoadingFilter</filter-name>
        <filter-class>
            org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
        </filter-class>
    </filter>


    <!-- 设置监听加载上下文 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>
    <filter-mapping>
    <filter-name>lazyLoadingFilter</filter-name>
    <url-pattern>*.action</url-pattern>
    </filter-mapping>
    
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>


</web-app>


<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- 配置Hibernate支持 -->


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/myfktest">
</property>
<property name="username" value="root"></property>
<property name="password" value=""></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.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/fk/model/Person.hbm.xml</value>
</list>
</property>
</bean>


<bean id="personDao" class="com.fk.dao.impl.PersonDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>


<bean id="service" class="com.fk.sevice.impl.ServiceImpl">
<property name="personDao">
<ref bean="personDao" />
</property>
</bean>




<bean id="listPerson" class="com.fk.action.PersonAction" scope="prototype">
<property name="service">
<ref bean="service" />
</property>
</bean>


<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>


<!-- 配置事务特性,配置add,delete,update开始的方法,事务传播特性为required -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>


<!-- 配置那些类的方法进行事务管理,当前com.sy.crm.service包中的子包, 类中所有方法需要,还需要参考tx:advice的设置 -->
<aop:config>
<aop:pointcut id="allManagerMethod"
expression="execution(*
    com.fk.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" />
</aop:config>


</beans>


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- struts2委托spring管理 -->
<constant name="struts.objectFactory" value="spring" />
<package name="fk" extends="struts-default">
<action name="listPerson" class="listPerson">
<result name="success" type="dispatcher">/list.jsp</result>
</action>
</package>
</struts>


3 jsp页面


<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>list employee page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<style type="text/css">
table {
border: 1px solid black;
border-collapse: collapse;
}


table thead tr th {
border: 1px solid black;
padding: 3px;
background-color: #cccccc;
}


table tbody tr td {
border: 1px solid black;
padding: 3px;
}
</style>
</head>
<body>
<center>
<h3>雇员管理:</h3>
<br>
<h4>
<a href="../emp/add.jsp">员工注册</a>
</h4>
<s:form action="listPerson" theme="simple">
<table>
<thead>
<tr>
<th>选择</th>
<th>编号</th>
<th>姓名</th>
<th>电话</th>
<th>地址</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<s:iterator value="personList">
<tr>
<td><input type="checkbox" name="id"
value='<s:property value="id" />' /></td>
<td><s:property value="id" /></td>
<td><s:property value="name" /></td>
<td><s:property value="phone" /></td>
<td><s:property value="address" /></td>
<td><a
href='<s:url action="edit"><s:param name="id" value="id" /></s:url>'>
修改 </a> &nbsp; <a
href='<s:url action="delete"><s:param name="id" value="id" /></s:url>'>
删除 </a></td>
</tr>
</s:iterator>
</tbody>
</table>
<s:submit value="delete" />
</s:form>
</center>
</body>


</html>


哥要实现的是,http://127.0.0.1:8080/testssh/listPerson,访问这个action后,跳转到指定list.jsp页面,并刷出数据来。


流程讲下:

action要继承actionSupport,要重写execute方法,当请求action是listperson时,会执行execute方法,该方法中调用service-dao,然后返回list

所以actioin中要定义一个list成员变量,提供get set方法·,这样执行完execute后,会把list结果返回,不用存在resquest中了,只需要返回一个标志位即可

action代码:

package com.fk.action;


import java.util.ArrayList;
import java.util.List;


import com.fk.model.Person;
import com.fk.sevice.Service;
import com.opensymphony.xwork2.ActionSupport;


public class PersonAction extends ActionSupport {


private static final long serialVersionUID = -6165045820084536332L;


private List<Person> personList = new ArrayList<Person>();


private Service service;


public Service getService() {
return service;
}


public void setService(Service service) {
this.service = service;
}


public List<Person> getPersonList() {
return personList;
}


public void setPersonList(List<Person> personList) {
this.personList = personList;
}


@Override
public String execute() throws Exception {
personList = service.getPerSonList();
return SUCCESS;
}


}


其实这块属于潜规则,别的很eazy,说白了就是action-service-dao,dao用使用templdate这个家伙,访问数据库,crud就行了。接着贴。


package com.fk.dao.impl;


import java.util.List;


import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


import com.fk.dao.PersonDao;
import com.fk.model.Person;


public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {


@SuppressWarnings("unchecked")
public List<Person> getPersonList() {
return (List<Person>) getHibernateTemplate().find("from Person");
}


}


数据库提前要建立好数据库,跟表,注意我们用的是hibernate,要用映射文件。贴:

<?xml version="1.0"?> 
<!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.fk.model.Person" table="t_person">
<id name="id" column="id" type="int">
<generator class="increment" />
</id>
<property name="name" column="name" type="string" />
<property name="phone" column="phone" type="string" />
<property name="address" column="address" type="string" />
</class>
</hibernate-mapping>


明白了不,很简单的,不过要动手搞一下,光明白不行。








0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4194次
    • 积分:195
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论