springmvc配置freemarker

首先目录结构如图
springmvc配置freemarker_第1张图片
1.POM文件中添加包依赖
注意spring-context-support的版本号与系统spring版本号一致

<dependency>
  <groupId>org.freemarkergroupId>
  <artifactId>freemarkerartifactId>
  <version>2.4.0version>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-context-supportartifactId>
  <version>4.1.2.RELEASEversion>
dependency>

2.application-context.xml 添加视图解析器

  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
            http://www.springframework.org/schema/beans/spring-beans.xsd  
            http://www.springframework.org/schema/mvc  
            http://www.springframework.org/schema/mvc/spring-mvc.xsd  
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context.xsd"  
    default-lazy-init="true">  
      
    <mvc:annotation-driven />  
      
    <context:component-scan base-package="com.my.demo" />  

     <bean id="freemarkerConfig"  class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
        <property name="templateLoaderPath" value="WEB-INF/" />  
        <property name="defaultEncoding" value="UTF-8" />  
     bean>  
       
     <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
        <property name="prefix" value="/">property>    
        <property name="suffix" value=".html" />  
        <property name="contentType" value="text/html;charset=UTF-8" />  
        <property name="requestContextAttribute" value="rc" />  
     bean> 

beans>  

注意web.xml里的配置如下


<web-app 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_3_0.xsd"
    xmlns:web="http://java.sun.com/xml/ns/javaee" id="WebApp_ID" version="3.0"
    metadata-complete="true">

    <display-name>displaynamedisplay-name>

    <servlet>
        <servlet-name>demoservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
         <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>/WEB-INF/classes/conf/application-context.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>demoservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>


web-app>

3.可以添加ftl模板文件 spring.ftl ,global.ftl是自定义指令

4.创建cotroller

package com.my.demo.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorldController {

    @RequestMapping("hello")
    public String loginFunction(HttpServletRequest request, Model model){
        System.out.println("controller start");
        model.addAttribute("book","234");
        System.out.println("controller end");
        return "hello";
    }

}

5.创建hello.html


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>hellotitle>
head>
<body>
hello ${book!}
body>
html>

访问localhost:8080/项目名/hello 就ok啦。

你可能感兴趣的:(java)