论坛首页 Java企业应用论坛

基于spring3.0.5 mvc 简单用户管理实例

浏览 106920 次
该帖已经被评为精华帖
作者 正文
   发表时间:2011-06-24  
liuxuejin 写道
lz
学习springMVC是什么书呢???

最好的书就是spring官方文档
0 请登录后投票
   发表时间:2011-07-08  
一点没觉得这样的东西有什么好用的(个人看法),一大堆的注解难道就比XML文件好???
0 请登录后投票
   发表时间:2011-07-10  
太阳神喻 写道

   

 applicationContext.xml如下:
<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation=" 
          http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-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/context 
          http://www.springframework.org/schema/context/spring-context-3.0.xsd 
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">
		<!-- 注意上面的default-autowire="byName",如果没有这个声明那么HibernateDaoSupport中的sessionFactory不会被注入 -->
		<!-- 约定优于配置,约定优于配置 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></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="dataSource"/>
       <property name="mappingDirectoryLocations">
         <list><!-- 这里直接映射的pojo类所在的包,简单方便不用没次加一个pojo类都需要到这里来添加 -->
            <value>classpath:com/fsj/spring/model</value>
         </list>
       </property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">
					true
				</prop>
			</props>
		</property>
	</bean>
	
	<!-- 自动扫描组件,这里要把web下面的 controller去除,他们是在spring3-servlet.xml中配置的,如果不去除会影响事务管理的。-->
	<context:component-scan base-package="com.fsj.spring">
		<context:exclude-filter type="regex" expression="com.fsj.spring.web.*"/>
	</context:component-scan>
	
	<!-- 下面是配置声明式事务管理的,个人感觉比用注解管理事务要简单方便 -->
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<aop:config>
		<aop:advisor pointcut="execution(* com.fsj.spring.service.*Service.*(..))" advice-ref="txAdvice"/>
	</aop:config>

	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true"/>
			<tx:method name="query*" read-only="true"/>
			<tx:method name="find*" read-only="true"/>
			<tx:method name="load*" read-only="true"/>
			<tx:method name="*" rollback-for="Exception"/>
		</tx:attributes>
	</tx:advice>
	
	
</beans>
 

请教一下,你在applicationContext.xml配置了事务拦截,service层也用注解声明了事务,是不是重复了?

<aop:advisor pointcut="execution(* com.fsj.spring.service.*Service.*(..))" advice-ref="txAdvice"/>

这段的配置的意思应该是拦截service包 所有*Service 类中的所有方法都是用事务控制,

也就是说不管你在service层的代码中是否添加@Service()注解一样可以控制事务?

我没试过,不知道对不对?请楼主赐教

0 请登录后投票
   发表时间:2011-07-11  
踩一脚,以后参考用
0 请登录后投票
   发表时间:2011-07-13  
很详细,不错
0 请登录后投票
   发表时间:2011-08-14  
无论我怎么陪web.xmlD的拦截。/或者/XX/*我的静态资源都被拦截了。
<servlet-mapping>
<servlet-name>yinhex</servlet-name>
<url-pattern>/uc/*</url-pattern>
</servlet-mapping>
配置为:
< mvc:resources  mapping="/javascripts/**"location="/javascripts/"/>
<mvc:resources mapping="/themes/**" location="/css/" />
<mvc:resources mapping="/upload/**" location="/upload/"/>
访问路径为: http://localhost:8080/yinhex/index/javascripts/jquery.bgiframe.js
index为controller的注解.为什么还是被拦截了呢???导致静态资源不可用
0 请登录后投票
   发表时间:2011-08-17  
楼主你好,当配置了mvc:resources 如
<mvc:resources mapping="/css/**" location="/css/" />
之后会导致
<context:component-scan base-package="cn.henu.springmvc.control" />
不可用。修改spring-mvc-3.0.xsd是如何修改的?
0 请登录后投票
   发表时间:2011-09-01  

非常好的一个例子,正学习呢,谢谢

0 请登录后投票
   发表时间:2011-09-15  
赞一个!学习了,楼主很负责任
0 请登录后投票
   发表时间:2011-09-22  
想请问下,怎么没有见到有,spring-webmvc这个包的
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics