本文使用的IDE是MyEclipse6.5,spring2和hibernate3.1根据向导就可以很容易的加入到应用中去,只有struts2需要手工的加入。下面我一步一步的帮大家整合struts2+spring2+hibernate3.1:
1.新建一Web Project:Project Name随便命名,这里填demo,J2EE Specification Level选择Java EE 5.0
2.定位到项目根节点,菜单:MyEclipse->Project Capabilities->Add Hibernate Capabilities,我这里选择的是Hibernate3.1,使用默认的勾选值,下一步,再下一步,选择刚才建立的数据源,下一步,选择一个package,完成。
3.开始测试hibernate是否可用,编写代码:
T1.class
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->public class T1 {
public void insert()
{
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
Transaction trans = session.beginTransaction();
QaAccount account = new QaAccount();
account.setAccountName("abc");
account.setAccountPwd("123");
account.setAccountPoint(0);
session.save(account);
trans.commit();
session.close();
}
}
t1.jsp
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><%
T1 t1 = new T1();
t1.insert();
%>
试试看吧,应该可以用Hibernate往数据库插入数据了。
4.加入Struts2,从网上下载Struts2的包,下载地址:http://apache.mirror.phpchina.com/struts/binaries/struts-2.0.11.2-all.zip
把Struts2包里的apps/struts2-blank-2.0.11.2.war->WEB-INF/lib里面的5个包拷贝到项目WebRoot/WEB-INFO/lib中。
然后修改web.xml,添加Struts2的过滤器,整个web.xml的内容如下(绿色字为新增内容):
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><?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">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<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>
在src目录添加struts.xml文件,内容如下:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><?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>
<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.devMode" value="false" />
<package name="default" namespace="/" extends="struts-default">
<!-- Add actions here -->
</package>
</struts>
现在开始使用struts2,做一个最简单的应用:
添加一个类T2.class,内容:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->package com.abc.action;
import com.opensymphony.xwork2.ActionSupport;
public class T2 extends ActionSupport {
private String message;
@Override
public String execute() throws Exception {
setMessage("Hello World!this is a test");
return SUCCESS;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
添加一个jsp页面t2.jsp:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->package com.abc.action;
import com.opensymphony.xwork2.ActionSupport;
public class T2 extends ActionSupport {
private String message;
@Override
public String execute() throws Exception {
setMessage("Hello World!");
return SUCCESS;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
现在需要把T2.class和t2.jsp联系起来,这里需要修改struts2.xml配置文件,修改后的struts2.xml内容为(绿色字为新增内容):
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><?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>
<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.devMode" value="false" />
<package name="com.abc.action" namespace="/" extends="struts-default">
<action name="t2" class="com.abc.action.T2">
<result>/t2.jsp</result>
</action>
<!-- Add actions here -->
</package>
</struts>
重启一下tomcat,在浏览器中浏览:http://127.0.0.1:8080/demo/t2.action,这时应该可以显示效果了
5.最后开始添加Spring了,菜单:MyEclipse->Project Capabilities->Add Spring Capabilities
勾选:Spring 2.0 AOP Libraries
Spring 2.0 Core Libraries
Spring 2.0 Persistence Core Libraries
Spring 2.0 Web Libraries
需要其他库的话,可以以后再添加。一路next,完成向导。
然后就是最后的集成部分了,呼呼,终于快写完了。
①把src/applicationContext.xml剪贴到WebRoot/WEB-INF/applicationContext.xml
②修改web.xml,完整内容为(绿色字为新增内容):
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><?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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
③修改struts.xml,添加内容:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><constant name="struts.objectFactory" value="spring" />
④添加struts2的spring plugin到项目中,一定要加,不然会出错。
文件名为struts2-spring-plugin-2.0.xx.x.jar,struts2的完整包里面有
6.最后再加入log4j
这是一个最简单的log4j配置文件log4j.properties
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->log4j.rootCategory=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p (%F\:%L) - %m%n
这个文件要放到src目录里
现在struts2+spring2+hibernate3.1的整合就完成了。
来源:http://www.cnblogs.com/modou/articles/1279999.html
分享到:
相关推荐
Struts1.2+Spring2.0+Hibernate3.1Struts1.2+Spring2.0+Hibernate3.1Struts1.2+Spring2.0+Hibernate3.1Struts1.2+Spring2.0+Hibernate3.1
应广大网友需求,自己抽了点时间写了一个Struts2.0+Spring2.0+Hibernate3.1的登录及增删改查的完整演示示例,以此奉献给大家,由于本人水平有限,有写的不好的地方还望大家多提宝贵意见,如果有不明白的地方请关注我的...
eclipse整合Struts1.2 + hibernate3.1 + spring2.0 相关链接:http://blog.csdn.net/shellwin/archive/2010/07/02/5708865.aspx
《轻量级Java EE企业应用实战:Struts2+Spring4+Hibernate整合开发(第4版)》是《轻量级Java EE企业应用实战》的第4版,第4版保持了前几版内容全面、深入的特点,主要完成全部知识的升级。 《轻量级Java EE企业...
笔者开发时所用到的软件: apache-tomcat-7.0.25.exe eclipse-jee-indigo-SR2-win32.zip mysql-5.5.20-win32.msi navicat100_mysql_en.zip ...适合于初学Struts2+Spring+Hibernate的开发人员参考学习。
Struts1.2+Struts2.1.6+spring 2.0+hibernate3.1+Ibatis2.3 SSH
一个struts2+spring+hibernate+dwr的小实例,不过木有搭建说明,不过有dwr的使用说明,至于表结构可以看映射配置文件就知道创建什么表了
spring3.1+hibernate4.1+struts2整合所有jar包
struts2+hibernate3.1+spring2.0指导
Struts1.2+Spring1.2+HIbernate3.1整合一个用户登录的例子,原以为这个很简单没有必要上传上去,但问我的人太多了,所以就发布上来了,我随后会在博客里做详细的介绍,请大家继续关注我的博客.
struts2.0+spring2.0+hibernate3.1+web应用
Struts1.2+Struts2.1.6+spring 2.0+hibernate3.1+Ibatis2.3内个框架的集成
struts2.3.4+spring3.1+hibernate4.0 整合
struts2.2.3+spring3.1+hibernate3.67整合
Struts2.3+Spring3.1+Hibernate3.6整合所用到的所有包
Extjs3.1+struts2+spring2.5+hibernate3.5环境
Struts2+Spring2+Hibernate3.1开发环境搭建
Struts2+Hibernate3.1+Spring2.5
struts1.2+spring2.0+hibernate3.1jar包
struts2.3+spring3.1+hibernate3.6整合jar包,非常详细的包结构