【问题标题】:How to redirect from one html page to another html using Springboot如何使用 Spring Boot 从一个 html 页面重定向到另一个 html
【发布时间】:2020-05-25 19:37:29
【问题描述】:

我正在使用 Springboot 和 Thymeleaf 制作一个井字游戏应用程序。但我面临将我的index.html 重定向到message.html 的问题

这是我的 index.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>

<title>Tic Tac Toe</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script
    src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="js/app-main.js"></script>

<link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<link rel="stylesheet" href="css/app-layout.css" />

<link rel="icon" type="image/png" href="images/tic-tac-toe.png" />

</head>
<body>

    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <div class="navbar-header">
                <span class="navbar-brand"> <img alt="Brand"
                    src="images/tic-tac-toe.png" /> Tic Tac Toe
                </span>
            </div>
        </div>
    </nav>

    <div class="game-area panel panel-default">
        <div class="panel-body">

            <h4 class="game-status">
                <span th:if="${game.getPlayerState().isInProgress()}"
                    class="label label-default"> Your turn. Think very carefully
                    and click a square when ready.</span> <span
                    th:if="${game.getPlayerState().isWin()}"
                    class="label label-success"> YOU WIN! Your superior
                    intellect prevailed and assured a sound victory. Click here to <a
                    href="/message">view your reward</a>
                </span> <span th:if="${game.getPlayerState().isLoss()}"
                    class="label label-danger"> You've lost. Better luck next
                    time!</span> <span th:if="${game.getPlayerState().isDraw()}"
                    class="label label-info"> Mheh! A smelly draw. Beats losing
                    though, right?</span>
            </h4>

            <table class="board">
                <tr th:each="row : ${game.getBoard().getTiles()}" class="board-row">
                    <td th:each="tile : ${row}">
                        <div th:id="${tile.getId()}" class="board-row-tile"
                            th:classappend="${!tile.isEmpty()} ? ${tile} + '-value' : (${!game.isGameOver()} ? 'available')"
                            th:text="${tile}">&nbsp;</div>
                    </td>
                </tr>
            </table>

            <form id="form_mark_tile" th:action="@{/}" method="POST"
                class="form-inline">
                <div class="checkbox">
                    <label><input name="player_go_first" type="checkbox"
                        th:checked="${game.isPlayerGoFirst()}" /> Play First</label>
                </div>

                <div class="btn-new-game-wrap">
                    <a id="btn-new-game" class="btn btn-success btn-lg"
                        href="javascript:void(0);" role="button">New Game</a>
                </div>

                <input id="is_game_over" type="hidden"
                    th:value="${game.isGameOver()}" /> <input id="tile_id"
                    name="tile_id" type="hidden" value="" /> <input id="new_game"
                    name="new_game" type="hidden" value="" />
            </form>

        </div>
    </div>

</body>
</html>

这是我的井字游戏控制器

@Controller
@SessionAttributes("game")
public class TicTacToeController {

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

@RequestMapping(value = "/", method = RequestMethod.POST)
public String markTile(@ModelAttribute("game") Game game, @RequestParam("tile_id") String tileId,
        @RequestParam(value = "new_game", required = false, defaultValue = "false") boolean newGame,
        @RequestParam(value = "player_go_first", required = false, defaultValue = "false") boolean playerGoFirst) {

    if (newGame) {
        game.reset();
        game.setPlayerGoFirst(playerGoFirst);
        if (!playerGoFirst) {
            // give computer a small advantage by always placing X in the center as its
            // first move
            game.markTile("1-1");
        }
    } else {
        game.markTile(tileId); // Player Turn

        game.markTileRandom(); // Computer Turn
    }

    return "index";
}

@GetMapping(value = "/message")
public String message() {
    return "redirect:/message.html";
}

@ModelAttribute("game")
public Game populateGame() {
    return new Game();
}

}

有人可以用一段代码指导我如何将其重定向到新的 HTML 页面,message.html。我没有从index.html 传递任何特定数据。在 href 属性上运行时出现以下错误。

> Whitelabel 错误页面 此应用程序没有显式映射 /error,因此您将其视为后备。

【问题讨论】:

    标签: java html spring-boot


    【解决方案1】:

    首先我建议你使用ErrorController接口,这样你就可以更好地跟踪你的错误在哪里。

    https://www.baeldung.com/spring-boot-custom-error-page

    至于您的错误,我假设您的其他控制器端点工作正常(如果不是这种情况,请检查 @ComponentScan 相关问题),并且您的模板文件夹中有一个 message.html 模板(如果您没有'没有你的 message.html 那么你肯定会得到 404 错误)。但假设你拥有这一切:

    1. 直接从浏览器转到http://localhost:8080/message.html,看看它是如何失败的。所以从这里我们知道这不是你的重定向引起的问题

    2. 如果不是重定向的问题,则表示问题是无法解析此 URL。所以在某些时候 Spring 不知道在哪里可以找到你的页面(因为没有人告诉他:( )

    3. 现在您将“messaga.html”文件移动到“src/main/resources/static/”文件夹并... Tadaaa !神奇地起作用。

    为什么它现在有效?基本上是因为 Spring 默认在哪里查找静态内容 (https://docs.spring.io/spring-boot/docs/1.1.5.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-static-content)。

    如果你想保持静态页面,去/message端点重定向到/message.html是没有意义的,你可以直接设置你的href attr比如/message.html

    如果您希望您的 message.html 页面不是静态的,请将其设为模板并使用 ModelAndView 或其他方式返回它。 https://www.baeldung.com/spring-mvc-model-model-map-model-view

    如果您仍想重定向更多内容。 https://www.baeldung.com/spring-redirect-and-forward

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-17
      • 2012-12-04
      • 2021-06-27
      • 2022-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-09
      相关资源
      最近更新 更多