user7135817
user7135817

Reputation:

Problems with Spring Boot web application in Java?

I am very new to Spring Boot. I was reading the documentation and trying to play around with stuffs. I like the whole idea of not having to do anything with the configuration stuffs like in Spring MVC app but then I was trying to connect my Controller with my View by putting data in a Model object. But for some reason, I am not getting what I am looking for in the View. Can someone please point my error. This is my controller class-

package com.example.LoginAndRegistration;

import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

    @RequestMapping("/Login")
    public String welcome(Model model) {
        model.addAttribute("login", "Hello World");
        return "loginview";
    }

}

I have used Thymeleaf to render my model object to my view. I added the thymeleaf dependency through Maven and then added the following html file in my templates folder of my Springboot application. But I still dont get the message that I am passing from my controller. Rather it prints out loginview as string rather than looking up for loginview in my template folder and printing out the message HelloWorld.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1"></meta>
<title>Insert title here</title>
</head>
<body>
Message: <span th:text="${login}"></span>
</body>
</html>

These is what I see in my logs-

2017-06-18 11:33:30.508  INFO 3112 --- [           main] c.e.L.LoginAndRegistrationApplication    : Starting LoginAndRegistrationApplication on VAIO with PID 3112 (C:\Users\user\javaprojects\LoginAndRegistration\target\classes started by user in C:\Users\user\javaprojects\LoginAndRegistration)
2017-06-18 11:33:30.534  INFO 3112 --- [           main] c.e.L.LoginAndRegistrationApplication    : No active profile set, falling back to default profiles: default
2017-06-18 11:33:30.818  INFO 3112 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@58134517: startup date [Sun Jun 18 11:33:30 EAT 2017]; root of context hierarchy
2017-06-18 11:33:34.615  INFO 3112 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-06-18 11:33:34.641  INFO 3112 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2017-06-18 11:33:34.644  INFO 3112 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-06-18 11:33:34.983  INFO 3112 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-06-18 11:33:34.983  INFO 3112 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4198 ms
2017-06-18 11:33:35.402  INFO 3112 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-06-18 11:33:35.410  INFO 3112 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-06-18 11:33:35.412  INFO 3112 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-06-18 11:33:35.412  INFO 3112 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-06-18 11:33:35.413  INFO 3112 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-06-18 11:33:36.071  INFO 3112 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@58134517: startup date [Sun Jun 18 11:33:30 EAT 2017]; root of context hierarchy
2017-06-18 11:33:36.215  INFO 3112 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/Login]}" onto public java.lang.String com.example.LoginAndRegistration.LoginController.welcome(org.springframework.ui.Model)
2017-06-18 11:33:36.222  INFO 3112 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-06-18 11:33:36.223  INFO 3112 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-06-18 11:33:36.290  INFO 3112 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-18 11:33:36.290  INFO 3112 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-18 11:33:36.370  INFO 3112 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-18 11:33:36.761  INFO 3112 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-06-18 11:33:36.871  INFO 3112 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-06-18 11:33:36.878  INFO 3112 --- [           main] c.e.L.LoginAndRegistrationApplication    : Started LoginAndRegistrationApplication in 7.793 seconds (JVM running for 13.483)
2017-06-18 11:33:54.561  INFO 3112 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-06-18 11:33:54.561  INFO 3112 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2017-06-18 11:33:54.754  INFO 3112 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 193 ms

Project

Upvotes: 0

Views: 1175

Answers (2)

Arif K.
Arif K.

Reputation: 43

Would you try to creata a subpackage of com.example.LoginAndRegistration as com.example.LoginAndRegistration.controller and move your loginController in it? I had same problem and solved like that

Upvotes: 0

Mykola Yashchenko
Mykola Yashchenko

Reputation: 5371

Change @RestController to @Controller. When a bean is annotated with @RestController, Spring assumes that each method of a controller is annotated with @ResponseBody.

Upvotes: 4

Related Questions