Spring MVC Scala App just returns index.html page

2019-08-16 06:27发布

问题:

I am trying to create a simple spring MVC app in Scala I did define my methods in the controller to bring back html pages based on name from resources folder but it just always brings back just index page and the rest of html pages while trying to access the route it just fails, but same application works fine in Java.

full source code is here:-

Java:- https://github.com/kali786516/SpringConfigServer-client/tree/master/src/main/java/com/example/SpringConfigServerclient

Scala:- https://github.com/kali786516/SpringConfigServer-client/tree/master/src/main/scala/com/ps/spring/mvc/psbankapp

Error in Scala:-

Index html Works Fine:-

but rest of the routes doesn't work in scala

Scala controller:-

package com.ps.spring.mvc.psbankapp.controllers

import org.springframework.beans.factory.annotation.Value
import org.springframework.cloud.context.config.annotation.RefreshScope
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestMethod

//@RefreshScope
@Controller
//@ComponentScan(basePackages = Array("com.ps.spring.mvc.psbankapp"))
class AccountController {


  @RequestMapping(value = Array("/"))
  def showHomePage(): Unit = {
    "index"
  }

  @RequestMapping(value = Array("/new"), method = Array(RequestMethod.GET))
  def newAccount(): Unit = {
    "newAccount"
  }

  @RequestMapping(value = Array("/showAccount"))
  def showAccount(): Unit = {
    "showAccount"
  }
}

Java Controller:-

package com.example.SpringConfigServerclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;

@RefreshScope
@Controller
public class RateController {

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String getIndex() {
        return "index";
    }

    @RequestMapping(value = "/new",method = RequestMethod.GET)
    public String newAccount() {
        return "newAccount";
    }

    @RequestMapping(value = "/showAccount",method = RequestMethod.GET)
    public String showAccount() {
        return "showAccount";
    }
}

回答1:

Finally got it working by adding below context in my HTML files.

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />

Answer:- Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers

full html if you need:-

<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>Pluralsight Training: Config Client</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"></link>
</head>
<body>
<div class="row">
    <div class="col-md-2"></div>
    <div class="col-md-8">
        <h1>Pluralsight Training: Welcome to PS Bank Web Application index</h1>
    </div>
    <div class="col-md-2"></div>
</div>
</body>
</html>