4.JSPを使用してバックグラウンドで取得したデータを展示する(添削・改ざん)
18754 ワード
前回のプロジェクト(1)に続き、すべてのデータ修正HelloController:がデータベースからすべてのレコードを取得し、バックグラウンドで取得できるようにパラメータを設定します.変更jsp
アクセスhttp://localhost:8080/jfinal_demo02/hello/hello
(2)追加データ機能を追加HelloControllerに追加する方法:
(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:に対応して新しいsaveを作成する.jspページ:
アクセスhttp://localhost:8080/jfinal_demo02/hello/hello
追加ボタンをクリックしてgetリクエストに対応するsave.jspページ:クリックコミット、これはpostリクエスト、バックグラウンドにデータを追加してhello.jsp (3)削除機能と修正機能を追加hello.jsp、リンクの後ろにidパラメータが入ってくることに注意して、バックグラウンドはこのパラメータを取得して修正して更新します
HelloControllerで更新と削除方法を追加します.
updateを追加します.jsp Update.jspの内容は次のとおりです:アクセス:http://localhost:8080/jfinal_demo02/hello/hello クリック3番削除:修正2番:提出後展示ページ:
"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に追加する方法:
(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:に対応して新しい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
追加ボタンをクリックしてgetリクエストに対応するsave.jspページ:クリックコミット、これはpostリクエスト、バックグラウンドにデータを追加してhello.jsp (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 Update.jspの内容は次のとおりです:アクセス:http://localhost:8080/jfinal_demo02/hello/hello クリック3番削除:修正2番:提出後展示ページ: