spingmvc與freemarker整合

來源:互聯網
上載者:User

標籤:內容   pad   springmvc   tab   org   support   控制   mave   package   

springmvc是配置在web工程中的,首先使用maven建立標準web工程,建立好的工程目錄如下

 

在pom.xml中添加一個依賴就可以

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-webmvc</artifactId>

    <version>3.2.17.RELEASE</version>

</dependency>

maven會自動下載spring-webmvc所有依賴的包

 

 

 

freemarker模板引擎

    添加依賴

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context-support</artifactId>

    <version>3.2.17.RELEASE</version>

</dependency>

<dependency>

    <groupId>org.freemarker</groupId>

    <artifactId>freemarker</artifactId>

    <version>2.3.23</version>

</dependency>

ncy>

 

 

在控制層建立一個測試類別

package com.llh.springmvc.controllers;

 

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

 

@Controller

 

public class TestController {

    @RequestMapping("/test")

    public String test(){

        System.out.println("springMVC is running");

        return "success";

    }

}

 

在src/main/resources目錄下建立一個檔案“springmvc-init.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.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context.xsd">

    <!--掃描所有的控制層  -->

    <context:component-scan base-package="com.llh.springmvc.controllers" />

    <!--配置freemarker視圖解析器 -->

    <bean id="freemarkerConfig"

        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">

        <property name="templateLoaderPath" value="/WEB-INF/page/" />

    </bean>

    <bean id="viewResolver"

        class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">

        <property name="suffix" value=".ftl" />

        <property name="contentType" value="text/html; charset=UTF-8" />

    </bean>

</beans>

 在WEB-INF目錄建一個page目錄,裡面放一個success.ftl檔案

<html>

<body>

    <h2>success page</h2>

</body>

</html>

 

 

啟動工程,瀏覽器訪問 http:localhost:8080/springmvc/test 就能看到返回 success page 值了,同時控制台列印出“springMVC is running”。

spingmvc與freemarker整合

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.