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);
}
}
Had the same issue as you, and I believe it's not due to the java code. The only solution I found was switching to maven. Absolutely no other change in the java code, only build.gradle -> pom.xml.
Related
I'm working on a team to create a Spring application. I recently pulled from Git and am working with the main repository. On my machine I get an error, but my teammates are able to run the application just fine.
Here's the error:
Parameter 0 of constructor in com.revature.controllers.FoodItemController required a bean named 'entityManagerFactory' that could not be found.
Snippet of my controller:
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.revature.daos.FoodItemDAO;
import com.revature.models.FoodItem;
#RestController
#RequestMapping(value="/food-item")
#ResponseBody
public class FoodItemController {
private FoodItemDAO fDAO;
#Autowired
public FoodItemController(FoodItemDAO fDAO) {
super();
this.fDAO = fDAO;
}
// Getting all FoodItems
#GetMapping
public ResponseEntity<List<FoodItem>> getAllFoodItems() {
return ResponseEntity.ok(fDAO.findAll());
}
Application.properties:
server.port = 4009
server.servlet.context-path = /food
management.endpoint.health.show-details = always
management.endpoints.web.exposure.include = *
spring.datasource.url = jdbc:postgresql://<some-ip-address>:5432/postgres
spring.datasource.username = <username>
spring.datasource.password = <password>
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.default_schema = project_2
build.gradle
plugins {
id 'org.springframework.boot' version '2.7.2'
id 'io.spring.dependency-management' version '1.0.12.RELEASE'
id 'java'
id "org.hibernate.orm" version "6.1.1.Final"
}
group = 'com.revature'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'org.postgresql:postgresql'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
We're not using XML. What might be the problem?
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.
In order to use Spring Integration Amqp in a Spring Boot application, what are the dependencies I need to include?
Spring Boot version is 2.0.5.
Current dependencies I have are spring-boot-starter-integration and spring-integration-amqp
Error messages are classes like SimpleMessageListenerContainer and AmqpInboundChannelAdapter are not found on the classpath.
UPDATE:
My build.gradle entries --
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
}
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-integration')
compile('org.springframework.boot:spring-boot-starter-amqp')
compile('org.springframework.integration:spring-integration-amqp')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
I had to add the following dependencies to resolve the classes in question (the last in the list did it, using latest spring initalizr, spring-boot 2.0.5)
dependencies {
implementation('org.springframework.boot:spring-boot-starter-amqp')
implementation('org.springframework.boot:spring-boot-starter-integration')
testImplementation('org.springframework.boot:spring-boot-starter-test')
compile 'org.springframework.integration:spring-integration-amqp'
}
To be fair, this answer was already given, just not for gradle.
I am using gradle 4.10.2 on a linux machine, spring-boot initialzr with the options RabbitMQ and Spring-Integration. Here are the changed files:
build.gradle
buildscript {
ext {
springBootVersion = '2.0.5.RELEASE'
}
repositories {
mavenCentral()
}
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.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
implementation('org.springframework.boot:spring-boot-starter-amqp')
implementation('org.springframework.boot:spring-boot-starter-integration')
testImplementation('org.springframework.boot:spring-boot-starter-test')
compile 'org.springframework.integration:spring-integration-amqp'
}
Implementation of Example 12.2.1 Configuring with Java Configuration from the Spring Integration docs:
package com.example.integrationamqp;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.amqp.inbound.AmqpInboundChannelAdapter;
import org.springframework.integration.amqp.inbound.AmqpInboundGateway;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
#SpringBootApplication
public class IntegrationAmqpApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(IntegrationAmqpApplication.class)
.web(WebApplicationType.NONE)
.run(args);
}
#Bean
public MessageChannel amqpInputChannel() {
return new DirectChannel();
}
#Bean
public AmqpInboundChannelAdapter inbound(SimpleMessageListenerContainer listenerContainer,
#Qualifier("amqpInputChannel") MessageChannel channel) {
AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(listenerContainer);
adapter.setOutputChannel(channel);
return adapter;
}
#Bean
public SimpleMessageListenerContainer container(ConnectionFactory connectionFactory) {
SimpleMessageListenerContainer container =
new SimpleMessageListenerContainer(connectionFactory);
container.setQueueNames("foo");
container.setConcurrentConsumers(2);
// ...
return container;
}
#Bean
#ServiceActivator(inputChannel = "amqpInputChannel")
public MessageHandler handler() {
return new MessageHandler() {
#Override
public void handleMessage(Message<?> message) throws MessagingException {
System.out.println(message.getPayload());
}
};
}
}
Add this dependency:
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
And are you sure you have this one?:
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-amqp</artifactId>
I have a SpringBootApp that I can run in eclipse by right-clinging and then selecting Run as -> Java Application. However, when I build a .war file and deploy it to my tomcat server. The console looks as though the application should have deployed but I'm unable to hit my rest services. I'm getting a 404 error. I really have no clue why it is not working so if anyone knows a solution could you explain why... Also, when I look in my war file i see all the classes files needed to deploy the application.
You can download the code github.
Controller:
package com.sample.pkg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
#Controller
#ComponentScan("com.sample.pkg")
public class SampleController {
#Autowired
SampleComponent component;
#RequestMapping("/sendOption")
public String update(#RequestParam(value = "option") String option) {
if (option.equalsIgnoreCase("option1")) {
component.updateStatus(true);
return "Something was updated";
} else if (option.equalsIgnoreCase("option2")) {
component.updateStatus(false);
return "Something else was updated";
}
return "You have entered in an option that is invalid. Please enter a valid option.";
}
#RequestMapping("/getOption")
public String control() {
if (component.getStatus()) {
return "option1";
} else if (!component.getStatus()) {
return "option2";
}
return "error";
}
}
Application.java
package com.chicken.door;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.PropertySource;
#SpringBootApplication
#PropertySource("classpath:application.properties")
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.3.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
war {
baseName = 'gs-spring-boot'
version = '0.1.0'
}
jar {
baseName = 'gs-spring-boot'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-actuator")
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testCompile("org.springframework.boot:spring-boot-starter-test")
}
Output:
2017-07-18 21:32:13.246 INFO 947 --- [ main] com.sample.pkg.Application : Started Application in 10.6 seconds (JVM running for 10.976)
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.