この画像のコメント入力欄の下に,コメントした履歴を出力できません、JSTLのcoreタグで書いているはずなのに、なぜ出力されないのでしょうか?
Main.java(servlet)のコードです。追加しました。どうぞよろしくお願いします。
java
1package servlet; 2 3 4import java.util.List; 5import javax.servlet.RequestDispatcher; 6 7import java.io.IOException; 8 9 10import javax.servlet.ServletException; 11import javax.servlet.annotation.WebServlet; 12import javax.servlet.http.HttpServlet; 13import javax.servlet.http.HttpServletRequest; 14import javax.servlet.http.HttpServletResponse; 15import javax.servlet.http.HttpSession; 16 17 18import model.GetMutterListLogic; 19import model.Mutter; 20import model.User; 21import model.PostMutterLogic; 22 23/** 24 * Servlet implementation class Main 25 */ 26@WebServlet("/Main") 27public class Main extends HttpServlet { 28 29 private static final long serialVersionUID = 1L; 30 31 /** 32 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 33 */ 34 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 35 36 //つぶやきリストを取得して、リクエストスコープに保存 37 GetMutterListLogic getMutterListLogic=new GetMutterListLogic(); 38 List<Mutter>mutterList=getMutterListLogic.execute(); 39 request.setAttribute("mutterList", mutterList); 40 41 42 43 //ログインしているか確認するため 44 //セッションスコープからユーザー情報を取得 45 HttpSession session=request.getSession(); 46 User loginUser=(User)session.getAttribute("loginUser"); 47 48 if(loginUser==null){//ログインしていない場合 49 //リダイレクト 50 response.sendRedirect("/docoTsubuu/"); 51 }else{//ログイン済みの場合 52 //フォワード 53 RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/main.jsp"); 54 dispatcher.forward(request, response); 55 } 56 57 } 58 protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{ 59 //リクエストパラメータの取得 60 request.setCharacterEncoding("UTF-8"); 61 String text=request.getParameter("text"); 62 63 //入力値チェック 64 if(text!=null&&text.length()!=0){ 65 66 //セッションスコープに保存されたユーザー情報を取得 67 HttpSession session=request.getSession(); 68 User loginUser=(User)session.getAttribute("loginUser"); 69 70 //つぶやきをつぶやきリストに追加 71 Mutter mutter=new Mutter(loginUser.getName(),text); 72 PostMutterLogic postMutterLogic=new PostMutterLogic(); 73 postMutterLogic.execute(mutter); 74 75 //つぶやきリストを習得して、リクエストスコープに保存 76 GetMutterListLogic getMutterListLogic=new GetMutterListLogic(); 77 List<Mutter> mutterList=getMutterListLogic.execute(); 78 request.setAttribute("mutterList", mutterList); 79 80 81 }else{ 82 //エラーメッセージをリクエストスコープに保存 83 84 request.setAttribute("errorMsg", "つぶやきが入力されていません"); 85 } 86 //メイン画面にフォワード 87 RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/main.jsp"); 88 dispatcher.forward(request, response); 89 90 // TODO Auto-generated method stub 91 response.getWriter().append("Served at: ").append(request.getContextPath()); 92 } 93 94} 95
main.jspのコードです。
java
1<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5<html> 6<head> 7<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8<title>どこつぶ</title> 9</head> 10<body> 11<h1>どこ粒メイン</h1> 12<p> 13<c:out value="${loginUser.name}"/>さん、ログイン中 14<a href="/docoTsubuu/Logout">ログアウト</a> 15</p> 16<p><a href="/docoTsubuu/Main">更新</a></p> 17<form action="/docoTsubuu/Main"method="post"> 18<input type="text"name="text"> 19<input type="submit" value="つぶやく"> 20</form> 21<c:if test="${not empty errorMsg}"> 22<p>${errorMsg}</p> 23</c:if> 24<c:forEach var="mutter" items="${mutterList}"> 25<p><c:out value="${mutter.userName}"/>: 26<c:out value="${mutter.text}"/></p> 27</c:forEach> 28</body> 29</html> 30
MutterDAOです
java
1package dao; 2 3import java.sql.Connection; 4import java.sql.DriverManager; 5import java.sql.PreparedStatement; 6import java.sql.ResultSet; 7import java.sql.SQLException; 8import java.util.ArrayList; 9import java.util.List; 10 11import model.Mutter; 12 13 14public class MutterDAO { 15 16 private final String DRIVER_NAME="org.h2.Driver"; 17 private final String JDBC_URL="jdbc:h2:file:/User/taichi/Documents/networkspace/docoTsubuu/h2data.dat"; 18 private final String DB_USER="sa"; 19 private final String DB_PASS=""; 20 21 public List<Mutter> findAll(){ 22 Connection conn=null; 23 List<Mutter>mutterList=new ArrayList<Mutter>(); 24 try{ 25 Class.forName(DRIVER_NAME); 26 conn=DriverManager.getConnection(JDBC_URL,DB_USER,DB_PASS); 27 28 //SELECT文の準備 29 String sql="SELECT ID,NAME,TEXT FROM MUTTER ORDER BY ID DESC"; 30 PreparedStatement pStmt=conn.prepareStatement(sql); 31 32 //SELECTを実行 33 ResultSet rs=pStmt.executeQuery(); 34 35 //SELECT文の結果をArrayList二格納 36 while(rs.next()){ 37 int id=rs.getInt("ID"); 38 String userName=rs.getString("NAME"); 39 String text=rs.getString("NAME"); 40 Mutter mutter=new Mutter(id,userName,text); 41 mutterList.add(mutter); 42 } 43 }catch(SQLException e){ 44 e.printStackTrace(); 45 return null; 46 }catch(ClassNotFoundException e){ 47 e.printStackTrace(); 48 return null; 49 }finally{ 50 //データベース切断 51 if(conn!=null){ 52 try{ 53 conn.close(); 54 }catch(SQLException e){ 55 e.printStackTrace(); 56 return null; 57 } 58 } 59 } 60 return mutterList; 61 } 62 public boolean create(Mutter mutter){ 63 Connection conn=null; 64 try{ 65 //データベースへ接続 66 conn=DriverManager.getConnection(JDBC_URL,DB_USER,DB_PASS); 67 68 //INSERT文の準備(idは自動連番なので指定しなくてもいい) 69 String sql="INSERT INTO MUTTER(NAME,TEXT)VALUES(?,?)"; 70 PreparedStatement pStmt=conn.prepareStatement(sql); 71 72 //INSERT文中の「?」二使用する値を設定しSQLを完成 73 pStmt.setString(1, mutter.getUserName()); 74 pStmt.setString(2, mutter.getText()); 75 76 //INSERT文を実行 77 int result=pStmt.executeUpdate(); 78 79 if(result !=1){ 80 return false; 81 } 82 }catch(SQLException e){ 83 e.printStackTrace(); 84 return false; 85 }finally{ 86 //データベース切断 87 if(conn!=null){ 88 try{ 89 conn.close(); 90 }catch(SQLException e){ 91 e.printStackTrace(); 92 } 93 } 94 } 95 return true; 96 } 97} 98
回答1件
あなたの回答
tips
プレビュー