J2EE之SSH搭建(struts2.1+hibernate3.3+spring3.0 整合 非小白教程)

工具:myecplise10+struts2.1+hibernate3.3+spring3.0

第一步:新建工程 

 File-->new-->Web Project:


第二步:搭建struts2框架

引入struts2:



看到此页面,选择好选项后,点击Finish

WebRoot下的lib文件夹中的web.xml会自动配置好,如下

web.xml:

<?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">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <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>/*</url-pattern>
  </filter-mapping></web-app>

struts2配置:

<?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="包名" extends="struts-default" namespace="/">
<action name="Action名" class="实例名" method="方法名">
<result name="返回值"></result>
</action>
</package>
</struts>    

此时可以随意在struts.xml写一个Action,测试一下是否成功,避免之后错误难以发现。

第三步:

搭建spring3.0

由于需要整合spring和structs2, 添加Struts2-Spring插件包

struts2-spring-plugin-2.1.8.1



选择5个包(此为spring3.0版本,不同版本选项不同)




点击next后,



点击Finish,此时spring已搭建好。

web.xml配置添加监听器,如下:

<listener>

         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

web.xml完整配置为:

<?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">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 指定Spring配置路径  --> 
<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>/WEB-INF/applicationContext.xml</param-value>  
</context-param>  
  <!-- Spring 启动 IOC 容器的 ServletContextListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Strut2 启动 过滤器 -->
  <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>/*</url-pattern>
  </filter-mapping></web-app>


第四部:

搭建hibernate3.3






点击next直到出现下图,我通过DB driver连接mysql数据库(如果不会,请自行搜索或者手动填入数据,注意根据数据库选择对应的驱动)



点击next显示如下图,由于通过spring整合,所以不用建立Session工程类,点击Finish


点击完成,至此,基本框架已搭建好。

第五步   建包,配置


由于是MVC,又由spring整合,所以要建立抽象类和实现类,如com.soft.dao中的UserDao只有抽象方法,com.soft.dao.impl的实UserDaoImpl类现接口,实现方法。



很多同学因为spring配置出错,下面给出applicationContext配置代码:

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
    >

<--数据库配置-->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/sshshixun">
</property>
<property name="username" value="root"></property>
<property name="password" value="root"></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>
<prop key="hibernate.show_sql">
true
</prop>
<prop key="hibernate.format_sql">
true
</prop>

</props>

</property>

<!-- 数据库表映射文件配置 -->

<property name="mappingResources">
<list>
<value>com/soft/beans/Test1.hbm.xml</value></list>
</property></bean>

<bean id="userDAO" class="com.soft.dao.impl.UserDaoImpl" scope="singleton">
<property name="sessionFactory" ref="sessionFactory"></property>


</bean>

<!-- bean配置 -->

<bean id="userService" class="com.soft.service.impl.UserServiceImpl" scope="singleton">
<property name="userDao" ref="userDAO"></property></bean>

<!-- Spring AOP 事务管理配置 -->
<!-- 1. 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 2. 配置事务属性,编写切面通知,对哪些方法进行增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- tx:method 配置切入点方法
     name:方法名称,*表示任意方法,xxxx*表示以xxxx开头的方法;
     propagation:设置传播行为;
     isolation:设置隔离级别;
     read-only:是否只读
  -->
<tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="query*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
</tx:attributes>
</tx:advice>

<!-- 3. 配置事务切点, 并把切点和事务属性关联起来,配置到Service层 -->
<aop:config>
    <!-- 3.1 定义切入点 -->
<aop:pointcut id="txPointcut" expression="execution(* com.soft.service..*.*(..))" />
<!-- 3.2关联切入点与增强的通知 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>
<!-- action配置 -->
<bean id="userAction" class="com.soft.action.UserAction" scope="singleton">
<property name="userService" ref="userService"></property>

</bean>

</beans>


配置好struts.xml后,ssh整合可以使用。


新手在线求教SSH框架(struts2.1 spring3.0 hibernate3.3)搭建问题

01-29

各位高手帮忙!刚才用add capabilities的方法搭建了一个SSH框架,什么也没有写,运行一下就报错了,连index.jsp都看不到,报错如下,请指教!rn2011-1-29 22:56:38 org.apache.catalina.core.StandardContext filterStartrn严重: Exception starting filter struts2rnClass: com.opensymphony.xwork2.spring.SpringObjectFactoryrnFile: SpringObjectFactory.javarnMethod: getClassInstancernLine: 209 - com/opensymphony/xwork2/spring/SpringObjectFactory.java:209:-1rn at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:431)rn at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:69)rn at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)rn at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:269)rn at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:250)rn at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:368)rn at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:98)rn at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4193)rn at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4792)rn at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:138)rn at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:785)rn at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:763)rn at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:558)rn at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1124)rn at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1047)rn at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:542)rn at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1390)rn at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:355)rn at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)rn at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:89)rn at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:312)rn at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:292)rn at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:998)rn at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:772)rn at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:138)rn at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:990)rn at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:275)rn at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:138)rn at org.apache.catalina.core.StandardService.startInternal(StandardService.java:424)rn at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:138)rn at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:648)rn at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:138)rn at org.apache.catalina.startup.Catalina.start(Catalina.java:576)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)rn at java.lang.reflect.Method.invoke(Method.java:597)rn at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)rn at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:415)rnCaused by: java.lang.NullPointerExceptionrn at com.opensymphony.xwork2.spring.SpringObjectFactory.getClassInstance(SpringObjectFactory.java:209)rn at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyResultType(XmlConfigurationProvider.java:519)rn at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addResultTypes(XmlConfigurationProvider.java:490)rn at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:446)rn at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:264)rn at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111)rn at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:193)rn at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55)rn at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:374)rn at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:418)rn ... 38 morern2011-1-29 22:56:38 org.apache.catalina.core.StandardContext startInternalrn严重: Error filterStartrn2011-1-29 22:56:38 org.apache.catalina.core.StandardContext startInternalrn严重: Context [/SSH4] startup failed due to previous errorsrn2011-1-29 22:56:38 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbcrn严重: The web application [/SSH4] registered the JBDC driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.rn2011-1-29 22:56:38 org.apache.catalina.loader.WebappClassLoader clearThreadLocalMaprn严重: The web application [/SSH4] created a ThreadLocal with key of type [null] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@194e776]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@e80740]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak.rn2011-1-29 22:56:38 org.apache.catalina.loader.WebappClassLoader clearThreadLocalMaprn严重: The web application [/SSH4] created a ThreadLocal with key of type [null] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@1342545]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@7b1641]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak.rn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