try {
……エンティティの保存処理……
} catch(ConstraintViolationException e){
……エラー時の処理……
}<%
request.setCharacterEncoding("utf-8");
List list = (List)request.getAttribute("list");
MyDataEntity entity = (MyDataEntity)request.getAttribute("entity");
String msg = (String)request.getAttribute("msg");
%>
<p><%=msg %></p>
<form method="post" action="/hello">
NAME:<input type="text" name="name" value="<%= entity.getName() %>">
MAIL:<input type="text" name="mail" value="<%= entity.getMail() %>">
TEL :<input type="text" name="tel" value="<%= entity.getTel() %>">
<input type="submit">
</form>
<ul>
<% for(Object obj : list) { %>
<li><%=obj %></li>
<% } %>
</ul>
try {
manager.persist(entity);
manager.getTransaction().commit();
} catch(ConstraintViolationException e){
List list = getAllEntity();
request.setAttribute("msg", "<b>Validation Error!</b><br>");
request.setAttribute("list", list);
request.setAttribute("entity", entity);
RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
dispatcher.forward(request,response);
}※リストが表示されない場合
AddBlockなどの広告ブロックツールがONになっているとリストなどが表示されない場合があります。これらのツールをOFFにしてみてください。
package com.tuyano.libro;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Set;
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;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
@WebServlet("/hello")
public class MyDataEntityServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected EntityManagerFactory factory =
Persistence.createEntityManagerFactory("mydataentity-persistance");
@Override
public void init() throws ServletException {
super.init();
EntityManager manager = factory.createEntityManager();
try {
manager.getTransaction().begin();
manager.persist(
new MyDataEntity("taro", "taro@yamada", "090-999-999")
);
……必要なだけダミーエンティティを追加……
manager.getTransaction().commit();
} finally {
manager.close();
}
}
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
List list = getAllEntity();
request.setAttribute("msg", "input Entity:");
request.setAttribute("list", list);
request.setAttribute("entity", new MyDataEntity("noname", "", ""));
RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
dispatcher.forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String mail = request.getParameter("mail");
String tel = request.getParameter("tel");
EntityManager manager = factory.createEntityManager();
MyDataEntity entity = new MyDataEntity(name, mail, tel);
manager.getTransaction().begin();
try {
manager.persist(entity);
manager.getTransaction().commit();
} catch(ConstraintViolationException e){
List list = getAllEntity();
request.setAttribute("msg", "<b>Validation Error!</b><br>");
request.setAttribute("list", list);
request.setAttribute("entity", entity);
RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
dispatcher.forward(request,response);
}
response.sendRedirect("/hello");
}
private List getAllEntity() {
EntityManager manager = factory.createEntityManager();
Query query = manager.createQuery("select e from MyDataEntity e");
return query.getResultList();
}
}
| << 前へ | 次へ >> |