Spring-boot notes-static Resources and page development (vi)

Source: Internet
Author: User
Static Resources 1.1: Default Method

Loading static resources in Springboot is not the same as in normal Web applications. Static Resources (JS, CSS, pictures, and other resources) the default directory location needs to be placed under Classpath, and the following directory rules are met:/static/public/resources/meta-inf/resources

Let's take a look at a directory structure first:

We create a new directory static in the resources directory, below which has an image directory, the static is consistent with what we said above the Springboot default static resource directory, so if we visit this picture, we can pass the http:// Localhost:8080/image/timg.jpg. In other words, the above directories are all static resource mapping paths, priority order: meta-inf/resources > Resources > Static > public
Recommendation: Use the default configuration method for spring boot directly . 1.2 Modify the default mode of configuration file

# The default value is/**
spring.mvc.static-path-pattern=
# The default value is classpath:/meta-inf/resources/,classpath:/resources/, classpath:/static/,classpath:/public/
spring.resources.static-locations= Here sets the path to point to, with multiple comma-separated

such as I put spring.mvc.static-path-pattern=/demo/**, then the above our picture will need to pass http://localhost:8080/demo/image/ Timg.jpg can access to, if I changed the following locations, then my picture is still static can not access to the.
2. Webmvcconfigureradapter
overrides its Addresourcehandlers method by writing a class to inherit webmvcconfigureradapter, and can increase the configuration of some resource paths:

Import org.springframework.context.annotation.Configuration;
Import Org.springframework.util.ResourceUtils;
Import Org.springframework.web.servlet.DispatcherServlet;
Import ORG.SPRINGFRAMEWORK.WEB.SERVLET.CONFIG.ANNOTATION.ENABLEWEBMVC;
Import Org.springframework.web.servlet.config.annotation.InterceptorRegistry;
Import Org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

Import Org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 /** * Created by Gonghao on 2017/6/3. //@EnableWebMvc: If you add this annotation, you are fully control MVC and use @Configuration public class Mywebappconfigurer extends carefully webmvcconfigureradapter{@Override public void Addresourcehandlers (Resourcehandlerregistry registry) {Reg Istry.addresourcehandler ("/myresource/**"). Addresourcelocations (resourceutils.classpath_url_prefix+ "/
        myresource/");
        Registry.addresourcehandler ("/static/**"). Addresourcelocations (resourceutils.classpath_url_prefix+ "/static/"); Super.addresourcEhandlers (registry); }
}

We don't use @enablewebmvc annotations here, so we're adding some resource path configuration instead of completely modifying the default resource path configuration. That means I have added this configuration: I can access the picture through Http://localhost:8080/static/image/timg.jpg and http://localhost:8080/image/timg.jpg. Page Development

The dynamic template engine supported by Springboot includes the following types: Thymeleaf freemarker Velocity Groovy Mustache

Springboot support for JSP is not very good, you should avoid using JSP.
If you use several of these template engines, the default template configuration path is: src/main/resources/templates, templates don't spell mistakes. in the screenshot above, we've created a new index.html under the templates. Let's take a look at how to access the page.
We are using the thymeleaf, remember to introduce the Pom file.

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId> Spring-boot-starter-thymeleaf</artifactid>
        </dependency>

And look at the contents of the index.html:

<! DOCTYPE html>

You may find it strange to see the wording above, TH:SRC and @{} What the hell is this? In fact, this is the grammar of Thymeleaf. @{} is used to introduce external resources. "Students who do not understand themselves go to study alone."
And look at our controller code:

Import Org.springframework.stereotype.Controller;
Import Org.springframework.ui.ModelMap;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.servlet.ModelAndView;

/**
 * Created by Gonghao on 2017/6/15.
 */
@Controller public
class Indexcontroller {
    @RequestMapping ("/index") public
    String Index ( Modelmap map) {
        Map.addattribute ("Hello2", "Hello2 thymeleaf!");
        Map.addattribute ("Hello", "Hello thymeleaf!");
        Return "index";
    }

Note that this is no longer a @restcontroller, but a @controller. This is exactly the same as our springmvc, so we can access our page through Http://localhost:8080/index. Because Springboot integrates with thymeleaf, it will default to find files in the directory under the templates under resources. Here we have completed a simple page development.

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.