惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 无会

[转]java取得Linuxcpu,内存,磁盘实时信息 中国32个省的日语读法 26字母日语读法 65个源代码网站 经典面试问题【转】 An introduction to the Java 2 Platform, Enterprise Edition specification by way of BEA's WebLogic Server java 中文网址大全 自我介绍 [原] 子类访问基类方法 http1.1 MYSQL初学者使用指南 候捷谈Java反射机制 jstl1.0 和 jstl1.1 区别 moiment lrc Hibernate检索策略 日本日記(十二月三日) 自定义Hibernate Dialect解决createSQLQuery时的decimal,long类型问题 hibernate 中 session 说明 java.lang.ClassCastException: org.apache.struts.action.ActionMessage 错误
Struts+Spring+Hibernate整合
无会 · 2007-12-14 · via 博客园 - 无会

Struts+Spring+Hibernate

本次配置环境:Myeclipse5.5、MySQL5.0Struts1.2Spring2.0Hibernate3.1

一、建工程
略。。。。。。

二、要使用StrutsSpringHibernate必须导入必需的包
1Struts(和之前没区别)
2Spring
   分别导入Spring 2.0 Core LibrariesSpring 2.0 Web Libraries
   选择把*.jar Copy到工程/WebRoot/WEB-INF/lib下;点击NEXT
   选择applicationContext.xml的目录,/WebRoot/WEB-INF;点击finish
3Hibernate
   在导入Hibernate时,当然先要配置DataSource咯,这里就不再说了
   选择导入Hibernate全选上
   选上复选框:Hibernate 3.1 Core......、Hibernate 3.1 Advanced......、Spring 2.0 ORM/DAO.......
   同样选择把*.jar Copy到工程/WebRoot/WEB-INF/lib下;点击NEXT

   这里我们选择把hibernate交给spring去管理
 选中单选按钮 Spring configuration file......
 点击NEXT

  选择已存在的applicationContext.xml文件,
   填写SessionFactory ID sessionFactory 点击NEXT

   这里我们需要填写Bean Id dataSource
   选择 DB Driver :选择刚才配置的DataSource 点击NEXT

   这里不需要创建 SessionFactory Class 点击Finish
   注意:查看applicationContext.xml的变化

三、映射VO、数据操作
首先工程的结构建好,比较简单的结构:
org.chenwj.dao
org.chenwj.form
org.chenwj.struts
org.chenwj.struts.action
org.chenwj.vo

映射表userinfo创建持久类到org.chenwj.vo目录
dao下创建数据库操作类 UserDAO 这里只是对数据库进去插入,代码如下:
private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {

       returnsessionFactory;

    }

    publicvoid setSessionFactory(SessionFactory sessionFactory) {

       this.sessionFactory = sessionFactory;

    }

    /* 用户注册 */
  
 publicboolean regist(Userinfo user) {

       try {

           Session session = sessionFactory.openSession();

           Transaction tx = session.beginTransaction();

           session.save(user);

           tx.commit();

           session.close();

           returntrue;

       } catch (Exception ex) {

           ex.printStackTrace();

           returnfalse;

              }
       }
    使用依赖注入,setter设值sessionFactory
    到此数据层已经完成
 

四、配置struts-config.xml
    添加actionformjsp ……
    首先在struts-config.xml添加一个插件
    <plug-in
    className="org.springframework.web.struts.ContextLoaderPlugIn">

       <set-property property="contextConfigLocation"

           value="/WEB-INF/applicationContext.xml" />

    </plug-in>
   
为什么要添回这个插件呢?
   
因为在后面会在applicationContext.xml下配置action,让action交给spring
   
去管理,实现了struts的依赖注入机制
   
接下来添加cuntroller,这里你可以使用DelegatingActionProxy代理
  
<controller processorClass=
   
"org.springframework.web.struts.DelegatingRequestProcessor"/>

    Controller取代了strutsRequestProcessor,在定义action里,我们可以省略
type
属性。(我个人比较喜欢用这个)下面让我们看配置好的struts-config.xml:
<struts-config>

        <data-sources />

        <form-beans>

           <form-bean name="userForm"

           type="org.chenwj.struts.form.UserForm" />

        </form-beans>

    <global-exceptions />

    <global-forwards />

    <action-mappings>

       <action attribute="userForm" input="/index.jsp" name="userForm"

           path="/user" scope="request">

           <forward name="success" path="/success.jsp" />

           <forward name="error" path="/index.jsp" />

       </action><!--type</span><span style="FONT-SIZE: 10pt; COLOR: teal; FONT-FAMILY: 宋体;">属性可不写</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: teal; FONT-FAMILY: 'Courier New';">-->

    </action-mappings>

    <controller processorClass=
"org.springframework.web.struts.DelegatingRequestProcessor"/>

     <message-resources

       parameter="org.chenwj.struts.ApplicationResources" />

     <plug-in

    className="org.springframework.web.struts.ContextLoaderPlugIn">

       <set-property property="contextConfigLocation"

           value="/WEB-INF/applicationContext.xml" />

     FONT-SIZ