2016-10-20 3 views
1

私のJava Springアプリケーションでは、jspページで警告すると、この変数がnullを返す理由を理解しようとしています。私はシステムコンソールのログを入れて、実際の値を表示しますが、jspページで取得するとnullです。奇妙な:セッション変数がポストコントローラから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 

で私の試みである、それはnullを返し、これが試み

<script> 
function getNotify() { 

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

</script> 

EDITTEDです:これが完了コントローラー

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

+0

同じセッションで作業していることを確認しましたか?何らかの理由(クッキーが転送されず、いくつかのコードがセッションをリフレッシュするなど)のために新しいものを取得する可能性があります。 – Thomas

+0

はい私はやった......それはそこにあるコードです – Francis

+0

'request.getSession()'は新しいセッションを作成します(存在しない場合)。何らかの理由で新しいセッションが作成されていないことを確認するには、 'request.getSession(false)'と呼ぶことができます。さらに、コードをより多く投稿することができます。コントローラクラス全体とjspページ。 – pleft

答えて

0

まあ、 "$ {}"は、コントローラによって設定されたセッション値を取得するために使用されます。あなたのケースでは、 "$ {mp3}"がそのトリックを行います。 試してみる - >

<script> 
function getNotify() { 
    var mp3 = "${mp3}"; 
    alert(mp3);   
setInterval(getNotify, 20000); 
} 
</script> 
関連する問題