zoukankan      html  css  js  c++  java
  • spring mvc+freemarker整合(非注解方式)

    spring jar包下载: http://www.springsource.org/download/community

    freemarker jar包下载:http://ebr.springsource.com/repository/app/bundle/version/detail?name=com.springsource.freemarker&version=2.3.18

    1、创建一个web项目:springmvc

    2、将spring 3.0 中的jar包全部拷贝到项目中来

    3、将freemaker的包也拷贝到项目中来,注意导入commons-logging.jar

    4、配置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">
     
     
     <!--  Spring 服务层的配置文件 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
     
      <!--  Spring 容器启动监听器 -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener
            </listener-class>
        </listener>
     <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <!--为DispatcherServlet建立映射 -->
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
     
     
     
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

    5、在WEB-INF/目录下创建springmvc-servlet.xml文件, 注意springmvc-servlet名称是根据web.xml里配置的servlet名称+servlet.xml来的

    6、配置springmvc-servlet.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:p="http://www.springframework.org/schema/p"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
     <!--以下视图配置根据需要任选一种即可 -->
    
     <bean id="viewResolver"
      class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
      <property name="suffix">
       <value>.ftl</value>
      </property>
      <property name="viewClass">
       <value>org.springframework.web.servlet.view.freemarker.FreeMarkerView
       </value>
      </property>
     </bean>
    
    <bean name="HelloAction" class="myspring.mvc.web.SpringMvcController" />  <!-- 配置 控制器 -->
    
    
     <bean id="urlMapping"
      class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
      <property name="mappings">
       <props>
        <prop key="/hello">HelloAction </prop>  <!-- 映射URL访问 -->
       </props>
      </property>
     </bean>
    
    
    <!-- 配置freemarker -->
     <bean id="freemarkerConfig"        
      class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
      <property name="templateLoaderPath" value="/WEB-INF/view/" />
       <property name="freemarkerSettings"> <props> <prop
       key="template_update_delay">0</prop> <prop
       key="default_encoding">UTF-8</prop> <prop
       key="number_format">0.##########</prop> <prop
       key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <prop
       key="classic_compatible">true</prop> <prop
       key="template_exception_handler">ignore</prop> </props> </property>
     </bean>
    
     
    
    </beans>

    7、在WEB-INF/目录下创建view/ftl/welcome.ftl .

    welcome.ftl 模板文件的内容如下 :

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    info :  ${name}
    </body>
    </html> 

    8、编写控制器:

    package myspring.mvc.web;
    
    import java.lang.annotation.Annotation;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;
    
    public class SpringMvcController  implements Controller{
     
     public ModelAndView handleRequest(HttpServletRequest request,
    
                HttpServletResponse response) throws Exception {
    
    //           request.setAttribute("name", "welcome to spring!");
    //           return new ModelAndView("ftl/welcome");
       ModelAndView mv = new ModelAndView("ftl/welcome");// //welcome就是视图的名称welcome.ftl
       mv.addObject("name"," Hello world , test my first spring mvc ! ");
       return mv;
        }
    
     
    
    }

    9、此时src目录下的applicationContext.xml暂且不需配置

    运行项目,输入:http://localhost:8080/springmvc/hello

    页面会输出: info : Hello world , test my first spring mvc !

    项目结构如下:

    示例源码下载:springmvc+freemarker非注解方式

  • 相关阅读:
    再谈TextField
    IOS-TextField知多少
    leftBarButtonItems
    LeftBarButtonItems,定制导航栏返回按钮
    Apple Mach-O Linker (id) Error "_OBJC_CLASS...错误解决办法 Apple Mach-O Linker (id) Error "_OBJC_CLASS...错误解决办法
    Unrecognized Selector Sent to Instance问题之诱敌深入关门打狗解决办法
    UNRECOGNIZED SELECTOR SENT TO INSTANCE 问题快速定位的方法
    Present ViewController,模态详解
    UILABEL AUTOLAYOUT自动换行 版本区别
    iOS自动布局解决警告Automatic Preferred Max Layout Width is not available on iOS versions prior to 8.0
  • 原文地址:https://www.cnblogs.com/summer520/p/3150752.html
Copyright © 2011-2022 走看看