Spring boot cannot resolve view page issue in mustache - spring

package springboot.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
#Controller
public class IndexController {
#GetMapping("/")
public String index() {
return "index";
}
}
index.html
<!DOCTYPE HTML>
<html>
<head>
<title>start</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>start</h1>
</body>
</html>
build.gradle
buildscript {
ext {
springBootVersion = '2.1.9.RELEASE'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group 'com.jojoldu.book'
version '1.0.4-SNAPSHOT-'+new Date().format("yyyyMMddHHmmss")
sourceCompatibility = 11
repositories {
mavenCentral()
jcenter()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.projectlombok:lombok')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-mustache')
compile('com.h2database:h2')
compile('org.springframework.boot:spring-boot-starter-oauth2-client')
compile('org.springframework.session:spring-session-jdbc')
compile("org.mariadb.jdbc:mariadb-java-client")
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile("org.springframework.security:spring-security-test")
}
I am using intellij and spring boot and use mustache to use templates. I installed mustache plugin and changed to .mustache instead of .html, but a can not resolve Mvc view error occurs in the controller and a 404 error occurs when I run it. What is the cause? ?
I put the same html file in the static folder and tried to /index.html in the view resolver but couldn't find it.

Related

How to map JSP file in Spring boot?

This is my first spring boot application. I want to load home.jsp file in 8080 port.
When I run port 8080/home I can see home.jsp file is download. How I run that home.jsp
Bellow you can see my HomeController.Java file
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller
public class HomeController {
#RequestMapping("home")
public String Home()
{
System.out.print("Hello");
return "home.jsp";
}
}
Bellow you can see my home.jsp file
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
I love Java
</body>
</html>
Bellow you can see my gradle.build file
plugins {
id 'org.springframework.boot' version '2.6.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
classpath('se.transmode.gradle:gradle-docker:1.2')
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.apache.tomcat:tomcat-jasper:9.0.55'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
Here I added tomcat dependency
application.yml
server.port=8080
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
try adding the followings in gradle then update gradle
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
runtime('com.h2database:h2')
runtime('org.springframework.boot:spring-boot-devtools')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
ohh and add
'/' in front of the requestmapping
#Controller
public class HomeController {
#RequestMapping("/home")
public String Home()
{
System.out.print("Hello");
return "home.jsp";
}
}

Why cant i import #RestController spring boot

enter image description herei'm just trying to set up a simple spring boot application that has rest controller. But cant import Rest Controller. Here is my main method
package com.test.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
#RestController
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
And here is my build.gradle. the script collects all the jars on the classpath and builds a single jar.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.2.1.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
bootJar {
baseName = 'gs-spring-boot-docker'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-web'
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
Any Ideas? Thanks
You are missing import for #RequestMapping("/").
import org.springframework.web.bind.annotation.RequestMapping;
I invalidated and restarted the caches which worked!

Gradle for this Spring Boot project + jsp

Im using Gradle for this Spring boot project and my task is to create another jsp file , for example : index.jsp and do something that Spring boot can generate that index.jsp
My problem is when i create index.jsp in webapp -> WEB_INF -> index.jsp
it only return the message 'index' instead of what is in file index.
Application.java
package edu.msudenver.tsp.website;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
PledgeController.java
package edu.msudenver.tsp.website.controllers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class PledgeController {
#GetMapping("/hello")
public String getHelloMessage() {
return "index";
}
}
Application.properties
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
build.gradle
buildscript {
ext {
springBootVersion = '1.5.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
index.jsp
> <%# page contentType="text/html;charset=UTF-8" language="java" %>
> <html> <head>
> <title>Hello Spring mvc</title> </head> <body>
add dependencies for JSP
compile('javax.servlet:jstl')
compile("org.apache.tomcat.embed:tomcat-embed-jasper")
and index.jsp PATH is webapp/WEB_INF/jsp/index.jsp.
if you want example, https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp.
You have to use #Controller annotation instead of #RestController in your class PledgeController.
#Controller
public class PledgeController {
#GetMapping("/hello")
public String getHelloMessage() {
return "index";
}
}
I had the same issue. It's not about the java code. The only solution I found was switching to maven. The only change was build.gradle -> pom.xml, and nothing else in the controller or jsp file, what you have there is correct.

Spring Boot 1.5.9 context-path HEAD response not working

I have problem with setting up HEAD resposnse from RestController in Spring Boor. I'm currently using Spring Boot version 1.5.9.
I have settup new project for demostrating this problem. In application.properties I have just one line:
server.context-path=/api/v1
My RestController "TestController.java"
#RestController
public class TestController {
#GetMapping("/test")
public String test() {
return "test";
}
}
When I try to get only header with HTTP HEAD request on url http://localhost:8080/api/v1/test, then the response stack and I don't get any response. In app console there is no errors.
If I remove server.context-path=/api/v1 from application.properties. Then HEAD request to http://localhost:8080/test is working as expected.
Thank you for any help,
Martin
Here is the build.gradle
buildscript {
ext {
springBootVersion = '1.5.9.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-websocket')
// compile('com.microsoft.sqlserver:sqljdbc4')
runtime('org.springframework.boot:spring-boot-devtools')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

Thymeleaf views not found with springboot

I am trying to follow this tutorial for adding thymeleaf to a springboot app but I can't seem to get it to work.
Tutorial: http://spr.com/part-2-adding-views-using-thymeleaf-and-jsp-if-you-want/
I was able to get springboot to work fine when I started the app using #RestController in LoginController but when I changed #RestController to #Controller I'm getting an error page saying:
There was an unexpected error (type=Not Found, status=404).
No message available
I set a breakpoint in the controller and confirmed that it is hitting the index method in LoginController. I feel like this has to do with how I've added Thymeleaf since I haven't done much else to the application but everything I've tried so far results in the same error page.
my build.gradle
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-snapshot" }
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'GazeFest'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.thymeleaf:thymeleaf-spring4:3.0.0.RELEASE")
}
task wrapper(type: Wrapper) {
gradleVersion = '3.0'
}
my Application.java
package gazefest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
my LoginController.java
package gazefest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#Controller
public class LoginController {
#RequestMapping("/")
public String index(Model model) {
model.addAttribute("message", "HELLO!");
return "index";
}
}
my index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<meta charset="UTF-8" />
<title>HELLO</title>
</head>
<body>
<p th:text="${message}"></p>
</body>
</html>
my file structure
Thanks for taking a look!
I don't think you should be using the thymeleaf-spring4 dependency, but you should be using the Spring boot starter for Thymeleaf.
For Maven:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
For Gradle:
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
I suggest using the Spring Initializr to set up your project. This allows you to select any Spring boot starter and add it to your Gradle/Maven descriptor so you won't make any mistakes by picking a dependency.

Resources