2

Updated: to describe the question more clearly

I create a web applicaiton with spring boot and thymeleaf, everything works fine if I open the login page, then login, then head for the management module or reports module sequently.

The proleam occurs when I type the url locahost:8080/send/kf/index(needs to authenticate, but I have open access to all in customized filter) in the browser, the page loads without js and css. In debug mode, I saw /send/kf was unexpectly put into the path like the following. I can get the resource if I access localhost:8080/assets/avatars/avatar.png.

enter image description here

The following is the folder structure I put my static resources. How could /send/kf automatically added into the url, please help me solve this problem. Thanks! enter image description here

7
  • does using /assets/ instead of assets/ fix your issue? Commented Sep 25, 2017 at 9:05
  • No, the right path should be /assets/css/..., but /send/kf is automatically put into the url. Commented Sep 25, 2017 at 9:46
  • do you use spring-security? If yes this could help. resource 1, resource 2 Commented Sep 25, 2017 at 11:50
  • @DavePateral ... and thats what using /assets/ should fix, right? so just add forward slash in the beginning of your url Commented Sep 25, 2017 at 12:05
  • @Patrick Yse, I use spring security, thanks for your guides, but the problem is not about that. The resources can be loaded if I open the login page, but can't be loaded when I type the url(/send/kf/index) in the browser, wihch needs to load these resources, but /send/kf is unexpectly put into the path Commented Sep 26, 2017 at 2:26

3 Answers 3

1

you can use spring.resources.static-locations in your application.properties file

spring.resources.static-locations=classpath:/resources/css/

spring.mvc.static-path-pattern=/resources/**

this is taken form documentation

Note:Static resources, like JavaScript or CSS, can easily be served from your Spring Boot application just be dropping them into the right place in the source code. By default Spring Boot serves static content from resources in the classpath at "/static" (or "/public")

Sign up to request clarification or add additional context in comments.

3 Comments

This is to relocate the address of static resources, right? My js and css is under resouces/static/assets, it can be loaded if I log into the site, but fails to load when directly hitting an inner url.
do you mean that you access this page through a hyperlink ?
Not a hyperlink, just type url in the browser.
0

Using /assets/ instead of assets/ fixes the issue, as otherwise it's a relative url that's supposed to get added to the end of existing url.

Comments

0

I find a solution for this question. I don't know how /send/kf is put into the url for the static resources, but I think if I put the controller mapping under the root path, this problem should avoid. As I think, the url is correct and resources can load successfully.

@Controller
public class ManualMessageController {

    @Autowired
    private MsgTemplateRepository msgTemplateRepository;

    @RequestMapping("/manualMsg")
    public String manualMsg(Model model){
        model.addAttribute("msgTemplateList", msgTemplateRepository.findByStatus(1));
        return "manualMessage";
    }
}

updated:

The right solution is to use absolute path rather than relative path in the project. So change assets/css to /assets/css works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.