1

I am using Thymeleaf along with Springboot in Scala. When I do a get request from HTML page, it goes to the controller and returns the next HTML file name as a string instead of the entire HTML.

my build.gradle

compile("org.thymeleaf:thymeleaf-spring4")
compile("nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")  

I have placed all html in src/main/resources/templates

main html

<html xmlns:th="http://www.thymeleaf.org">

<a href="#" th:href="@{/api/v1/users/userval}">Login</a>

Controller

@RestController 
@RequestMapping(value = Array("/api/v1/users"),produces = Array("text/html"))
class UserController {

@RequestMapping(method = Array(RequestMethod.POST))
@ResponseStatus(value = HttpStatus.CREATED)
def createUser(@Valid @RequestBody user:User) = {     

 UserRepository.populateUser(user)

}

 @RequestMapping(value=Array("/userval"),method = Array(RequestMethod.GET))
  def userLoginForm( model:Model) = {
   model.addAttribute("userLogin", new UserLogin())
  "login"}  

 @RequestMapping(value=Array("/userval"),method = Array(RequestMethod.POST))
  def getUser(@ModelAttribute  userLogin:UserLogin, bindingResult: BindingResult) = {
    "reservations"
  } 

Here when I click on login link in main HTML, I get "login" as a string instead of login.html

komal dedhia
  • 217
  • 1
  • 3
  • 8

1 Answers1

3

If you want the response to render a view, annotate your class with @Controller rather than @RestController

@RestController tells Spring that the return value is the body of the response, i.e. no view rendering will occur. It's the equivalent of annotating the class with @Controller and each method with @ResponseBody.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • I did used @Controller and I placed my html pages in templates folder but still in scala it just unables to pick up the html page but where as in java same application works ok ? – sri hari kali charan Tummala Jun 25 '19 at 21:17
  • @sriharikalicharanTummala That sounds like a different problem. It would be better to tackled it as a separate question that provides sufficient information for someone to reproduce the problem. – Andy Wilkinson Jun 26 '19 at 09:31
  • https://stackoverflow.com/questions/56761061/spring-mvc-scala-app-just-returns-index-html-page-rest-of-the-routes-doesnt-wor , got it worked sorry to bother you @Andy Wilkinson – sri hari kali charan Tummala Jun 26 '19 at 13:19