eclipse(struts2+spring3+hibernate3)
案例下载:SSH
整合包下载:struts2+spring3+hibernate3
1、右键新建web project
2、添加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>
3、添加相关配置
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&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>
log4j.properties
#\u8bbe\u7f6e\u7ea7\u522b\u548c\u591a\u4e2a\u76ee\u7684\u5730
log4j.rootLogger=INFO,appender1,appender2
#\u8f93\u51fa\u5230\u63a7\u5236\u53f0
log4j.appender.appender1=org.apache.log4j.ConsoleAppender
#\u8bbe\u7f6e\u8f93\u51fa\u6837\u5f0f
log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout
#\u8f93\u51fa\u5230\u6587\u4ef6(\u8fd9\u91cc\u9ed8\u8ba4\u4e3a\u8ffd\u52a0\u65b9\u5f0f)
log4j.appender.appender2=org.apache.log4j.FileAppender
#\u8bbe\u7f6e\u6587\u4ef6\u8f93\u51fa\u8def\u5f84
#\u30101\u3011\u6587\u672c\u6587\u4ef6
log4j.appender.appender2.File=d:/SSH.log
#\u30102\u3011HTML\u6587\u4ef6
#log4j.appender.appender2.File=c:/Log4JDemo02.html
#\u8bbe\u7f6e\u6587\u4ef6\u8f93\u51fa\u6837\u5f0f
log4j.appender.appender2.layout=org.apache.log4j.TTCCLayout
#log4j.appender.appender2.layout=org.apache.log4j.HTMLLayout
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>
4、添加目录结构
5、添加ssh2所需要的jar包到以下路径
6、添加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>