Freemark与Spring MVC的整合

来源:互联网 发布:阿里云服务器ecs配置 编辑:程序博客网 时间:2024/04/29 15:50

1、项目的视图存放位置


2、整合spring MVC 的配置,web.xml的配置如下

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
  6.     <display-name></display-name>  
  7.     <!-- 加载spring容器 -->  
  8.     <context-param>  
  9.         <param-name>contextConfigLocation</param-name>  
  10.         <param-value>classpath:applicationContext-*.xml</param-value>  
  11.     </context-param>  
  12.     <listener>  
  13.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  14.     </listener>  
  15.     <!-- springMVC前端控制器 -->  
  16.     <servlet>  
  17.         <servlet-name>springmvc</servlet-name>  
  18.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  19.         <init-param>  
  20.             <param-name>contextConfigLocation</param-name>  
  21.             <param-value>classpath:springmvc.xml</param-value>  
  22.         </init-param>  
  23.     </servlet>  
  24.     <servlet-mapping>  
  25.         <servlet-name>springmvc</servlet-name>  
  26.         <url-pattern>*.action</url-pattern>  
  27.     </servlet-mapping>  
  28.   
  29.     <!--主页的配置 -->  
  30.     <welcome-file-list>  
  31.         <welcome-file>index.jsp</welcome-file>  
  32.     </welcome-file-list>  
  33. </web-app>  
2、FreeMarker与SpringMVC整合,首先,在springmvc的配置文件普通视图之前,加入freemarker的视图。Spring MVC的配置文件,

springmvc.xml

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
  8.         http://www.springframework.org/schema/mvc   
  9.         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   
  10.         http://www.springframework.org/schema/context   
  11.         http://www.springframework.org/schema/context/spring-context-3.2.xsd   
  12.         http://www.springframework.org/schema/aop   
  13.         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
  14.         http://www.springframework.org/schema/tx   
  15.         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">  
  16.   
  17.     <!-- 可以扫描controller、service、... 这里让扫描controller,指定controller的包 -->  
  18.     <context:component-scan base-package="com.controller"></context:component-scan>  
  19.     <!--静态资源的处理 -->  
  20.     <mvc:resources location="/resources/" mapping="/resources/**" />  
  21.     <!--mvc的注解开发 -->  
  22.     <mvc:annotation-driven></mvc:annotation-driven>  
  23.       
  24.     <!-- freemarker config -->  
  25.     <bean id="freemarkerConfig"  
  26.         class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
  27.         <property name="templateLoaderPath" value="/WEB-INF/ftl/" />  
  28.         <!-- 设置FreeMarker环境属性 -->  
  29.         <property name="freemarkerSettings">  
  30.             <props>  
  31.                 <!--刷新模板的周期,单位为秒 -->  
  32.                 <prop key="template_update_delay">20</prop>  
  33.                 <!--模板的编码格式 -->  
  34.                 <prop key="default_encoding">UTF-8</prop>  
  35.                 <!--本地化设置 -->  
  36.                 <prop key="locale">UTF-8</prop>  
  37.                 <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>  
  38.                 <prop key="time_format">HH:mm:ss</prop>  
  39.                 <prop key="number_format">0.####</prop>  
  40.                 <prop key="boolean_format">true,false</prop>  
  41.                 <prop key="whitespace_stripping">true</prop>  
  42.                 <prop key="tag_syntax">auto_detect</prop>  
  43.                 <prop key="url_escaping_charset">UTF-8</prop>  
  44.             </props>  
  45.         </property>  
  46.   
  47.     </bean>  
  48.   
  49.     <!-- View resolvers can also be configured with ResourceBundles or XML files.   
  50.         If you need different view resolving based on Locale, you have to use the   
  51.         resource bundle resolver. -->  
  52.     <bean id="viewResolver"  
  53.         class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
  54.         <property name="cache" value="true" />  
  55.         <property name="prefix" value="" />  
  56.         <property name="suffix" value=".ftl" />  
  57.         <property name="contentType" value="text/html; charset=UTF-8"></property>  
  58.         <property name="viewClass">  
  59.             <value>org.springframework.web.servlet.view.freemarker.FreeMarkerView  
  60.             </value>  
  61.         </property>  
  62.     </bean>  
  63.   
  64.     <!-- 视图解析器 解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包 -->  
  65.     <bean  
  66.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  67.         <!-- 配置jsp路径的前缀 -->  
  68.         <property name="prefix" value="/WEB-INF/jsp/" />  
  69.         <!-- 配置jsp路径的后缀 -->  
  70.         <property name="suffix" value=".jsp" />  
  71.     </bean>  
  72.   
  73. </beans>  

