整合struts2+spring2+hibernate3.1

本文使用的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

你可能感兴趣的:(spring,log4j,Web,struts,javaee)