ajax html

jQuery

Ajax (html)

View (list_ajax.jsp)

<%
  Connection conn = null;
  Statement stmt = null;
  try{
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/text","root","1234");
    stmt = conn.createStatement();

    String sql;
    sql = "SELECT * FROM test order by idx desc";
    ResultSet rs = stmt.executeQuery(sql);

    Map<String,Object> object = null;
    List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();

    while(re.next()){
      int idx=rs.getInt("idx");
      String content = rs.getString("Content");
      object = new HashMap<String,Object>();
      object.put("idx",idx);
      object.put("content",content);
      list.add(object);
      request.setAttribute("list", list);
    }
    rs.close();
  }catch(Exception e){
    e.printStackTrace();
  }finally{
    try{if(stmt!=null) {stmt.close();}}catch(SQLException e){}
    try{if(conn!=null) {conn.close();}}catch(SQLException e){}
  }
%>

<c:choose>
  <c:when test="${fn:length(list) > 0}">
    <c:foreach items="${list }" var="list">
      <tr>
        <td width="30%">
          ${list.idx}
        </td>
        <td>
          ${list.content}
        </td>
      </tr>
    <c:forEach>
  </c:when>
  <c:otherwise>
    <tr>
      <td colspan="2">
        no data
      </td>
    </tr>
  </c:otherwise>
</c:choose>


ajax.html

<html>
<head> 
<script type="text/javascript" src="/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
  $(function(){
    $.ajax({
      // form action(url), type(method)
      // ajax needs data type even though form doesn't need.
      url : '/list_ajax.jsp',
      type : 'GET',
      dataType : 'html',
      /* data : {}, */
      success : function(data){
        $("table").html(data);
      }
    });

    $("#submitButton").click(function()){
      $.ajax({
        // form action(url), type(method)
        // ajax needs data type even though form doesn't need.
        url : '/insert_ajax.jsp',
        type : 'POST',
        dataType : 'html',
        //parameter
        //data : {content : $("input[name=content]").val()}, 
        // to send a form, name value is must-have
        data : $("form").serialize();
        success : function(data){
          $("table").html(data);
        }      
    })
  })
</script>
</head>
<body>
  <form action="/insert.jsp" method="post" id="frm">
    <input type="text" name="content" />
    <input type="button" id="submitButton" value="submit" />
  </form>
  <table width="100%" border="1"></table>
<body>
</html>

Server side (/insert_ajax.jsp)

<%
  String content = request.getParameter("content");
  Connection conn = null;
  Statement stmt = null;
  try{
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/text","root","1234");
    String sql = "insert into test(content) values(?)";
    pstmt = conn.createStatement();
    pstmt.setString(1,content);
    pstmt.executeUpdate();
  }catch(Exception e){
    e.printStackTrace();
  }finally{
    try{if(pstmt!=null) {stmt.close();}}catch(SQLException e){}
    try{if(conn!=null) {conn.close();}}catch(SQLException e){}
  }

response.sendRedirect("/list_ajax.jsp");
%>

Continue reading

Traversing

jQuery

Traversing

<script>
$(function(){
    // parent(), find(), prev(), next()
    // when you want to find child -> find()
    $("table").find("#txt").val(); // value
    $("table").find("tr:eq(0)").find("td:eq(0)").find("#txt").val(); // value
    console.log("txt",txt);
    // when you want to find parent -> parseInt
    $("table").find("#txt").parent().val(); // <td>
    // when you want to find next (same level) -> next()
    $("table").find("#txt").parent().next().find(".txt:eq(1)").val(); // value2
    // when you want to find previous (same level) -> prev()
    $("table").find("#txt:eq(3)").prev().val(); // value2
  });
</script>
<body>
  <table>
    <tr>
      <td>
        <input type="text" id="txt" value="value"/>
      </td>
      <td>
        <input type="text" id="txt" value="value1"/>
        <input type="text" id="txt" value="value2"/>
        <input type="text" id="txt" value="value3"/>
        <input type="text" id="txt" value="value4"/>
      </td>
    </tr>
  </table>
</body>
<head>
<style type="text/css">
  .sample1{
    color : red;
    font-weight : bold;
  }
  .sample2{
    color : green;
  }
</style>
</head>
<script>
$(function(){
    //addClass("ClassName") add className to selected selector
    $("#sampleDiv1").addClass("sample2");
    //removeClass("ClassName") delete className from selected selector
    $("#sampleDiv1").removeClass("sample2");
    //hasclass("ClassName") check whether selected selector has className (true/false)
    let flag = $("#sampleDiv1").hasClass("sample2");
    console.log(flag);
  });
</script>
<body>
  <div id="sampleDiv">control style1</div>
  <div id="sampleDiv2">control style2</div>
</body>

Continue reading

selector

jQuery

Selector

<script>
$(function(){
    $("#xxx").val(); //  id
    $(".xxx").val(); //  class
    $("input[class=text]") // class = text1
    $("input[name=textName1]") // name=textName1
  });
</script>
<body>
  <input type="text" name="textName1" class="text1" value="text1" /></br>
  <input type="text" name="textName2" class="text2" value="text2" /></br>
</body>
<script>
$(function(){
    let checkedRadio = $("input[name=rdo]:checked").val(); // name=textName1
    console.log(checkedRadio); // print second
  });
</script>
<body>
  <input type="radio" name="rdo" checked="checked" value="first" /></br>
  <input type="radio" name="rdo" value="second" /></br> // selected
</body>
<script>
$(function(){
    //  $("div:eq(0)") = $("div").eq(0) 
    let class2 = $(".class2:eq(1)").html(); // eq means sequence (starts from 0)
    console.log(class2); // print "value2"
  });
</script>
<body>
  <div class="class2">
    <h2>value1</h2>
  </div>
  <div class="class2">
    <h2>value2</h2>
  </div>
</body>

Continue reading

Pagination


© 2017. by isme2n

Powered by aiden