JAVA freemarker (2)---the detailed steps of SPRINGMVC integration Freemarker in idea

Source: Internet
Author: User
Tags data structures exception handling
First step: Build.gradle configuration introduces jar packs,
Group ' Wyscha ' version ' 1.0-snapshot ' Apply plugin: ' java ' Apply plugin: ' War ' webappdirname = ' WebApp ' Sourcecompatibi Lity = 1.8 Repositories {mavencentral ()} sourcesets {main {java {srcdir ' activity/src/m
        Ain/java '} resources {srcdir ' core/src/main/resources '}} test { java {srcdir ' Core/src/test/java '}}} dependencies {//spring compile group: ' Org.springframework ', Name: ' Spring-core ', version: ' 4.2.8.RELEASE ' compile group: ' Org.springframework ', Name: ' Sprin G-aop ', version: ' 4.2.8.RELEASE ' compile group: ' Org.springframework ', Name: ' Spring-aspects ', version: ' 4.2.8.RELEASE ' Compile group: ' Org.springframework ', Name: ' Spring-context-support ', version: ' 4.2.8.RELEASE ' compile group: ' O Rg.springframework ', Name: ' Spring-context ', version: ' 4.2.8.RELEASE ' compile group: ' Org.springframework ', Name: ' SPR Ing-beans ', Version: ' 4.2.8.RELEASE ' compile group: ' Org.springframework ', Name: ' Spring-tx ', version: ' 4.2.8.RELEASE ' compile group: ' org . Springframework ', Name: ' Spring-test ', version: ' 4.2.8.RELEASE ' compile group: ' Org.springframework ', Name: ' Spring-o RM ', version: ' 4.2.8.RELEASE ' compile group: ' Org.springframework ', Name: ' SPRING-OXM ', version: ' 4.2.8.RELEASE ' C Ompile group: ' Org.springframework ', Name: ' Spring-web ', version: ' 4.2.8.RELEASE ' compile group: ' Org.springframework ' , name: ' Spring-webmvc ', version: ' 4.2.8.RELEASE ' compile group: ' Org.springframework ', Name: ' Spring-jdbc ', version:
    ' 4.2.8.RELEASE ' compile group: ' Org.springframework ', Name: ' Spring-expression ', version: ' 4.2.8.RELEASE '//scheduling Compile group: ' Org.quartz-scheduler ', Name: ' Quartz ', version: ' 2.2.1 '//Pinyin compile group: ' Com.belerweb ', name : ' pinyin4j ', version: ' 2.5.0 '//excel compile group: ' Org.apache.poi ', Name: ' Poi ', Version: ' 3.9 ' compile GR OUP: ' Org.apache.poi ', nAme: ' Poi-ooxml ', version: ' 3.9 ' compile group: ' Org.apache.xmlbeans ', Name: ' XMLBeans ', version: ' 2.6.0 '//acti  Viti Compile group: ' Org.activiti ', Name: ' Activiti-engine ', version: ' 6.0.0 ' compile group: ' Org.activiti ', name:
    ' Activiti-spring ', version: ' 6.0.0 '//mybatis compile group: ' Org.mybatis ', Name: ' MyBatis ', version: ' 3.3.1 ' Compile group: ' Org.mybatis ', Name: ' Mybatis-spring ', version: ' 1.2.5 ' compile group: ' Org.mybatis.generator ', name: ' Mybatis-generator-core ', version: ' 1.3.5 '//Database compile group: ' MySQL ', Name: ' Mysql-connector-java ', version: ' 
    5.1.40 ' Compile files (' Webapp/web-inf/lib/sqljdbc41.jar ') compile files (' Webapp/web-inf/lib/bcprov-jdk15-140.jar ')
    Compile files (' Webapp/web-inf/lib/ezmorph-1.0.6.jar ') compile files (' Webapp/web-inf/lib/json-lib-2.3-jdk13.jar ') Compile group: ' COMMONS-DBCP ', Name: ' COMMONS-DBCP ', version: ' 1.4 ' compile group: ' Com.alibaba ', Name: ' Druid ', V Ersion: ' 1.1.0 '//PublicTotal compile group: ' Commons-lang ', Name: ' Commons-lang ', version: ' 2.5 ' compile group: ' Commons-beanutils ', name: ' C Ommons-beanutils ', version: ' 1.9.2 ' compile group: ' Commons-configuration ', Name: ' Commons-configuration ', version: ' 1 Compile group: ' Commons-fileupload ', Name: ' Commons-fileupload ', version: ' 1.3.1 ' compile group: ' Commons-io ' , name: ' Commons-io ', Version: ' 2.4 ' compile group: ' Org.freemarker ', Name: ' Freemarker ', version: ' 2.3.20 '//htt Pclient compile group: ' Org.apache.httpcomponents ', Name: ' HttpClient ', version: ' 4.5.2 '//json compile Group  : ' Com.alibaba ', Name: ' Fastjson ', version: ' 1.2.24 '//compile ' net.sf.json-lib:json-lib:2.4:jdk15 ' compile group: ' Com.google.code.gson ', Name: ' Gson ', version: ' 2.8.0 ' compile group: ' Com.fasterxml.jackson.core ', Name: ' Jackson-da Tabind ', version: ' 2.8.8 ' compile group: ' Com.fasterxml.jackson.core ', Name: ' Jackson-core ', version: ' 2.8.8 ' Comp Ile Group: ' Com.fasterxmL.jackson.core ', Name: ' Jackson-annotations ', version: ' 2.8.8 '//log compile ' org.slf4j:slf4j-api:1.7.14 ' comp Ile ' ch.qos.logback:logback-core:1.1.3 ' compile ' ch.qos.logback:logback-classic:1.1.3 ' compile group: ' Org.logback -extensions ', Name: ' Logback-ext-spring ', version: ' 0.1.4 '//Container label compile group: ' Javax ', Name: ' Javaee-api ', ver Sion: ' 7.0 ' Runtime ("jstl:jstl:1.2") Compile group: ' Taglibs ', Name: ' Standard ', version: ' 1.1.2 ' Testcompil E group: ' JUnit ', Name: ' JUnit ', Version: ' 4.12 '}/* idea tool to enable this configuration/apply plugin: ' idea ' idea {module {i Nheritoutputdirs = False OutputDir = file ("webapp/web-inf/classes/")}}/* Apply plugin: ' ECLIPSE-WTP ' Eclip
    se{classpath{defaultoutputdir = file (' webapp/web-inf/classes/')}} */Task Copyj2eewebjars (type:copy) { From Configurations.runtime into ' webapp/web-inf/lib ' exclude ' **/*sqljdbc41* ', ' **/*json-lib-2.3-jdk13* ', ' **/ *ezmorph-1.0.6* ', ' **/*BCProv-jdk15-140* '} 
Step Two: Create a new Freemarker file freemarker.properties in src/main/resources/conf,
Template_update_delay=0
default_encoding=utf-8
output_encoding=utf-8
number_format=0.##########
classic_compatible=true
template_exception_handler=ignore
locale=zh_cn
date_format=yyyy-mm-dd
time_format=hh:mm:ss
datetime_format=yyyy-mm-dd HH:mm:ss
Step Three: Add Freemarker configuration to Springmvc.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:p= "http://www.springframework.org/schema/p" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" Xmlns:contex t= "Http://www.springframework.org/schema/context" xmlns:mvc= "Http://www.springframework.org/schema/mvc" 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 Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc.xsd "> <mvc:annotation-driven/> <context:component-scan base-package=" Com.cloud.wysch "A" > <context:exclude-filter type= "annotation" expression= "Org.springframework.stereotype.Service"/> & Lt;/context:component-scan> <!--set Freemarker configuration filePath--> <bean id= "freemarkerconfiguration" class= " Org.springframework.beans.factory.config.PropertiesFactoryBean "> <property name=" Location "value=" Classpath : Conf/freemarker.properties "/> </bean> <!--Configure Freemarker template path--> <bean id=" Fmxmlescape "cl ass= "Freemarker.template.utility.XmlEscape"/> <bean id= "Freemarkerconfig" class=. Web.servlet.view.freemarker.FreeMarkerConfigurer "> <property name=" freemarkersettings "ref=" Freemarkerconfi Guration "/> <property name=" Templateloaderpath "value="/web-inf/ftl/"/> <property name=" free Markervariables "> <map> <!--set some common global variables--> <entry key=" Xml_ Escape "value-ref=" Fmxmlescape "/> </map> </property> </bean> <!--configuration Freemarker View parser--> <bean id= "VIEWRESOLVERFTL" class=Ervlet.view.freemarker.FreeMarkerViewResolver "> <property name=" contentType "value=" text/html; Charset=utf-8 "/> <property name=" Cache "value=" true "/> <property" name= "suffix". value= " /> <property name= "exposerequestattributes" value= "true"/> <property "name=" Butes "value=" true "/> <property name=" Exposespringmacrohelpers "value=" true "/> <property nam E= "Requestcontextattribute" value= "request"/> <property name= "order" value= "0"/><!--This familiar setting represents the case in multiple views , the order of precedence matches.
        --> </bean> <bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/views/" ></property> <property name= "suffix" value= ". JSP" >&lt ;/property> <property name= "Viewclass" value= "Org.springframework.web.servlet.view.InternalResourceView"/&G
        T <property NamE= "Order" value= "1"/> </bean> <bean id= multipartresolver "class=" Org.springframework.web.multipart. Commons. Commonsmultipartresolver "> <property name=" maxuploadsize "value=" 104857600 "/>" <property name= "Maxinmemorysize" value= "4096"/> <property name= "defaultencoding" value= "UTF-8" ></property> &L t;/bean> <bean class= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" > <!
        --Defines the default exception handling page that uses--> <property name= "Defaulterrorview" value= "error" When registering for the exception type ></property> <!--define the name of the variable that the exception-handling page uses to get exception information, the default name is exception--> <property name= "Exceptionattribute" value= "Exception" >&lt ;/property> <!--define exceptions that require special handling, with the class name or full pathname as the key, and the exception page name as the value--> <property name= "Exceptionmappings"
            ; <props> <prop key= "java.lang.Exception" >error</prop> <prop key= "java. LaNg. Throwable ">error</prop> </props> </property> </bean> </beans>
Step Fourth: Write controller and FTL files

Create a new package under the Src/main/java directory Com.cloud.wyscha.base.controller, and under the package, create a new Freemarkcontroller.java, as follows:

@Controller
@RequestMapping ("/freemark") public
class Freemarkcontroller extends Basecontroller {

    @ Requestmapping ("/helloworld.action") public
    Modelandview HelloWorld (httpservletrequest request, HttpServletResponse response) {
        map<string, object> Map = new hashmap<string, object> ();
        Map.put ("Word0", "Hello");
        Map.put ("Word1", "world!");
        return new Modelandview ("HelloWorld", map);
    }

HELLOWORLD.FTL, as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Fifth step: Visit
Http://localhost/freemark/helloWorld.action

————————————————————————————————————————————————— – Java Architect Project Combat, high concurrency cluster distributed, large data high availability video tutorials, total 760G

Download Address:

https://item.taobao.com/item.htm?id=555888526201

01. Senior Architect 42 Stage high
02.Java Advanced System Training Architecture Course 148 hours
03.Java Advanced Internet Architect Course
04.Java Internet Architecture Netty, Nio, Mina, etc.-Video tutorials
05. Java Advanced Architecture Design 2016 finishing-video Tutorials
06. Architect Basics, advanced film
07.Java architect required Linux Operation series Course
08.Java Advanced System training Program 116 hours
+
Hadoop series tutorials, Java design patterns and data structures, Spring Cloud micro services, Springboot Getting started

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.