2007-10-11

SSH2: Struts2 + Spring + Hibernate 的登录

关键字: Struts2 Spring Hibernate
使用分层结构,包括 dao,service,control,web层

Struts2 集成 Spring 需要以下几个步骤

1 加入struts2-spring-plugin-2.0.9.jar到项目中(web项目就是丢到/WEB-INF/lib中)

2 Web.xml文件里加入以下listener
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--默认加载/WEB-INF 目录下的applicationContext.xml -->

如果需要其它的spring配置文件可以在web.xml中加入以下配置
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
</context-param>
<!--param-value 中间的多个配置文件可以使用 , 隔开-->


3 在/WEB-INF 目录下新建applicationContext.xml,并在其中配置Struts2的action及其它需要的bean

4 在struts.xml中将 <action 元素中 class="" 的属性改为spring配置文件中的id
         <!-- spring 配置文件中配置 action -->
 <bean id="loginAction" class="login.LoginAction"
		scope="prototype">
		<property name="userService" ref="userService" />
	</bean>


         <!-- struts 配置文件中配置 action 注意class属性不再是类带路径全名了 -->
 <action name="Login" class="loginAction">
			<result name="success">/login/success.jsp</result>
			<result name="error">/login/error.jsp</result>
	</action>


-
-
-

Spring 集成 Hibernate 就不累述了,网上到处都有

-
-

附件里有原代码,导入eclipse(需要myeclipse插件),修改数据源,就能运行了

数据源在src目录下dataSource.properties里,我将数据源配置在外部properties

文件里,spring的配置文件动态载入其内容,如果使用的不是mysql,请注意修改hibernate方言
<prop key="hibernate.dialect">
    org.hibernate.dialect.MySQLDialect
</prop>


说明:项目内没有所需要的包(struts2,spring2,hibernate3),导入项目以后需要自己

导入相应的三方包,spring使用的是2.0

-

struts2 好像支持不了spring2.0 基于 XML Schmea 的配置,基于这个配置事务应用程序就出错,如果有人知道怎么在struts2集成spring2中使用spring2的xml Schmea的配置,请告知
  • SSH2.rar (18.5 KB)
  • 描述: Struts2 + Spring + Hibernate 的登录
  • 下载次数: 1819
评论
sinostone 2008-06-19
楼主,我看了你的配置文件,struts.xml中未指定struts动作由spring来接管,但是却在spring的配置文件中注入了struts的动作。请问这是怎么回事,是不是哪里出了差错?
xiquwgugou 2008-04-25
nice job,这三个结合,我觉得包最难受,老出错。
jackzhangyunjie 2008-03-30
不明白你为什么要在Spring中配置Action的Bean,根本没有这个必要的。
Action里只用把Spring的Bean注入过来就可以用啦。
还有你有代码合理性有问题。
moses3017 2008-03-20
Please use zip to compress, I can not open it on Linux.Thank you.
ericxu131 2008-03-20
fuliang 写道
kyo100900 写道
不错,可以继续重构。不过你所说的:“struts2 好像支持不了spring2.0 基于 XML Schmea 的配置,基于这个配置事务应用程序就出错”。我没有太明白

我使用的是XML Schmea 的配置没有出现问题.
<aop:config>
   <aop:pointcut id="serviceOperation" expression="execution(* edu.jlu.fuliang.Service.impl.*ServiceImpl.*(..))"/>
   <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/>
</aop:config>	
<tx:advice id="txAdvice">
	<tx:attributes>
	   <tx:method name="get*" read-only="true"/>
	   <tx:method name="*"/>
	</tx:attributes>
</tx:advice>
	
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	<property name="sessionFactory">
		<ref bean="sessionFactory"/>
	</property>
</bean>



	<aop:config proxy-target-class="true">
		<aop:pointcut id="managerPointcut"
			expression="execution(* com..*.*Manager.*(..))" />
		<aop:advisor id="managerTx" pointcut-ref="managerPointcut"
			advice-ref="txAdvice" order="0" />
	</aop:config>
	<tx:advice id="txAdvice"
		transaction-manager="dataSourceTransactionManager">
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="find*" propagation="REQUIRED"
				read-only="true" />
			<tx:method name="update*" propagation="REQUIRED" />

			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>

我这个配置就不行sad

非常奇怪

而且我每次访问action几次以后就访问不了action了 白屏 而且不进action

最可恶的是还不报错。。。。。。。

