前提・実現したいこと
test01.jspから<a href="./Test02">とリンクする場合と<a href="./Test02?id=foo">でリンクする場合
test02.jsp側でidがあるか無いか判断して、その分岐で表示内容を変えたい
test02.jspでどのようにURLを判断すればよろしいですか
また"?id=foo"この部分の名称がわからないので教えてください
Test02.javaではidの有無により処理を分岐しています
idがある場合はtest02.jspでそのIDに対する中身を表示できているのですがIDがない場合は何も表示されなくて困ってます
該当のソースコード
test01jsp
1<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 3<!DOCTYPE html> 4<html> 5<head> 6<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7<script type="text/javascript" src="../js/dialog.js" charset="UTF-8"> 8</script> 9<title>test</title> 10<script type="text/javascript"> 11<!-- 12 13function deleteEmp(info.id) { 14 if(window.confirm('OK')){ 15 location.href = "./Delete?id=" + id; 16 } 17} 18 19// --> 20</script> 21 22</head> 23<body> 24<a href="./Register">登録</a><br> 25<h1>一覧</h1> 26<table border="1"> 27<tr><th>No</th><th>会社名</th><th>社員名</th><th>社員ID</th><th>性別</th><th>勤務形態</th><th>詳細</th><th>削除</th></tr> 28<c:forEach var="info" items="${en}" > 29 <tr> 30 <td><c:out value="${info.id}" /></td> 31 <td><a href="./Test02?ed=${info.id}">中身</a></td> 32 <td><p><a onClick="deleteEmp('${info.id}')">削除</a></p></td> 33 </tr> 34</c:forEach> 35</table> 36</body> 37</html>
test02jsp
1<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 3<!DOCTYPE html> 4<html> 5<head> 6<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7<script type="text/javascript" src="../js/dialog.js" charset="UTF-8"> 8</script> 9<title>Test</title> 10<script type="text/javascript"> 11<!-- 12 13function register() { 14 if(window.confirm('登録')){ 15 return true; 16 } 17 else{ 18 return false; 19 } 20} 21 22// --> 23</script> 24 25</head> 26<body> 27<h1>Test</h1> 28<p>詳細</p> 29<form action = "./Test02" method = "post" onSubmit="return register()" > 30<font color="red">tet1</font> 31<c:forEach var="info" items="${en}" varStatus="status"> 32<font color="red">tet2</font> 33<input type="hidden" name="empId" value="${info.id}" > 34<font color="red">tet3</font> 35<table border = 1> 36<tr><td>name</td><td><input type = "text" name = "name" value="${info.name}" ></td></tr> 37</table> 38</c:forEach> 39<input type = "submit" value = "toroku"> 40<a href="./Test01"> <button type="button">modoru</button></a><br> 41</form> 42</body> 43</html>
Test02java
1@WebServlet(name = "Test02", urlPatterns = {"/Register"}) 2public class Register extends HttpServlet { 3 private static final long serialVersionUID = 1L; 4 5 6 @Override 7 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 8 HttpSession session = request.getSession(); 9 request.setCharacterEncoding("UTF-8"); 10 if(request.getParameter("eid") != null){ 11 int id = Integer.parseInt(request.getParameter("id")); 12 Dao dao = new Dao(); 13 ArrayList<Info> Info = dao.selectid(id); 14 request.setAttribute("en", Info); 15 } 16 17 18 request.getRequestDispatcher("/WEB-INF/jsp/test02.jsp").forward(request, response); 19 } 20 21 @Override 22 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 23 //省略 24 } 25 26}
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/28 07:55