1

我正在尝试在某些 JavaScript 中访问 Spring Source Security 用户变量 ${username} 并且它一直以空字符串的形式返回。在上一页中,我仅使用 ${username} 显示用户名。据我所知,我需要做的就是在 javascript 中得到它:

<c:out value="${username}"/>

但是在下面的代码中,生成的 HTML 总是有一个空字符串。我错过了什么?

<%@ page session="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The Admin Page</title>
<style type="text/css">

    .wideCols .field-radio { width: 5%; }
    .wideCols .field-username { width: 15%; }
    .wideCols .field-firstName { width: 15%; }
    .wideCols .field-lastName { width: 20%; }
    .wideCols .field-email { width: 40%; }
    .wideCols .field-enabled { width: 5%; }
</style>
</head>
<body>
    <h1>Users</h1>
    <spring:url value="/admin/addUser" var="accountUrl" />

    <a href="${accountUrl}">Add User</a> 
    <div id="result1"></div>

    <div id="grid" class="wideCols"></div>
    <!-- <button type="button" id="saveNode"></button>  -->


    <script type="text/javascript">
        require([ "dojo/store/JsonRest",
                  "dojo/_base/declare",
                  "dgrid/OnDemandGrid",
                  "dgrid/Selection", 
                  "dgrid/editor",
                  "dgrid/selector",
                  "dojo/domReady!" 
                ], function(JsonRest, declare, OnDemandGrid, Selection, editor, selector) {

            var userAccountStore = new JsonRest({
                idProperty : "userAccountId",
                target : "<c:url value="/userMgr/" />"
            });

            window.grid = new (declare([OnDemandGrid, Selection]))({                
                store : userAccountStore,           
                columns:  [ selector({ label: 'radio'}, "radio"),
                            { label: "Username", field: "username"}, 
                            { label: "First Name", field: "firstName"}, 
                            { label: "Last Name", field: "lastName"}, 
                            { label: "Email", field: "email"}, 
                            editor({ label: "Enabled", field: "enabled", autoSave: "true", canEdit: function(object){
                                return object.username != "<c:out value="${username}"/>";
                            }, }, "checkbox")],
            }, "grid"); 

        });

    </script>
</body>
</html>
4

1 回答 1

3

用户名不会自动添加到您的模型中。您需要在控制器中执行此操作,或者如果您无权访问控制器代码,则可以在 Spring Security taglib 中使用已弃用的方法。:

<sec:authentication property="principal.username" />

有关更多信息,请参阅http://static.springsource.org/spring-security/site/docs/3.0.x/reference/taglibs.html#d0e6295

于 2013-04-04T14:10:46.970 回答