eclipse(struts2+spring3+hibernate3)
案例下载:SSH
整合包下载:struts2+spring3+hibernate3
1、右键新建web project
2、添加web.xml
<web-app version&#61;"3.0" xmlns&#61;"http://java.sun.com/xml/ns/javaee" xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation&#61;"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.jspwelcome-file>welcome-file-list><context-param><param-name>contextConfigLocationparam-name><param-value>classpath:applicationContext*.xmlparam-value>context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>listener><filter><filter-name>struts2filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>filter><filter-mapping><filter-name>struts2filter-name><url-pattern>*.actionurl-pattern>filter-mapping>web-app>
3、添加相关配置
applicationContext.xml
<beans
xmlns&#61;"http://www.springframework.org/schema/beans"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xmlns:p&#61;"http://www.springframework.org/schema/p"xmlns:context&#61;"http://www.springframework.org/schema/context"xmlns:aop&#61;"http://www.springframework.org/schema/aop"xmlns:tx&#61;"http://www.springframework.org/schema/tx" xsi:schemaLocation&#61;"http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://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&#61;"byName"><context:annotation-config /><context:component-scan base-package&#61;"com.gx.dao,com.gx.service" /><bean id&#61;"dataSource"class&#61;"org.apache.commons.dbcp.BasicDataSource"><property name&#61;"driverClassName"value&#61;"com.mysql.jdbc.Driver">property><property name&#61;"url"value&#61;"jdbc:mysql://127.0.0.1:3306/gxsoft?useUnicode&#61;true&characterEncoding&#61;utf-8">property><property name&#61;"username" value&#61;"root">property><property name&#61;"password" value&#61;"123">property>bean><bean id&#61;"sessionFactory"class&#61;"org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name&#61;"dataSource"><ref bean&#61;"dataSource" />property><property name&#61;"hibernateProperties"><props><prop key&#61;"hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>props>property><property name&#61;"mappingResources"><list><value>com/gx/po/TbEmp.hbm.xmlvalue>list>property>bean><bean id&#61;"txManager" class&#61;"org.springframework.orm.hibernate3.HibernateTransactionManager"><property name&#61;"sessionFactory" ref&#61;"sessionFactory">property>bean><tx:annotation-driven transaction-manager&#61;"txManager"/>
beans>
log4j.properties
#\u8bbe\u7f6e\u7ea7\u522b\u548c\u591a\u4e2a\u76ee\u7684\u5730
log4j.rootLogger&#61;INFO,appender1,appender2#\u8f93\u51fa\u5230\u63a7\u5236\u53f0
log4j.appender.appender1&#61;org.apache.log4j.ConsoleAppender
#\u8bbe\u7f6e\u8f93\u51fa\u6837\u5f0f
log4j.appender.appender1.layout&#61;org.apache.log4j.TTCCLayout#\u8f93\u51fa\u5230\u6587\u4ef6(\u8fd9\u91cc\u9ed8\u8ba4\u4e3a\u8ffd\u52a0\u65b9\u5f0f)
log4j.appender.appender2&#61;org.apache.log4j.FileAppender
#\u8bbe\u7f6e\u6587\u4ef6\u8f93\u51fa\u8def\u5f84
#\u30101\u3011\u6587\u672c\u6587\u4ef6
log4j.appender.appender2.File&#61;d:/SSH.log
#\u30102\u3011HTML\u6587\u4ef6
#log4j.appender.appender2.File&#61;c:/Log4JDemo02.html
#\u8bbe\u7f6e\u6587\u4ef6\u8f93\u51fa\u6837\u5f0f
log4j.appender.appender2.layout&#61;org.apache.log4j.TTCCLayout
#log4j.appender.appender2.layout&#61;org.apache.log4j.HTMLLayout
struts.xml
<struts><package name&#61;"login" namespace&#61;"/" extends&#61;"struts-default"><action name&#61;"userAction" class&#61;"com.gx.web.UserAction"><result name&#61;"success">/jsp/list.jspresult><result name&#61;"input">/jsp/index.jspresult><result name&#61;"list">/jsp/list.jspresult><result name&#61;"add">/jsp/add.jspresult><result name&#61;"update">/jsp/update.jspresult>action>package>
struts>
4、添加目录结构
5、添加ssh2所需要的jar包到以下路径
6、添加jsp
index.jsp
<%&#64; page language&#61;"java" import&#61;"java.util.*" pageEncoding&#61;"UTF-8"%>
<%
String path &#61; request.getContextPath();
String basePath &#61; request.getScheme()&#43;"://"&#43;request.getServerName()&#43;":"&#43;request.getServerPort()&#43;path&#43;"/";
%>
<html><head><base href&#61;"<%&#61;basePath%>"><title>My JSP &#39;index.jsp&#39; starting pagetitle><meta http-equiv&#61;"pragma" content&#61;"no-cache"><meta http-equiv&#61;"cache-control" content&#61;"no-cache"><meta http-equiv&#61;"expires" content&#61;"0"> <meta http-equiv&#61;"keywords" content&#61;"keyword1,keyword2,keyword3"><meta http-equiv&#61;"description" content&#61;"This is my page">head><body><form action&#61;"<%&#61;request.getContextPath() %>/userAction!login.action" method&#61;"post"><table><tr><td>用户名td><td><input type&#61;"text" name&#61;"user.ename">td>tr><tr><td>密码td><td><input type&#61;"password" name&#61;"user.password">td>tr><tr><td><input type&#61;"submit" value&#61;"立即登陆">td>tr>table>form>body>
html>
add.jsp
<%&#64; page language&#61;"java" import&#61;"java.util.*" pageEncoding&#61;"UTF-8"%>
<%
String path &#61; request.getContextPath();
String basePath &#61; request.getScheme()&#43;"://"&#43;request.getServerName()&#43;":"&#43;request.getServerPort()&#43;path&#43;"/";
%>
<%&#64; taglib uri&#61;"http://java.sun.com/jsp/jstl/core" prefix&#61;"c" %>
<%&#64; taglib uri&#61;"http://java.sun.com/jsp/jstl/fmt" prefix&#61;"fmt" %>
<html><head><base href&#61;"<%&#61;basePath%>"><title>My JSP &#39;add.jsp&#39; starting pagetitle><meta http-equiv&#61;"pragma" content&#61;"no-cache"><meta http-equiv&#61;"cache-control" content&#61;"no-cache"><meta http-equiv&#61;"expires" content&#61;"0"> <meta http-equiv&#61;"keywords" content&#61;"keyword1,keyword2,keyword3"><meta http-equiv&#61;"description" content&#61;"This is my page">head><body><form action&#61;"<%&#61;request.getContextPath() %>/userAction!add.action" method&#61;"post"><input type&#61;"hidden" name&#61;"user.password" value&#61;"123"><table><tr><td>姓名td><td><input type&#61;"text" name&#61;"user.ename">td>tr><tr><td>所属部门td><td><input type&#61;"text" name&#61;"user.dept">td>tr><tr><td>年龄td><td><input type&#61;"text" name&#61;"user.age">td>tr><tr><td>性别td><td><input type&#61;"text" name&#61;"user.gender">td>tr><tr><td>入职时间td><td><input type&#61;"text" name&#61;"user.workDate">td>tr><tr><td><input type&#61;"submit" value&#61;"添加">td><td><input type&#61;"button" value&#61;"返回" onclick&#61;"history.go(-1);">td>tr>table>form>body>
html>
list.jsp
<%&#64; page language&#61;"java" import&#61;"java.util.*" pageEncoding&#61;"UTF-8"%>
<%
String path &#61; request.getContextPath();
String basePath &#61; request.getScheme()&#43;"://"&#43;request.getServerName()&#43;":"&#43;request.getServerPort()&#43;path&#43;"/";
%>
<%&#64; taglib uri&#61;"http://java.sun.com/jsp/jstl/core" prefix&#61;"c" %>
<%&#64; taglib uri&#61;"http://java.sun.com/jsp/jstl/fmt" prefix&#61;"fmt" %>
<%&#64; taglib uri&#61;"http://java.sun.com/jsp/jstl/functions" prefix&#61;"fn" %>
<c:set value&#61;"${pageContext.request.contextPath}" scope&#61;"page" var&#61;"ctx">c:set>
<html><head><base href&#61;"<%&#61;basePath%>"><title>My JSP &#39;list.jsp&#39; starting pagetitle><meta http-equiv&#61;"pragma" content&#61;"no-cache"><meta http-equiv&#61;"cache-control" content&#61;"no-cache"><meta http-equiv&#61;"expires" content&#61;"0"> <meta http-equiv&#61;"keywords" content&#61;"keyword1,keyword2,keyword3"><meta http-equiv&#61;"description" content&#61;"This is my page"><style type&#61;"text/css">body{font-family: Microsoft Yahei, sans-serif;}style>head><body><div><input type&#61;"button" value&#61;"查询" onclick&#61;"window.location.href&#61;&#39;<%&#61;request.getContextPath() %>/userAction!findAll.action&#39;"><input type&#61;"button" value&#61;"添加" onclick&#61;"window.location.href&#61;&#39;<%&#61;request.getContextPath() %>/userAction!toadd.action&#39;">div><table><thead><tr><th>编号th><th>姓名th><th>所属部门th><th>年龄th><th>性别th><th>入职时间th><th>操作th>tr>thead><tbody><c:forEach items&#61;"${tbEmps}" var&#61;"item" varStatus&#61;"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&#61;"${item.workDate }" type&#61;"both" pattern&#61;"yyyy-MM-dd"/>td><td><a href&#61;"<%&#61;request.getContextPath() %>/userAction!toupdate.action?user.eid&#61;${item.eid }">修改a>/<a href&#61;"<%&#61;request.getContextPath() %>/userAction!delete.action?user.eid&#61;${item.eid }">删除a>td>tr>c:forEach>tbody>table>body>
html>
update.jsp
<%&#64; page language&#61;"java" import&#61;"java.util.*" pageEncoding&#61;"UTF-8"%>
<%
String path &#61; request.getContextPath();
String basePath &#61; request.getScheme()&#43;"://"&#43;request.getServerName()&#43;":"&#43;request.getServerPort()&#43;path&#43;"/";
%>
<%&#64; taglib uri&#61;"http://java.sun.com/jsp/jstl/core" prefix&#61;"c" %>
<%&#64; taglib uri&#61;"http://java.sun.com/jsp/jstl/fmt" prefix&#61;"fmt" %>
<html><head><base href&#61;"<%&#61;basePath%>"><title>My JSP &#39;update.jsp&#39; starting pagetitle><meta http-equiv&#61;"pragma" content&#61;"no-cache"><meta http-equiv&#61;"cache-control" content&#61;"no-cache"><meta http-equiv&#61;"expires" content&#61;"0"> <meta http-equiv&#61;"keywords" content&#61;"keyword1,keyword2,keyword3"><meta http-equiv&#61;"description" content&#61;"This is my page">head><body><form action&#61;"<%&#61;request.getContextPath() %>/userAction!update.action" method&#61;"post"><input type&#61;"hidden" name&#61;"user.password" value&#61;"${user.password }"><input type&#61;"hidden" name&#61;"user.eid" value&#61;"${user.eid }"><table><tr><td>姓名td><td><input type&#61;"text" name&#61;"user.ename" value&#61;"${user.ename }">td>tr><tr><td>所属部门td><td><input type&#61;"text" name&#61;"user.dept" value&#61;"${user.dept }">td>tr><tr><td>年龄td><td><input type&#61;"text" name&#61;"user.age" value&#61;"${user.age }">td>tr><tr><td>性别td><td><input type&#61;"text" name&#61;"user.gender" value&#61;"${user.gender }">td>tr><tr><td>入职时间td><td><input type&#61;"text" name&#61;"user.workDate" value&#61;"
html>