gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:08:05 24 4
gpt4 key购买 nike

在我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com