Ajaxについての質問です。
現在、SAStrutsを使用してWebアプリケーションを作っているのですが、
JSPページから値を送信して、データベースからの結果をAjaxで表示したいと考えております。
データベースからの値を取得するだけなら、なんとかできたのですが、
JSP → JAVA への値の受け渡しがうまくいきません。
データベースから値を取得するだけのコードに
どのような記述を付け加えていけばよいでしょうか。
ご回答をよろしくお願い致します。
```JSP <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel="stylesheet" type="text/css" href="${f:url('/css/global.css')}"/> <script src="${f:url('/js/jquery-1.4.2.min.js')}"></script> </head> <body> <html:errors/> <script type="text/javascript"> function loadJSON() { var val = ''; <!-- textの値を変数に入れることができればOK --> $.ajax ({ url: 'http://localhost:8080/Practice/emp/load_json', type: 'POST', dataType: 'json', cache: false, data :{'testKey': val}, <!-- 変数に入れることができた。--> error: function(){ alert("jsonファイルの読み込みに失敗しました"); }, success: function(json){ var row = document.createElement("tr"); var col = document.createElement("td"); row.appned console.log("ファイルの取得に成功しました"); $("#response").html("個数:" + json.length + "<br/>"); for( var i=0; i<json.length; i++ ) { $("#response").append("<table border=\"1\" width=\"600px\" align=\"center\"> <tr> <td>" + json[i].name + "</td> </td><br/>").html(); } } }); } </script> <s:form> <table class="tablebg" width="600px" align="center"> <tr> <td width="80%"> Todoを検索する </td> <td rowspan="2"> <input type="button" value=" 検索 " onclick="loadJSON()"/><br/> </td> </tr> <tr> <td width="80%"> <html:text property="name" value="" size="65%" /> </td> </tr> </table> </s:form> <br/><br/> <div id="response"></div> </body> </html>
###該当のソースコード
JAVA
1public class EmpAction { 2 3 public List<Emp> empItems; 4 5 @ActionForm 6 @Resource 7 protected EmpForm empForm; 8 9 @Resource 10 protected EmpService empService; 11 12 13 public JdbcManager jdbcManager; 14 15 @Execute( validator = false ) 16 public String load_json() throws IOException { 17 18 empItems = empService.findAll(); 19 HttpServletResponse httpServletResponse = ResponseUtil.getResponse(); 20 httpServletResponse.setContentType("application/json"); 21 PrintWriter sendPoint = new PrintWriter(httpServletResponse.getOutputStream()); 22 23 sendPoint.println(JSON.encode(empItems)); 24 sendPoint.flush(); 25 sendPoint.close(); 26 27 return null; 28 } 29 30} 31

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/26 14:06
2017/05/29 01:47 編集