最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

spring mvc整合freemarker基于注解方式

来源:懂视网 责编:小采 时间:2020-11-27 22:41:35
文档

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
推荐度:
导读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

基于网络改进为:最正常版本
代码如下:
<?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

文档

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
推荐度:
标签: spring mvc 注解方式
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top