3、Spring相关配置文件的加载

      3.1 db.properties

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. jdbc.driver=com.mysql.jdbc.Driver  
  2. jdbc.url=jdbc:mysql://localhost:3306/student  
  3. jdbc.username=root  
  4. jdbc.password=1234  

      3.2 applicationContext-dao.xml

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
  7.         http://www.springframework.org/schema/mvc   
  8.         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   
  9.         http://www.springframework.org/schema/context   
  10.         http://www.springframework.org/schema/context/spring-context-3.2.xsd   
  11.         http://www.springframework.org/schema/aop   
  12.         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
  13.         http://www.springframework.org/schema/tx   
  14.         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">  
  15.   
  16.     <!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->  
  17.     <context:property-placeholder location="classpath:db.properties" />  
  18.   
  19.     <!-- 配置数据源,dbcp -->  
  20.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  21.         destroy-method="close">  
  22.         <property name="driverClassName" value="${jdbc.driver}" />  
  23.         <property name="url" value="${jdbc.url}" />  
  24.         <property name="username" value="${jdbc.username}" />  
  25.         <property name="password" value="${jdbc.password}" />  
  26.         <property name="maxActive" value="30" />  
  27.         <property name="maxIdle" value="5" />  
  28.     </bean>  
  29.   
  30.     <!-- sqlSessionFactory -->  
  31.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  32.         <!-- 数据库连接池 -->  
  33.         <property name="dataSource" ref="dataSource" />  
  34.         <!-- 加载mybatis的全局配置文件 -->  
  35.         <property name="configLocation" value="classpath:sqlMapConfig.xml" />  
  36.     </bean>  
  37.   
  38.     <!-- mapper扫描器 -->  
  39.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  40.         <!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->  
  41.         <property name="basePackage" value="com.mapper"></property>  
  42.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
  43.     </bean>  
  44.   
  45. </beans>  

      3.3 applicationContext-transaction.xml

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
  7.         http://www.springframework.org/schema/mvc   
  8.         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   
  9.         http://www.springframework.org/schema/context   
  10.         http://www.springframework.org/schema/context/spring-context-3.2.xsd   
  11.         http://www.springframework.org/schema/aop   
  12.         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
  13.         http://www.springframework.org/schema/tx   
  14.         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">  
  15.   
  16.     <!-- 事务管理器 对mybatis操作数据库进行事务控制,spring使用jdbc的事务控制类 -->  
  17.     <bean id="transactionManager"  
  18.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  19.         <!-- 数据源 dataSource在applicationContext-dao.xml中已经配置 -->  
  20.         <property name="dataSource" ref="dataSource" />  
  21.     </bean>  
  22.     <!-- 通知 -->  
  23.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  24.         <tx:attributes>  
  25.             <!-- 传播行为 -->  
  26.             <tx:method name="save*" propagation="REQUIRED" />  
  27.             <tx:method name="delete*" propagation="REQUIRED" />  
  28.             <tx:method name="insert*" propagation="REQUIRED" />  
  29.             <tx:method name="update*" propagation="REQUIRED" />  
  30.             <tx:method name="find*" propagation="SUPPORTS" read-only="true" />  
  31.             <tx:method name="get*" propagation="SUPPORTS" read-only="true" />  
  32.             <tx:method name="select*" propagation="SUPPORTS" read-only="true" />  
  33.         </tx:attributes>  
  34.     </tx:advice>  
  35.   
  36.     <!-- aop -->  
  37.     <aop:config>  
  38.         <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.service.*.*(..))" />  
  39.     </aop:config>  
  40. </beans>  

      3.4 applicationContext-service.xml

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
  7.         http://www.springframework.org/schema/mvc   
  8.         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   
  9.         http://www.springframework.org/schema/context   
  10.         http://www.springframework.org/schema/context/spring-context-3.2.xsd   
  11.         http://www.springframework.org/schema/aop   
  12.         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
  13.         http://www.springframework.org/schema/tx   
  14.         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">  
  15.     <context:spring-configured />  
  16.     <!--扫描Service的package -->  
  17.     <context:component-scan base-package="com.service" />  
  18.   
  19.   
  20. </beans>  

