javascript - 奇怪的: session variable returns null from a post controller to a jsp page

标签 javascript java spring jsp session

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

这是我在 Controller 中的尝试

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>

已编辑:这是完整的 Controller

@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;
    }

请帮忙

最佳答案

嗯,“${}”用于获取 Controller 设置的 session 值。就你而言, “${mp3}”就可以了。 试试这个 ->

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

关于javascript - 奇怪的: session variable returns null from a post controller to a jsp page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40150442/

相关文章:

javascript - 模块构建错误 : pngquant ENOENT - webpack build succeeds on local OS X, 在 AWS Ubuntu 16.04 服务器上失败

java - JPanel 的 TitledBorder 上没有 eclipse 刻框架

javascript url 解码不起作用

javascript - 根据 toFixed(2),为什么小数不能正确显示?

java - 为什么使用dialog和putextra不起作用

Java:继承与依赖注入(inject) "Autowired"

java - 使用 REST 访问 JPA 数据失败 找不到合适的 HttpMessageConverter

java - 导入 270000 行数据时的 Hibernate 问题

javascript - Processingjs Firefox 错误 : "TypeError: $it2.hasNext is not a function"

java - 如何使用带有内联 XSD 的 jaxb2 maven 插件?