myeclipse(struts2+spring3+hibernate3)

myeclipse(struts2+spring3+hibernate3)


案例下载:SSH

整合包:struts2+spring3+hibernate3


1、右键新建web project
这里写图片描述


2、添加struts、spring、hibernate支持
(1)struts
这里写图片描述
这里写图片描述
这里写图片描述

(2)spring
这里写图片描述
这里写图片描述
这里写图片描述

(3)hibernate
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述


3、解决包冲突
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
全选,打开
这里写图片描述
点击OK!


4、搭建目录结构
这里写图片描述


5、hibernate反转工程
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述


6、添加dao与dao实现、service与service实现
这里写图片描述


7、配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <package name="login" namespace="/" extends="struts-default">
        <action name="userAction" class="com.gx.web.UserAction">
            <result name="success">/jsp/list.jsp</result>
            <result name="input">/jsp/index.jsp</result>
            <result name="list">/jsp/list.jsp</result>
            <result name="add">/jsp/add.jsp</result>
            <result name="update">/jsp/update.jsp</result>
        </action>
    </package>
</struts>


8、配置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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/aop   
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.1.xsd  
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd" default-autowire="byName">

    <!-- 开启注解 -->
    <context:annotation-config />
    <!-- spring 扫描路径,注意当前工程只需要扫描dao和service,srpingmvc或者struts2注解才有变化 -->
    <context:component-scan base-package="com.gx.dao,com.gx.service" />

    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName"
            value="com.mysql.jdbc.Driver">
        </property>
        <property name="url"
            value="jdbc:mysql://127.0.0.1:3306/gxsoft?useUnicode=true&amp;characterEncoding=utf-8">
        </property>
        <property name="username" value="root"></property>
        <property name="password" value="123"></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>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                 <value>com/gx/po/TbEmp.hbm.xml</value>
            </list>
        </property>
    </bean>

    <!-- 配置声明式事务管理(采用注解的方式) -->
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="txManager"/>
</beans>

9、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    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_3_0.xsd">
  <display-name></display-name> 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        classpath:applicationContext*.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

  <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>*.action</url-pattern>
  </filter-mapping></web-app>

10、jsp
index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting 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">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <body>
    <form action="<%=request.getContextPath() %>/userAction!login.action" method="post">
        <table>
            <tr>
                <td>用户名</td>
                <td><input type="text" name="user.ename"></td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type="password" name="user.password"></td>
            </tr>
            <tr>
                <td><input type="submit" value="立即登陆"></td>
            </tr>
        </table>
    </form>
  </body>
</html>

add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>  

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'add.jsp' starting 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">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <form action="<%=request.getContextPath() %>/userAction!add.action" method="post">
        <input type="hidden" name="user.password" value="123">
        <table>
            <tr>
                <td>姓名</td>
                <td><input type="text" name="user.ename"></td>
            </tr>
            <tr>
                <td>所属部门</td>
                <td><input type="text" name="user.dept"></td>
            </tr>
            <tr>
                <td>年龄</td>
                <td><input type="text" name="user.age"></td>
            </tr>
            <tr>
                <td>性别</td>
                <td><input type="text" name="user.gender"></td>
            </tr>
            <tr>
                <td>入职时间</td>
                <td><input type="text" name="user.workDate"></td>
            </tr>
            <tr>
                <td><input type="submit" value="添加"></td>
                <td><input type="button" value="返回" onclick="history.go(-1);"></td>
            </tr>
        </table>
    </form>
  </body>
</html>

list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<c:set value="${pageContext.request.contextPath}" scope="page" var="ctx"></c:set>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'list.jsp' starting 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">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    <style type="text/css">
        body{
        font-family: Microsoft Yahei, sans-serif;
        }
    </style>
  </head>

  <body>
    <div>
        <input type="button" value="查询" onclick="window.location.href='<%=request.getContextPath() %>/userAction!findAll.action'">
        <input type="button" value="添加" onclick="window.location.href='<%=request.getContextPath() %>/userAction!toadd.action'">
    </div>
    <table>
        <thead>
            <tr>
                <th>编号</th>
                <th>姓名</th>
                <th>所属部门</th>
                <th>年龄</th>
                <th>性别</th>
                <th>入职时间</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <c:forEach items="${tbEmps}" var="item" varStatus="status">
                <tr>
                    <td>${item.eid }</td>
                    <td>${item.ename }</td>
                    <td>${item.dept }</td>
                    <td>${item.age }</td>
                    <td>${item.gender }</td>
                    <td><fmt:formatDate value="${item.workDate }" type="both" pattern="yyyy-MM-dd"/></td>
                    <td>
                        <a href="<%=request.getContextPath() %>/userAction!toupdate.action?user.eid=${item.eid }">修改</a>/
                        <a href="<%=request.getContextPath() %>/userAction!delete.action?user.eid=${item.eid }">删除</a>
                    </td>
                </tr>
            </c:forEach>
        </tbody>
    </table>
  </body>
</html>

update.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>  

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'update.jsp' starting 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">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
     <form action="<%=request.getContextPath() %>/userAction!update.action" method="post">
        <input type="hidden" name="user.password" value="${user.password }">
        <input type="hidden" name="user.eid" value="${user.eid }">
        <table>
            <tr>
                <td>姓名</td>
                <td><input type="text" name="user.ename" value="${user.ename }"></td>
            </tr>
            <tr>
                <td>所属部门</td>
                <td><input type="text" name="user.dept" value="${user.dept }"></td>
            </tr>
            <tr>
                <td>年龄</td>
                <td><input type="text" name="user.age" value="${user.age }"></td>
            </tr>
            <tr>
                <td>性别</td>
                <td><input type="text" name="user.gender" value="${user.gender }"></td>
            </tr>
            <tr>
                <td>入职时间</td>
                <td><input type="text" name="user.workDate" value="<fmt:formatDate value="${user.workDate }" type="both" pattern="yyyy-MM-dd" />"></td>
            </tr>
            <tr>
                <td><input type="submit" value="修改"></td>
                <td><input type="button" value="重置"></td>
            </tr>
        </table>
    </form>
  </body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值