Spring MVC Scala App 仅返回 index.html 页面其余路由不起作用

Posted

技术标签:

【中文标题】Spring MVC Scala App 仅返回 index.html 页面其余路由不起作用【英文标题】:Spring MVC Scala App just returns index.html page rest of the routes doesn't work 【发布时间】:2019-11-07 16:56:59 【问题描述】:

我正在尝试在 Scala 中创建一个简单的 Spring MVC 应用程序我确实在控制器中定义了我的方法以从资源文件夹中带回基于名称的 html 页面,但它总是只带回索引页面和其余的 html 页面同时尝试访问它只是失败的路由,但相同的应用程序在 Java 中运行良好。

完整的源代码在这里:-

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

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

Scala 中的错误:-

索引 html 工作正常:-

但其余路线在 scala 中不起作用

Scala 控制器:-

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 控制器:-

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】:

最后通过在我的 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" />

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

如果你需要完整的 html:-

<!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>

【讨论】:

以上是关于Spring MVC Scala App 仅返回 index.html 页面其余路由不起作用的主要内容,如果未能解决你的问题,请参考以下文章

如何为JSP配置spring boot mvc app?

Spring MVC全局异常后返回JSON异常数据

如何仅更新已更改的属性 - Spring MVC

如何仅更新已更改的属性 - Spring MVC mongoDB

谁在 Spring MVC (@ResponseBody) 中设置响应内容类型

spring-mvc 的一些使用技巧(转)