I am learning Spring Framework. I added home.html in resources/templates/home.html. But it is not visible when I visit http://localhost:8080. I have the following structure:
taco-cloud\src\main\java\tacos\TacoCloudApplication.java
package tacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class TacoCloudApplication {
public static void main(String[] args) {
SpringApplication.run(TacoCloudApplication.class, args);
}
}
taco-cloud\src\main\java\tacos\HomeController.java
package tacos;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
#Controller
public class HomeController
{
#GetMapping("/")
public String home()
{
return "home.html";
}
}
taco-cloud\src\main\resources\static\home.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Taco Cloud</title>
</head>
<body>
<h1>Welcome to...</h1>
<img th:src="#{/images/TacoCloud.png}"/>
</body>
</html>
Output
Whitelable error page
localhost:8080/home.html
show home.html
You have to change the location of your home page to be in the static folder :
resources/static/home.html
^^^^^^
instead of
resources/templates/home.html
and specify the extension in your controller :
return "home.html";
^^^^^
else you have to create a view resolver to avoid using extensions and to specify the other locations of your pages take a look at Configure ViewResolver with Spring Boot and annotations gives No mapping found for HTTP request with URI error
You can try this.
#GetMapping("/")
public String home()
{
return "templates/home.html";
}
Check more details
Below are few different ways you can place html files in your project.(Note it is from highest to lowest precedence)
src/main/resources/resources/home.html
src/main/resources/static/home.html
src/main/resources/public/home.html
Go through this to get an idea about spring mvc project structure
Related
This is my pom.xml file
pom.xml enter image description here
Application.properties File
#spring.thymeleaf.enabled=true
#spring.thymeleaf.prefix=/templates/
#spring.thymeleaf.suffix=.jsp
spring.mvc.view.prefix=/templates/
spring.mvc.view.suffix=.jsp
Home.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h1>Registration</h1>
</body>
</html>
Controller
#RestController
public class LoginController {
#GetMapping(path="/home")
public String loginPage() {
System.out.println("hello");
return "home";
}
}
Application File
#SpringBootApplication
public class SpringJspDemoApplication {
public static void main(String[] args)
{
SpringApplication.run(SpringJspDemoApplication.class, args);
}
}
Output:
enter image description here this is the output I am getting a string value instead of jsp
Tried using Thymeleaf, tried to change #RestController-> #Controller, used #ComponentScan etc but issue not getting resolved.
Tried going through the solutions given on site but none of them are working for me, also they are quite older.
Project Structure :
enter image description here tried to put home.jsp at /webapp/WEB-INF/jsp/ that is also not working.
what will be the possible solution on this, can anyone please guide on this?
Below is the code when I change #Controller to #RestController
[![
Controller File
Blockquote
]1]1
And result is still
I have checked the status under network tab which is 200 only
Please Add #ComponentScan(basePackages={"com.web"}) in your main application file
Replace #RestController to #Controller like below
#Controller
public class LoginController {
#GetMapping(path="/home")
public String loginPage() {
System.out.println("hello");
return "home";
}
}
I am learning spring boot. I created this super simple project, but I keep getting a 404 Whitelabel error page when I try to return an HTML page with the #GetMapping annotation.
Here is my only controller:
package com.example.springplay;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
#Controller
public class MainController {
#GetMapping(value = "/")
public String hello(){
return "hello";
}
}
Here is the spring application:
package com.example.springplay;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class SpringplayApplication {
public static void main(String[] args) {
SpringApplication.run(SpringplayApplication.class, args);
}
}
Here is the directory
Here is the hello.html page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello world</h1>
</body>
</html>
The hello.html page is in the resources/templates folder, I don't know what's going on.
I copied this part which has the exact same structure from another working project, yet mine just gives me this Whitelabel error page.
Move hello.html to static folder in resource and change controller like this:
#GetMapping(value = "/")
public String hello(){
return "hello.html";
}
or like this:
#GetMapping(value = "/")
public ModelAndView hello(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("hello.html");
return modelAndView;
}
Remplace here
#GetMapping(value = "/")
public String hello(){
return "hello";
}
or like this
#RequestMapping(path = "/", produces = MediaType.TEXT_HTML_VALUE)
public String welcomepage() {
return "hello";
}
How do you reference a Java class using Thymeleaf? I'm trying to display the result of my Java class on a website. When I use Spring Boot to display the output of the Java class it works, and when I use Spring Boot to display a Thymeleaf HTML template it also works, but I'm stuck now on how to call the java class from within the Thymeleaf HTML code.
Spring Boot Application File
package net.javavatutorial.tutorials;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class SpringBootExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootExampleApplication.class, args);
}
}
Controller File
package net.javavatutorial.tutorials;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
#Controller
#RequestMapping("/")
class MainController
{
#RequestMapping(method = RequestMethod.GET)
ModelAndView
index()
{
ModelAndView mav = new ModelAndView("index");
mav.addObject("version", "0.1");
return mav;
}
}
Filewalk.java that takes in a directory path and outputs the files contained in that directory
package net.javavatutorial.tutorials;
import java.io.File;
public class Filewalk {
public static String filewalk(String s) {
String result = "";
// creates a file object
File file = new File(s);
// returns an array of all files
String[] fileList = file.list();
for (String str : fileList) {
result += str + "\n";
}
return result;
}
}
Index.HTML file where I want to display the Filewalk.java output String
<!DOCTYPE html>
<html>
<head>
<meta charset = "ISO-8859-1" />
<link href = "css/styles.css" rel = "stylesheet"/>
<title>Spring Boot Application</title>
</head>
<body>
<h4>Thymeleaf Spring Boot web application</h4>
</body>
</html>
The way you do this should be something like this:
Controller:
#RequestMapping(method = RequestMethod.GET)
public ModelAndView index() {
ModelAndView mav = new ModelAndView("index");
mav.addObject("version", "0.1");
mav.addObject("filewalk_results", Filewalk.filewalk("directory"));
return mav;
}
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset = "ISO-8859-1" />
<link href = "css/styles.css" rel = "stylesheet"/>
<title>Spring Boot Application</title>
</head>
<body>
<h4>Filewalk results:</h4>
<pre th:text="${filewalk_results}" />
</body>
</html>
You do all the actual calculation and create objects/information to put on your model, then output it in the HTML.
I am trying to build an application with spring boot and java in eclipse.
What I have tried so far:
HomeController.class
#Controller
#ComponentScan(basePackages = {"project.tool.frontend"})
public class HomeController {
#GetMapping("/home")
public String home(){
System.out.println("open page");
return "home";
}
}
home.html
<!DOCTYPE HTML>
<html>
<head>
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form action="/home" method="get">
First name: <input type="text" name="fname"><br> Last
name: <input type="text" name="lname"><br> <input
type="submit" value="Submit">
</form>
</body>
</html>
Application.class
#SpringBootApplication
public class Application{
public static void main(String agrs[])
{
SpringApplication.run(Application.class);
}
}
When I run this Application I get the "Whitelabel ErrorPage" Error. I also enabled the whitelabel errorpage in my application.properties .
And a question for unserstandig: When I use #GetMapping("/home") in my HomeController and the <form action="/home" in my html file it will be mapped through the "/home" get method right?
Thank you all.
As a static resource, your home.html should be at resources/public/home.html (or /static/). Is it there?
Your Application should tell in which package your controller is located, so it gets picked up
Does adding a ComponentScan help?
#SpringBootApplication
#ComponentScan(basePackages = { "com.acme.controllers" })
public class Application{
}
If you have not configured any view resolver, try returning "/views/home.html" instead of just "home". Change "/views/home.html" to whatever be the path of the file.
I suggest using Thymeleaf as your viewresolver.
You can place the home.html in src/main/resources folder to be recognized.
As you are using .html extension then I suggest you to use the thymeleaf as a view resolver.
Using thymeleaf HTML:
<form th:action="#{/home}" th:method="get"
...
...
//Rest of your code goes here.
</form>
Use #ComponentScan annotation:
#SpringBootApplication
#ComponentScan(basePackages = { "<full package name where controller exists>" })
public class Application{
public static void main(String agrs[]){
SpringApplication.run(Application.class);
}
}
I'm actually learning Spring Boot. I follow the guides and i'm tryng this one : Securing a Web Application.
So I've made 2 files (hello.html/home.html) and i would like to deploy my application to see them but when i try to access localhost:8080 i get this error :
Circular view path [home]: would dispatch back to the current handler URL [/home] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
If someone can help me there ? Here is my code :
src/main/java/com/society/MyApplication :
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class MyApplication {
public static void main(String[] args) throws Throwable {
SpringApplication.run(CalamarApplication.class, args);
}
}
src/main/java/com/society/MvcConfig.java
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
#Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/home").setViewName("home");
registry.addViewController("/").setViewName("home");
registry.addViewController("/hello").setViewName("hello");
registry.addViewController("/login").setViewName("login");
}
}
src/main/resources/templates/home.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>
Thanks by advance.