ningshuihan 2008-03-20
楼主可以试下配置下Tomcat的日志输出,找到错误所在。开发过程也碰到过这个错误,配置了Tomcat的日志后才找到原因,Tomcat默认的日志输出有些错误提示不准确。
jackson1225 2007-11-22
请问:源码success.jsp中
欢迎${request.user.name}

可以显示吗?为什么我用了之后不显示用户名,而是原样输出!
另外我在LoginAction中加入了
ActionContext ctx = ActionContext.getContext();
ctx.getSession().put("username",user.getName());

在success.jsp中加入
<s:property value="username" />
<s:property value="#{sessionScope.username}"/>
${sessionScope.username}

都不能正确显示用户名!而我不用spring,只用struts就可以正确输出.如果用了spring,即使action不用spring管理都不会正确显示.不知是什么原因啊,都整了一天了,各位有碰到过这样的问题吗?
zh950 2007-11-20
楼主你好,我前几天也做了一个类似的东西,在form中,action=xxx,xxx必须加.action,否则提交时不能自动加上.action后缀,但是在同一页面中,用url引用时,就能自动加上.action后缀,我用的是struts2.0.9,spring是2.0.6。不知道是怎么回事。
刚下载了你的源码,回去试一下。
yufei 2007-10-26
楼上的,导入struts2,spring2,和hibernate3的包了吗?
dataSource.properties文件里的数据库配置信息了吗?
不是mysql,修改hibernate配置文件的数据库方言了吗?
asdfasdf 2007-10-26
lz的例子有问题


呵呵 不能使用
锁上门睡觉 2007-10-23
我下载文件,import到eclipse。运行test.java文件,既然有这样的错误:
Exception in thread "main" org.springframework.beans.factory.BeanInitializationException:
Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/classes/dataSource.properties] cannot be opened because it does not exist
java.io.FileNotFoundException: class path resource [WEB-INF/classes/dataSource.properties] cannot be opened because it does not exist

at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:137)
at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:183)
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:162)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:69)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:373)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:295)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:87)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:72)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:63)
at dao.Test.main(Test.java:12)

我查看了/WEB-INF/classes/dataSource.properties 这个文件是存在的
allenny 2007-10-22
好像用Spring的aop方法拦截和Struts2的ActionContext有冲突,两者无法同时使用,不知到楼主有没有碰到过这类问题。
techno_it 2007-10-22
我也没有遇到问题。
pdw2009 2007-10-22
spring2也支持dtd
jiangzy 2007-10-19
少了包或多了很包,请检查
yufei 2007-10-17
引用
是否Web.xml配置有误,仔细检查了吗?

web.xml只有下面那点内容....
而且只要把spring配置文件里事务的配置方式改为1.x的配置方式,而不使用XML Schmea配置,就不会有问题
<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>
kyo100900 2007-10-17
yufei 写道
遇见鬼了,我只要一使用XML Schmea配置,就会出错
报下面的错误.....
2007-10-16 21:06:54 org.apache.catalina.core.StandardContext start
严重: Error listenerStart
2007-10-16 21:06:54 org.apache.catalina.core.StandardContext start
严重: Context [/ssh2] startup failed due to previous errors



是否Web.xml配置有误,仔细检查了吗?
alex8946 2007-10-16
不错!
yufei 2007-10-16
遇见鬼了,我只要一使用XML Schmea配置,就会出错
报下面的错误.....
2007-10-16 21:06:54 org.apache.catalina.core.StandardContext start
严重: Error listenerStart
2007-10-16 21:06:54 org.apache.catalina.core.StandardContext start
严重: Context [/ssh2] startup failed due to previous errors
fuliang 2007-10-15
kyo100900 写道
不错,可以继续重构。不过你所说的:“struts2 好像支持不了spring2.0 基于 XML Schmea 的配置,基于这个配置事务应用程序就出错”。我没有太明白

我使用的是XML Schmea 的配置没有出现问题.
<aop:config>
   <aop:pointcut id="serviceOperation" expression="execution(* edu.jlu.fuliang.Service.impl.*ServiceImpl.*(..))"/>
   <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/>
</aop:config>	
<tx:advice id="txAdvice">
	<tx:attributes>
	   <tx:method name="get*" read-only="true"/>
	   <tx:method name="*"/>
	</tx:attributes>
</tx:advice>
	
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	<property name="sessionFactory">
		<ref bean="sessionFactory"/>
	</property>
</bean>
发表评论

提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则

您还没有登录,请登录后发表评论

yufei
搜索本博客
我的相册
220461af-8990-42f1-a8a1-c50eff6ee530-thumb
select package lib
共 38 张
最新评论