【问题标题】:Pass variable from a jsp file to a tag将变量从 jsp 文件传递​​到标签
【发布时间】:2017-08-30 05:41:14
【问题描述】:

我正在使用标签将我的应用程序主体插入到应用程序的全局布局中。我正在尝试将在 .jsp 文件中声明的新 css/js 文件(特定于应用程序)插入到标签中(不确定我是否清楚,但代码应该澄清这一点)

这是我的 wrapper.tag 文件(有点简化):

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@tag description="Wrapper Tag" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>Title of the page</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="robots" content="noindex,nofollow" />

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

    <!-- Custom styles -->
    <spring:url value="/resources/core/css/style.css" var="coreCss" />
    <link href="${coreCss}" rel="stylesheet" />

    <!-- jQuery -->
    <script src="https://code.jquery.com/jquery-3.2.1.min.js" crossorigin="anonymous"></script>
    <!-- Bootstrap -->
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    <!-- Custom scripts -->
    <spring:url value="/resources/core/js/script.js" var="coreJs" />
    <script src="${coreJs}"></script>
</head>
<body>
    <div class="content">
        <div class="app-content">
            <jsp:doBody/>
        </div>
    </div>
</body>
</html>

下面是一个使用该标签的 jsp 文件示例:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>

<t:wrapper>
    <div class="container">
        <form class="form-signin">
            <h2 class="form-signin-heading">Please sign in</h2>
            <label for="inputEmail" class="sr-only">Email address</label>
            <input type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>

            <label for="inputPassword" class="sr-only">Password</label>
            <input type="password" id="inputPassword" class="form-control" placeholder="Password" required>

            <div class="checkbox">
                <label>
                    <input type="checkbox" value="remember-me"> Remember me
                </label>
            </div>

            <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
        </form>
    </div>
</t:wrapper>

我想在 jsp 文件中声明变量以插入新的 css/js 文件以加载到包装器标签的 &lt;head&gt; 中。

【问题讨论】:

    标签: java jsp spring-mvc jsp-tags


    【解决方案1】:

    我找到了一种方法,下面是实现它的方法。

    只需在jsp文件中的标签调用中传递一个属性即可:

    <spring:url value="/resources/user/css/login.css" var="loginCss" />
    
    <t:wrapper css="${loginCss}">
    

    并添加包装标签的&lt;head&gt;

    <%@ attribute name="css" %>
    <c:if test="${!empty css}" >
        <link href="${css}" rel="stylesheet" />
    </c:if>
    

    对于 JS 也是如此。

    【讨论】:

      猜你喜欢
      • 2014-09-05
      • 2013-06-10
      • 2013-07-17
      • 2011-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-01
      • 1970-01-01
      相关资源
      最近更新 更多