Spring3 MVC + jQuery easyUI 做的ajax版本用户管理
透视未来 – 埃森哲愿景大赛 浏览 2053 次
主题:Spring3 MVC + jQuery easyUI 做的ajax版本用户管理 精华帖 (1) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0) 作者 正文
发表时间:昨天 最后修改:昨天
猎头职位: 相关文章: 推荐群组: GT-Grid 更多相关推荐 上周写了篇基于spring3.0.5 mvc 简单用户管理实例 ( )
还是要先配置web.xml文件,如下: <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>Set Character Encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value><!-- 强制进行转码 --> </init-param> </filter> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 默认所对应的配置文件是WEB-INF下的{servlet-name}-servlet.xml,这里便是:spring3-servlet.xml --> <servlet> <servlet-name>spring3</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring3</servlet-name> <!-- 这里可以用 / 但不能用 /* ,拦截了所有请求会导致静态资源无法访问,所以要在spring3-servlet.xml中配置mvc:resources --> <url-pattern>/</url-pattern> </servlet-mapping>然后在applicationContext.xml中简单配置一下数据源和sessionFactory还有声明一下事务处理,这个和之前那一版是一样的,如下: <?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=" " default-autowire="byName"> <!-- 注意上面的default-autowire="byName",如果没有这个声明那么HibernateDaoSupport中的sessionFactory不会被注入 --> <!-- 约定优于配置,约定优于配置 --> <bean> <property value="com.mysql.jdbc.Driver"></property> <property value="jdbc:mysql://127.0.0.1:3306/test"></property> <property value="root"></property> <property value="root"></property> </bean> <bean> <property ref="dataSource"/> <property> <list><!-- 这里直接映射的pojo类所在的包,简单方便不用没次加一个pojo类都需要到这里来添加 --> <value>classpath:com/fsj/spring/model</value> </list> </property> <property> <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> <property ref="sessionFactory"></property> </bean> <aop:config> <aop:advisor pointcut="execution(* com.fsj.spring.service.*Service.*(..))" advice-ref="txAdvice"/> </aop:config> <tx:advice transaction-manager="txManager"> <tx:attributes> <tx:method read-only="true"/> <tx:method read-only="true"/> <tx:method read-only="true"/> <tx:method read-only="true"/> <tx:method rollback-for="Exception"/> </tx:attributes> </tx:advice> </beans>最后需要配置的就是spring mvc的配置文件spring3-servlet.xml了,这个和非ajax的版本稍微有点不一样,加入了对json数据转换的配置,因为我用的数据传输都是json类型的数据,如下: <?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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" " default-autowire="byName"> <!-- default-autowire="byName",约定优于配置,约定优于配置 --> <!-- 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd --> <mvc:resources mapping="/img/**" location="/img/"/> <mvc:resources mapping="/js/**" location="/js/"/> <mvc:resources mapping="/css/**" location="/css/"/> <mvc:resources mapping="/html/**" location="/html/"/> <!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --> <context:component-scan base-package="com.fsj.spring.web" /> <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射,添加拦截器,类级别的处理器映射 --> <bean> <property> <list> <bean/> </list> </property> </bean> <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射, 配置一个基于注解的定制的WebBindingInitializer,解决日期转换问题,方法级别的处理器映射 --> <bean> <property value="0" /> <property> <bean /> </property> <!-- 配置一下对json数据的转换 --> <property> <list> <bean></bean> </list> </property> </bean> <!-- ③:对模型视图名称的解析,即在模型视图名称添加前后缀 InternalResourceViewResolver默认的就是JstlView所以这里就不用配置viewClass了 --> <bean> <property value="/WEB-INF/view/"></property> <property value=".jsp"></property> </bean> </beans> (责任编辑:JavaVideo) |

