Reputation: 3
I am new to spring boot and trying to load a jsp page but even after trying a lot and searching on internet I couldn't find any solution of it. I am unable to get the error causing part in this complete process.
To build this all I used command 'gradle build' on command prompt.
To start the server I used command :- java -jar build\libs\one-0.0.1-SNAPSHOT.war
then I on browser I typed :- http://localhost:8080/helloWorld
Here is my build.gradle
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
id 'war'
}
group = 'com.tm'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
providedRuntime 'javax.servlet:jstl'
providedRuntime 'org.apache.tomcat.embed:tomcat-embed-jasper'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
This is my java code of controller HelloJSPController.java, it is in this folder structure: \src\main\java\com\tm\one
package com.tm.one;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloJSPController
{
@GetMapping("/helloWorld")
public String helloWorld()
{
return "helloWorld";
}
}
This is WebMvcConfig.java configuration file
package com.tm.one.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter
{
@Bean
public InternalResourceViewResolver viewResolver()
{
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
}
OR I tried this two lines in application.propertes also instead of above configuration file
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
This is JSP file helloWorld.jsp in folder structure :-- \one\src\main\webapp\WEB-INF\jsp
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<h2>Hello everyone I am AD</h2>
</body>
</html>
This is error I am getting on browser
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Jan 13 16:53:14 IST 2020
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template [helloWorld], template might not exist or might not be accessible by any of the configured Template Resolvers
This is OneApplication.java
package com.tm.one;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class OneApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(OneApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(OneApplication.class);
}
}
Upvotes: 0
Views: 865
Reputation: 1
Had the same issue as you, and I believe it's not because of the code. The only solution I found was switching to maven
. Absolutely no other change in the java code, only build.gradle
-> pom.xml
.
Upvotes: 0