华域联盟 .Net spring mvc整合freemarker基于注解方式

spring mvc整合freemarker基于注解方式

基于网络改进为:最正常版本

复制代码 代码如下:

<?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">

    <!-- 针对freemarker的视图配置 -->

    <bean id="viewResolver"

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

        <property name="order" value="5" />

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

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

    </bean>

     

<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>

Controller建立

复制代码 代码如下:

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;

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

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

import org.springframework.web.servlet.ModelAndView;

@Controller

public class SpringMvcController {

    @RequestMapping(value="/welcome",method={RequestMethod.GET}) 

    public ModelAndView getFirstPage(HttpServletRequest request) {

        //welcom就是视图的名称(welcom.ftl)

        ModelAndView mv = new ModelAndView("welcom");

        mv.addObject("name", "My First Spring Mvc");

        return mv;

    }

}

在url上敲http://localhost:8080/welcome就会到WEB-INF/view/welcom.ftl页面渲染数据

welcom.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>

Hello ${name}

</body>

</html>

页面出来的效果:

Hello My First Spring Mvc

您可能感兴趣的文章:

  • 使用SpringBoot注解方式处理事务回滚实现
  • spring boot注解方式使用redis缓存操作示例
  • Spring AOP如何整合redis(注解方式)实现缓存统一管理详解
  • Spring 使用注解方式进行事务管理配置方式
  • Spring-MVC异步请求之Servlet异步处理
  • springmvc处理异步请求的示例
  • 详解spring mvc对异步请求的处理
  • SpringMVC环境下实现的Ajax异步请求JSON格式数据
  • Spring中注解方式的异步请求

本文由 华域联盟 原创撰写:华域联盟 » spring mvc整合freemarker基于注解方式

转载请保留出处和原文链接:https://www.cnhackhy.com/47827.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部