3

I have a simple webpage and want to add JS to it.

The controller rendering the page is -

@Controller
@RequestMapping("/testui")
public class GreetingController {

@RequestMapping("/greeting")
public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
    model.addAttribute("name", name);
    return "pages/greeting";
}

}

The struture of my resources folder are -

src/main/resources
|
 -- static
 --templates
   |
    --css
    --js
    --pages

The Thymeleaf page is -

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>

</head>

<body>
Welcome to the future
</body>
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
</html>

PS - I have also tried -

<script type="text/javascript" th:src="@{js/jquery-1.10.2.min.js/}"></script>

But same problem

2 Answers 2

4

The problems is the place where your css and js folders are, those should be inside of static folder.

src/main/resources
|
 -- static
   |
    --css
    --js
 --templates
   |
    --pages

And in your *.html page you can use:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <!-- include css -->
    <link rel="stylesheet" type="text/css" th:href="@{/css/style.css}"/>
</head>

<body>
Welcome to the future
</body>
    <!-- include javascript -->
    <script type="text/javascript" th:src="@{/js/jquery-1.10.2.min.js}"></script>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

Use below

<script type="text/javascript" src="../js/jquery-1.10.2.min.js"></script>

4 Comments

nope didnt work.. I think i need to register a path in application.properties or have to write a configuration java class registering the location in the path
i followed the tutorial and added a configuration class (memorynotfound.com/…)
Basicly all content that needs to be served staticly (such as javascript files) should be placed under the static folder. spring.io/blog/2013/12/19/…
and also add them using the configuration bean

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.