Last active
September 26, 2023 00:13
-
-
Save Cartman0/f05fe43744c8daccd8f9 to your computer and use it in GitHub Desktop.
Java MVCのサンプル。「ID入力画面」を操作する。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mvc; | |
import java.io.IOException; | |
import javax.servlet.RequestDispatcher; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
/** | |
* MVCモデルクラス.<br> | |
* C(コントローラ) | |
*/ | |
@WebServlet("/ControllerServlet") | |
public class ControllerServlet extends HttpServlet { | |
private static final long serialVersionUID = 1L; | |
/** | |
* コンストラクタ. | |
*/ | |
public ControllerServlet() { | |
super(); | |
} | |
/** | |
* POSTメソッドでリクエストされた場合の処理. | |
* | |
* @param request | |
* @param response | |
* @throws javax.servlet.ServletException | |
* @throws java.io.IOException | |
*/ | |
@Override | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
// ①ID入力画面で入力されたIDを取得 | |
String id = request.getParameter("id"); | |
// ②IDに紐づくユーザ情報をモデルに格納するために指示 | |
// ②-1 ID処理クラスをインスタンス化 | |
IdProcessing ip = new IdProcessing(); | |
// ②-2 ID処理クラスに①で取得したIDを渡してユーザ情報をモデルに格納 | |
UserBean bean = ip.getUserData(id); | |
// ③ビューに画面を表示させるための準備 | |
RequestDispatcher rd; | |
// ③-1 モデルの情報を判定 | |
if (bean != null) { | |
// ③-1-1 モデルの情報が存在する場合 | |
// ③-1-1-1 setAttributeメソッドを使ってモデルの情報をセット | |
request.setAttribute("user", bean); | |
// ③-1-1-2 つぎに表示させる画面(ビュー)を指定 | |
rd = request.getRequestDispatcher("./View/userResponse.jsp"); | |
} else { | |
// ③-1-2 モデルの情報が存在しない(IDに紐づくユーザ情報がない)場合 | |
// ③-1-2-1 つぎに表示させる画面(ビュー)を指定 | |
rd = request.getRequestDispatcher("./View/userError.jsp"); | |
} | |
// ③-2 つぎの画面を表示 | |
rd.forward(request, response); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mvc; | |
/** | |
* ID処理クラス | |
* ModelとControllerの間 | |
*/ | |
public class IdProcessing { | |
/** | |
* 指定されたIDに紐づくユーザ情報を返却します. | |
* | |
* @param id ID | |
* @return ユーザ情報 | |
*/ | |
public UserBean getUserData(String id) { | |
UserBean bean; | |
// 引数のIDを判定 | |
if ("id01".equals(id)) { | |
// IDがid01の場合 | |
// Beanに名前を設定 | |
String name = "Taro Cartman"; | |
// Beanに年齢を設定 | |
byte age = 17; | |
/* | |
Beanに | |
ID, 名前, 年齢 | |
を設定 | |
*/ | |
bean = new UserBean(id, name, age); | |
return bean; | |
} else if ("id02".equals(id)) { | |
// IDがid02の場合 | |
String name = "Jiro Cartman"; | |
// Beanに年齢を設定 | |
byte age = 10; | |
/* | |
Beanに | |
ID, 名前, 年齢 | |
を設定 | |
*/ | |
bean = new UserBean(id, name, age); | |
return bean; | |
} | |
// IDが合致しない場合 | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mvc; | |
import java.io.Serializable; | |
/** | |
* Model | |
*/ | |
public class UserBean implements Serializable { | |
private static final long serialVersionUID = 1L; | |
private String id_; // ID | |
private String name_; // 名前 | |
private byte age_; // 年齢 -128 - 128 | |
/** | |
* コンストラクタ. メンバ変数の値を初期化. | |
*/ | |
public UserBean() { | |
this.id_ = ""; | |
this.name_ = ""; | |
this.age_ = 0; | |
} | |
public UserBean(String id, String name, byte age) { | |
this.id_ = id; | |
this.name_ = name; | |
this.age_ = age; | |
} | |
/** | |
* IDを返却します. | |
* | |
* @return ID | |
*/ | |
public String getId() { | |
return this.id_; | |
} | |
/** | |
* IDを設定します. | |
* | |
* @param id | |
*/ | |
public void setId(String id) { | |
this.id_ = id; | |
} | |
/** | |
* 名前を返却します. | |
* | |
* @return 名前 | |
*/ | |
public String getName() { | |
return this.name_; | |
} | |
/** | |
* 名前を設定します. | |
* | |
* @param name 名前 | |
*/ | |
public void setName(String name) { | |
this.name_ = name; | |
} | |
/** | |
* 年齢を返却します. | |
* | |
* @return 年齢 | |
*/ | |
public int getAge() { | |
return this.age_; | |
} | |
/** | |
* 年齢を設定します. | |
* | |
* @param age 年齢 | |
*/ | |
public void setAge(byte age) { | |
this.age_ = age; | |
} | |
/** | |
* Userをset | |
* | |
* @param id | |
* @param name | |
* @param age | |
*/ | |
public void setUser(String id, String name, byte age) { | |
this.setId(id); | |
this.setName(name); | |
this.setAge(age); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ page language="java" contentType="text/html; charset=UTF-8" %> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Error User</title> | |
</head> | |
<body> | |
<h1>Error User</h1> | |
<p>入力されたユーザは存在しません.</p> | |
<form> | |
<input type="button" onclick="location.href = './View/userRequest.jsp'" value="戻る"> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ page language="java" contentType="text/html; charset=UTF-8" %> | |
<%-- MVCモデル V(ビュー) --%> | |
<%-- ID入力画面 --%> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Input ID</title> | |
</head> | |
<body> | |
<h1>Input ID</h1> | |
<p>インプットフォーム</p> | |
<%-- POSTTメソッドでテキストを送信 --%> | |
<form action="../ControllerServlet" method="post"> | |
<p>IDを入力してください:<input type="text" name="id"></p> | |
<p>IDはid01 or id02です</p> | |
<input type="submit" value="request"> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@page language="java" contentType="text/html; charset=UTF-8" %> | |
<jsp:useBean id="user" scope="request" class="mvc.UserBean" /> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Request Result</title> | |
</head> | |
<body> | |
<p>リクエスト結果</p> | |
<p>ID:<%=user.getId()%></p> | |
<p>名前:<%=user.getName()%></p> | |
<p>年齢:<%=user.getAge()%></p> | |
<form> | |
<input type="button" onclick="location.href = './View/userRequest.jsp'" value="戻る"> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment