Springmvc-spring-hibernate Project Building Three--freemarker & Static resource integration

Source: Internet
Author: User

I. Previous directory structure the following schema is as follows:

  

Two. freemarker file configuration

Specify the location of the spring configuration file in the Web. xml file

  

Three. Configuring the Springmvc-servlet.xml File

1) Configure automatic Scan package--can read to @controller related Java package;

2) Default annotation map Support-read static files;

3) Set the Freemarker configuration file;

4) Set the access to the static resource file;

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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"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.xs D HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC http://www.springframework.org/schema/mvc/spring-mvc.xsd "
    >         <!--Configure automatic scanning of packages -    <Context:component-scanBase-package= "Com.ajs"></Context:component-scan>    <!--support for the default annotation mappings -         <Mvc:annotation-driven/>          <!--configuration file for Freemarker -    <BeanID= "Freemarkerconfig"class= "Org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">          < Propertyname= "Templateloaderpath"value= "/web-inf/views"/>    </Bean>    <!--freemarker Configuration View resolver -    <BeanID= "Viewresolver"class= "Org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">          < Propertyname= "Cache"value= "true"/>          < Propertyname= "prefix"value=""/>          < Propertyname= "suffix"value= ". FTL"/>    </Bean>        <!--access to static resource files (the role of Cache-period is not known for the time being) -    <mvc:resourcesMapping= "/images/**" Location= "/meta-inf/resources/images/"Cache-period= "31556926" />    <mvc:resourcesMapping= "/js/**" Location= "/meta-inf/resources/js/"Cache-period= "31556926" />    <mvc:resourcesMapping= "/css/**" Location= "/meta-inf/resources/css/"Cache-period= "31556926" />    </Beans>

After the above configuration is complete, you can access static resources in the corresponding directory (CSS/JS/IMAGES/FTL, etc.) in the project.

Four. Configuring SPRING.FTL for reading static resources

Copy the corresponding SPRING.FTL file in the WEBMVC corresponding directory, and put it in our own project (you can also use the corresponding path without copy setting), as follows:

  

Five. Use static resources in the FTL file, as shown in the sample file below

<#import".. /SPRING.FTL "as Spring/> <!--find the relative path to the SPRING.FTL -<!DOCTYPE HTML>  <HTML>  <Head>      <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">      <title>Success</title>         <Linkrel= "stylesheet"type= "Text/css"href= "< @spring. url '/css/default.css '/>"/>    <Scripttype= "Text/javascript"src= "< @spring. url '/js/jquery-1.12.3.min.js '/>"></Script></Head>  <Bodyclass= "BG">      <h4>Success Page</h4>    <ahref=".. /index ">Index</a>    <imgsrc= "< @spring. url '/images/pig.png '/>"/>    <ButtonID= "CHGBG">Chgbg</Button></Body>  <Scripttype= "Text/javascript">$ (document). Ready (function () {$ ("#chgBg"). Click (function () {$ (' body '). CSS ("Background-color", "Yello      W ");    }); });</Script></HTML>  

At this time Freemarker & static resource integration is completed;

Springmvc-spring-hibernate Project Building Three--freemarker & Static resource integration

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.