奇怪:从post控制器到jsp页面的session变量返回null

Weird: session variable returns null from a post controller to a jsp page

本文关键字:session 变量 null 返回 jsp post 控制器 奇怪      更新时间:2023-09-26

在我的Java Spring应用程序中,我一直在试图弄清楚为什么当我在jsp页面中警告它时,这个变量一直返回null。我已经把系统控制台日志,它打印实际值,但在jsp页面检索它时,它是空的。

这是我在控制器中的尝试

String sound = "sound.mp3";
            request.getSession().setAttribute("mp3", sound);
            String mp3 = (String) request.getSession().getAttribute("mp3");
            System.out.println("this is the sound notification>>>>>> " + mp3); //this returns the actual value

在尝试检索JSP页面中的值时,它返回null,这是尝试

<script>
function getNotify() {
        <% String mp3 = (String) request.getSession().getAttribute("mp3"); %>
        var mp3 = "<%= mp3 %>";
        alert(mp3);
 setInterval(getNotify, 20000);
</script>

已编辑:这是完整的控制器

@ResponseBody
    @RequestMapping(value = "/post-book", method = RequestMethod.POST)
    public ModelAndView postBook(HttpServletRequest request,
                           HttpServletResponse response,
                           @RequestParam String message,
                           @RequestParam String noteVal,
                           @CookieValue(required = true) String name,
                           @CookieValue(required = true) String emailaccess,
                           @CookieValue(required = true) String token) {
        String token2 = (String) request.getSession().getAttribute("token");
        String emailToken = (String) request.getSession().getAttribute("emailaccess");
        try {
            // fetch user info
            User user = _userDao.getByToken(token);
            if (user == null) {
                return logout(request, response);
            }
            //}

            Date today = new Date();
            //formatting date in Java using SimpleDateFormat
            SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            String date2 = DATE_FORMAT.format(today);
            System.out.println("Today in dd-MM-yyyy format : " + date2);
            // create new book object
            Books book = new Books();
            book.setMessage(message);
            book.setUser(user);
            book.setTimestamp(date2);
            // save book in db
            _bookDao.save(book);

            String sound = "postnotificationsound.mp3";
            request.getSession().setAttribute("sound", sound);
            String mp3 = (String) request.getSession().getAttribute("sound");
            System.out.println("this is the sound notification>>>>>> " + mp3); //this returns the actual value

            System.out.println("this is the sound notification2>>>>>> " + noteVal);

        } catch (Exception e) {
            logger.error("Exception in saving book: ", e.getStackTrace());
            e.printStackTrace();
        }
        return null;
    }

请帮忙

"${}"用于获取控制器已设置的会话值。对你来说,"${mp3}"就可以了。试试这个->

<script>
function getNotify() {
    var mp3 = "${mp3}";
    alert(mp3);        
setInterval(getNotify, 20000);
}
</script>