2017-12-29 32 views
1

「表示」ボタンをクリックして、特定のユーザをモーダルウィンドウのヘルプで表示したいとします。私はコントローラにajaxクエリでこれをやろうとしています。また、私はエラーがあります: 予期しないエラー(タイプ=内部サーバーエラー、ステータス= 500)がありました。 例外評価SpringEL式: "user.firstName"(ユーザ:190)aymaxクエリでthymeleafのコントローラからモダールウィンドウを生成する方法

表:

<tr th:each="user : ${users}"> 
      <td th:text="${user.firstName}"></td> 
      <td th:text="${user.lastName}"></td> 
      <td th:text="${user.username}"></td> 
      <td> 
       <a th:onclick="'javascript:viewUser(\'' + ${user.id} +'\');'" class="btn-floating waves-effect waves-light yellow"><i class="material-icons">visibility</i></a> 
       <a href="#updateUser" class="btn-floating waves-effect waves-light blue btn modal-trigger"><i class="material-icons">edit</i></a> 
       <a th:href="${'/users/delete/' + user.id}" class="btn-floating waves-effect waves-light red"><i class="material-icons">delete</i></a> 
      </td> 
     </tr> 

モーダル:

<div id="viewUser" class="modal modal-fixed-footer" th:fragment="view"> 
     <div class="modal-content"> 
      <h4>View User</h4> 
       <div class="row"> 
        <div class="col s12"> 
         <div class="row"> 
          <table> 
           <thead> 
           <tr> 
            <th></th> 
            <th></th> 
           </tr> 
           </thead> 
           <tbody> 
           <tr> 
            <td>First Name: </td> 
            <td th:text="${user.firstName}"></td> 
           </tr> 
           <tr> 
            <td>Last Name: </td> 
            <td th:text="${user.lastName}"></td> 
           </tr> 
           <tr> 
            <td>Username</td> 
            <td th:text="${user.username}"></td> 
           </tr> 
           </tbody> 
          </table> 
         </div> 
        </div> 
       </div> 
     </div> 
     <div class="modal-footer"> 
      <button class="modal-action modal-close waves-effect waves-light btn-floating red"><i class="material-icons">close</i></button> 
     </div> 
    </div> 

JS関数:

function viewUser(id) { 
    $.ajax({ 
     url: "https://stackoverflow.com/users/view/" + id, 
     success: function(response) { 
      console.log(response); 
      $('#viewUserHolder').html(response); 
      $('#viewUser').modal('open'); 
     } 
    }); 
} 

方法でコントローラ:

@RequestMapping(value = "/view/{id}") 
public String view(@PathVariable("id") Integer id, ModelMap modelMap) { 
    User user = userRepository.findOne(id); 
    modelMap.addAttribute("user", user); 
    return "users :: view"; 
} 
+0

最新のstacktraceを更新してください。あなたのコードのどこかに問題があります。それはユーザーのfirstNameを取得しようとしますが、何か間違っています。ユーザーがnullであるか、getFirstName()メソッドを持たないか、メソッド内で何かが失敗する可能性があります。 – StanislavL

+0

私はこのエラーに対処することができました。私はコンソールに正しいデータを取得しますが、私のモーダルウィンドウは何らかの理由で空です。これのための解決策を見つけるのを助けてくれますか?私はコードを更新しました、それを確認してください。私はこのチュートリアルを使用しようとしています。https://qtzar.com/2017/03/24/ajax-and-thymeleaf-for-modal-dialogs/ –

答えて

0

$( '#viewUser).modal()を追加することで問題を解決しました。モーダルウィンドウは開きたくありませんでした。

function viewUser(id) { 
     $.ajax({ 
      url: "https://stackoverflow.com/users/view/" + id, 
      success: function(response) { 
       $('#viewUserHolder').html(response); 
       $('#viewUser').modal(); 
       $('#viewUser').modal('open'); 
      } 
     }); 
    } 
関連する問題