h2 database console in html layout:fragment (Spring Boot + Java)

huangapple go评论73阅读模式
英文:

h2 database console in html layout:fragment (Spring Boot + Java)

问题

<body>
    <div class="container">
        <nav class="navbar navbar-default">
            <ul class="nav navbar-nav">
                <li><a href="/">主页</a></li>
                <li><a href="/blogs">博客</a></li>
                <li><a href="/bloggers">博主</a></li>
                <li sec:authorize="hasRole('ROLE_ADMIN')"><a href="/admin">管理员</a></li>
                <li sec:authorize="hasRole('ROLE_ADMIN')"><a href="/database">数据库</a></li>
                <li><a href="/signup"><button id="signup-small-button" class="btn btn-success">注册</button></a></li>
            </ul>
            <div class="navbar-right">
                <form sec:authorize="!isAuthenticated()" th:action="@{/loginfailed}" method="post">
                    <input th:value="${username}" type="text" name="username" placeholder="用户名" required class="login-input"/>
                    <input th:value="${password}" type="password" name="password" placeholder="密码" required class="login-input"/>
                    <button type="submit" id="login-button" class="btn btn-success btn-xs">登录</button>
                </form>
                <div sec:authorize="isAuthenticated()">
                    <span th:text="${#authentication.name + ' | '}" class="black"></span>
                    <form th:action="@{/logout}" method="post" id="logout-button-form">
                        <button type="submit" id="logout-button" class="btn btn-primary btn-xs">登出</button>
                    </form>
                </div>
            </div>
        </nav>
        <div layout:fragment="content"></div>
        <footer>
            <hr/>
            <p id="footer">由hazazs制作(版权所有©hazazs™)</p>
        </footer>
    </div>
</body>
英文:

I'm working on a Spring Boot application. I use main.html as a frame around all the other .htmls embedded. For this I use layout:fragment.

I also have a "DATABASE" menu in my navigaton bar, which points to the H2 database console.

Is it possible to embed this console into the main.html as fragment?

main.html's body:

&lt;body&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;nav class=&quot;navbar navbar-default&quot;&gt;
&lt;ul class=&quot;nav navbar-nav&quot;&gt;
&lt;li&gt;&lt;a href=&quot;/&quot;&gt;HOME&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/blogs&quot;&gt;BLOGS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/bloggers&quot;&gt;BLOGGERS&lt;/a&gt;&lt;/li&gt;
&lt;li sec:authorize=&quot;hasRole(&#39;ROLE_ADMIN&#39;)&quot;&gt;&lt;a href=&quot;/admin&quot;&gt;ADMIN&lt;/a&gt;&lt;/li&gt;
&lt;li sec:authorize=&quot;hasRole(&#39;ROLE_ADMIN&#39;)&quot;&gt;&lt;a href=&quot;/database&quot;&gt;DATABASE&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/signup&quot;&gt;&lt;button id=&quot;signup-small-button&quot; class=&quot;btn btn-success&quot;&gt;SIGN UP&lt;/button&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;navbar-right&quot;&gt;
&lt;form sec:authorize=&quot;!isAuthenticated()&quot; th:action=&quot;@{/loginfailed}&quot; method=&quot;post&quot;&gt;
&lt;input th:value=&quot;${username}&quot; type=&quot;text&quot; name=&quot;username&quot; placeholder=&quot;username&quot; required class=&quot;login-input&quot;/&gt;
&lt;input th:value=&quot;${password}&quot; type=&quot;password&quot; name=&quot;password&quot; placeholder=&quot;password&quot; required class=&quot;login-input&quot;/&gt;
&lt;button type=&quot;submit&quot; id=&quot;login-button&quot; class=&quot;btn btn-success btn-xs&quot;&gt;Log in&lt;/button&gt;
&lt;/form&gt;
&lt;div sec:authorize=&quot;isAuthenticated()&quot;&gt;
&lt;span th:text=&quot;${#authentication.name + &#39; | &#39;}&quot; class=&quot;black&quot;&gt;&lt;/span&gt;
&lt;form th:action=&quot;@{/logout}&quot; method=&quot;post&quot; id=&quot;logout-button-form&quot;&gt;
&lt;button type=&quot;submit&quot; id=&quot;logout-button&quot; class=&quot;btn btn-primary btn-xs&quot;&gt;Log out&lt;/button&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/nav&gt;
&lt;div layout:fragment=&quot;content&quot;&gt;&lt;/div&gt;
&lt;footer&gt;
&lt;hr/&gt;
&lt;p id=&quot;footer&quot;&gt;Made by hazazs (&#169;Copyrighted by hazazs™)&lt;/p&gt;
&lt;/footer&gt;
&lt;/div&gt;
&lt;/body&gt;

答案1

得分: 1

以下是翻译好的内容:

解决方案是一个名为 database.html 的文件:

<html layout:decorate="~{layout/main}">
    <head>
        <title>数据库</title>
    </head>
    <body>
        <div layout:fragment="content" class="center">
            <iframe src="http://IP:PORT/h2" id="h2-console"></iframe>
        </div>
    </body>
</html>

当然还有一个用于路径为 "/database" 的 @RequestMapping,用于返回这个 HTML。

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

application.yaml:

spring:
    h2:
        console:
            enabled: true
            path: /h2
英文:

The solution was a database.html file:

&lt;html layout:decorate=&quot;~{layout/main}&quot;&gt;
&lt;head&gt;
&lt;title&gt;DATABASE&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div layout:fragment=&quot;content&quot; class=&quot;center&quot;&gt;
&lt;iframe src=&quot;http://IP:PORT/h2&quot; id=&quot;h2-console&quot;&gt;&lt;/iframe&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

And of course a @RequestMapping for "/database" which returns this html.

@RequestMapping(value = &quot;/database&quot;, method = RequestMethod.GET)
public String database() {
return &quot;database&quot;;
} 

application.yaml:

spring:
h2:
console:
enabled: true
path: /h2

huangapple
  • 本文由 发表于 2020年10月12日 22:14:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64319613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定