4.JSPを使用してバックグラウンドで取得したデータを展示する(添削・改ざん)

18754 ワード

前回のプロジェクト(1)に続き、すべてのデータ修正HelloController:4.使用JSP展示后台取到的数据(增删改查)_第1张图片がデータベースからすべてのレコードを取得し、バックグラウンドで取得できるようにパラメータを設定します.変更jsp
"java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
"c"  uri="http://java.sun.com/jsp/jstl/core"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>

      <a href="${pageContext.request.contextPath}/hello/save">  a>
      <table>
         <tr>
            <td>idtd>
            <td>nametd>
            <td>agetd>
            <td>passwordtd>
            <td>  td>
         tr>
         <c:forEach items="${users}" var="l">
         <tr>
            <td>${l.id}td>
            <td>${l.name}td>
            <td>${l.age}td>
            <td>${l.password}td>
            <td>
            <a href="${pageContext.request.contextPath}/hello/delete/${l.id}">  a>
            <a href="${pageContext.request.contextPath}/hello/update/${l.id}">  a>
            td>
         tr>
         c:forEach>
      table>
body>
html>

アクセスhttp://localhost:8080/jfinal_demo02/hello/hello 这里写图片描述
(2)追加データ機能を追加HelloControllerに追加する方法:4.使用JSP展示后台取到的数据(增删改查)_第2张图片
(1)public void save(){(2)User=getModel(User.class,"");////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////getであれば新規ページ(5)render("save.jsp");;(6)}else{(7)if(user.get("id")!=null){{(=null){{((((((((=null)){{((((((((((((((((((((((((((((8)System.out.println(user);(9) user.save();//POST要求であれば、取得したフォームデータ(10)}(11)redirect("/hello/hello")を挿入する.(12)}(13)(14)}hello.jspは新しいリンクを追加し、HelloControllerのsave:4.使用JSP展示后台取到的数据(增删改查)_第3张图片に対応して新しいsaveを作成する.jspページ:这里写图片描述
"java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
   <form action="" method="post">
      id:<input type="text" name="id" />
      name:<input type="text" name="name" />
      age:<input type="text" name="age" />
      password:<input type="text" name="password" />
      <input type="submit" value="submit" />
   form>
body>
html>

アクセスhttp://localhost:8080/jfinal_demo02/hello/hello 4.使用JSP展示后台取到的数据(增删改查)_第4张图片
追加ボタンをクリックしてgetリクエストに対応するsave.jspページ:这里写图片描述クリックコミット、これはpostリクエスト、バックグラウンドにデータを追加してhello.jsp 4.使用JSP展示后台取到的数据(增删改查)_第5张图片(3)削除機能と修正機能を追加hello.jsp、リンクの後ろにidパラメータが入ってくることに注意して、バックグラウンドはこのパラメータを取得して修正して更新します
"java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
"c"  uri="http://java.sun.com/jsp/jstl/core"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>

      <a href="${pageContext.request.contextPath}/hello/save">  a>
      <table>
         <tr>
            <td>idtd>
            <td>nametd>
            <td>agetd>
            <td>passwordtd>
            <td>  td>
         tr>
         <c:forEach items="${users}" var="l">
         <tr>
            <td>${l.id}td>
            <td>${l.name}td>
            <td>${l.age}td>
            <td>${l.password}td>
            <td>
            <a href="${pageContext.request.contextPath}/hello/delete/${l.id}">  a>
            <a href="${pageContext.request.contextPath}/hello/update/${l.id}">  a>
            td>
         tr>
         c:forEach>
      table>
body>
html>

HelloControllerで更新と削除方法を追加します.
 public void delete(){
         int id=getParaToInt();
         User.dao.deleteById(id);
         redirect("/hello/hello");
     }

     public void update(){

          Integer id = getParaToInt();
            String method = getRequest().getMethod();
            if (method.equalsIgnoreCase("get")) {
                setAttr("id", id);
                User user = User.dao.findById(id);
                setAttr("users", user);
                 render("update.jsp");
            } else if (method.equalsIgnoreCase("post")) {
                User user= getModel(User.class,"");
                user.update();
                redirect("/hello/hello");
            }
     }

updateを追加します.jsp 4.使用JSP展示后台取到的数据(增删改查)_第6张图片 Update.jspの内容は次のとおりです:4.使用JSP展示后台取到的数据(增删改查)_第7张图片アクセス:http://localhost:8080/jfinal_demo02/hello/hello4.使用JSP展示后台取到的数据(增删改查)_第8张图片 4.使用JSP展示后台取到的数据(增删改查)_第9张图片クリック3番削除:4.使用JSP展示后台取到的数据(增删改查)_第10张图片修正2番:这里写图片描述提出後展示ページ:4.使用JSP展示后台取到的数据(增删改查)_第11张图片