4、Mybatis相关的配置文件

      4.1 sqlMapConfig.xml

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE configuration  
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">  
  5. <configuration>  
  6.   
  7.     <!-- 全局setting配置 ,根据需要再添加 -->  
  8.   
  9.     <!-- 配置别名 -->  
  10.     <typeAliases>  
  11.         <!-- 批量扫描别名 -->  
  12.         <package name="com.entity" />  
  13.     </typeAliases>  
  14.   
  15.     <!-- 配置mapper   
  16.     由于使用spring和mybatis的整合包进行整合,这了无需配置   
  17.     但必须遵循:mapper.java和mapper.xml文件同名且在同一目录 -->  
  18.     <!--<mappers></mappers> -->  
  19. </configu  

      4.2 NavMapper.xml

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper  
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  5.   
  6. <!-- namespace命名空间,作用就是对sql进行分类化管理,理解sql隔离 注意:使用mapper代理方法开发,namespace有特殊重要的作用 -->  
  7. <mapper namespace="com.mapper.NavMapper">  
  8.   
  9.     <select id="findNavById" parameterType="int" resultType="nav">  
  10.         SELECT  
  11.         *  
  12.         FROM nav WHERE id=#{id}  
  13.     </select>  
  14.   
  15.     <select id="findAll" resultType="nav">  
  16.         SELECT  
  17.         * FROM  
  18.         nav  
  19.     </select>  
  20.   
  21.     <insert id="add" parameterType="nav">  
  22.         insert into nav(name)  
  23.         values(#{name})  
  24.     </insert>  
  25.   
  26.     <update id="update" parameterType="nav">  
  27.         update nav set name=#{name}  
  28.         where id=#{id}  
  29.     </update>  
  30.     <delete id="delete" parameterType="int">  
  31.         delete from nav where id=#{id}  
  32.     </delete>  
  33. </mapper>  

      4.3 NavMapper.Java与NavMapper.xml


5、log4j相关的配置文件

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. # Global logging configuration  
  2. log4j.rootLogger=DEBUG, stdout  
  3. # Console output...  
  4. log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
  5. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
  6. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n  
6、关于页面的放置


7、NavController的实现

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. import java.util.List;  
  2. import java.util.Map;  
  3. import javax.annotation.Resource;  
  4. import org.springframework.stereotype.Controller;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6. import com.entity.Nav;  
  7. import com.service.NavService;  
  8.   
  9. @Controller  
  10. @RequestMapping("/navController")  
  11. public class NavController {  
  12.     @Resource  
  13.     private NavService navService;  
  14.   
  15.     public void setNavService(NavService navService) {  
  16.         this.navService = navService;  
  17.     }  
  18.   
  19.     @RequestMapping("/jsp")  
  20.     public String toJspPage() {  
  21.         return "index";  
  22.     }  
  23.   
  24.     @RequestMapping("/freemark")  
  25.     public String toFreemarkPage(Map<String, Object> model) {  
  26.         List<Nav> navs = navService.findAll();  
  27.         model.put("navs", navs);  
  28.         return "top";  
  29.     }  
  30. }  

8、访问测试



源码下载


转载自: http://blog.csdn.net/zbw18297786698/article/details/53885516

0 0
原创粉丝点击