데이터베이스에서 데이터를 가져 와서 웹 페이지에 표시하려고합니다.
엔티티 데이터가있는 테이블을 예상했지만 다음과 같습니다.
다음 수업이 있습니다.
학과:
package entity;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name = "DEPT")
@NamedQuery(name = "Department.getAll",
query = "select d from Department d")
public class Department implements Serializable {
@Id
@Column(name = "DEPTNO")
private int DEPTNO;
@Column(name = "DNAME")
private String dname;
@Column(name = "LOC")
private String loc;
public Department() {
}
public int getDEPTNO() {
return DEPTNO;
}
public void setDEPTNO(int DEPTNO) {
this.DEPTNO = DEPTNO;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Department that = (Department) o;
if (DEPTNO != that.DEPTNO) return false;
if (!dname.equals(that.dname)) return false;
return loc.equals(that.loc);
}
@Override
public int hashCode() {
int result = DEPTNO;
result = 31 * result + dname.hashCode();
result = 31 * result + loc.hashCode();
return result;
}
@Override
public String toString() {
return "Department{" +
"DEPTNO=" + DEPTNO +
", dname='" + dname + '\'' +
", loc='" + loc + '\'' +
'}';
}
}
부서 서비스 :
public class DepartmentService {
public EntityManager entityManager = Persistence.createEntityManagerFactory("persistenceUnit").createEntityManager();
public List<Department> getAll(){
TypedQuery<Department> typedQuery = entityManager.createNamedQuery("Department.getAll", Department.class);
return typedQuery.getResultList();
}
}
ShowAllServlet :
@WebServlet(name = "ShowAllServlet", urlPatterns = "/showAll")
public class ShowAllServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
DepartmentService departmentService = new DepartmentService();
req.setAttribute("result", departmentService.getAll());
}
}
그리고 jsp index.jsp
<%@ page import="entity.Department" %>
<%@ page import="java.util.ArrayList" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<div class="main">
<jsp:include page="/showAll"></jsp:include>
<table id="mainTable">
<tr>
<th>DEPTNO</th>
<th>DNAME</th>
<th>LOC</th>
</tr>
<%--@elvariable id="result" type="java.util.List"--%>
<c:forEach items="${result}" var="obj">
<tr>
<td>
<c:out value="${obj.DEPTNO}"></c:out>
</td>
<td>
<c:out value="${obj.dname}"></c:out>
</td>
<td>
<c:out value="${obj.loc}"></c:out>
</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
확인했고 요청에 "결과"속성이 있습니다.
JSP 아주 오래된 버전을 사용하고있는 것 같습니다. 서블릿에서 주석을 사용하는 한 웹 서버에서 사용할 수 있어야하는 최소한 Servlet 3.0 라이브러리를 사용해야합니다.
If you have web.xml
check the version in the header tag for the correct Servlet version, that should be at least 2.4. If you have a question why you should use it because this version and higher enables EL by default using isELIgnored="false"
. You can modify the page if you have a requirement to ignore EL on all pages with exception to use EL on that page.
<%@ page isELIgnored ="false" %>
If you have any libraries supplied with the web application that has implemented servlet, but lower version you should remove them.
If you are using pom.xml
specify scope of the library, that is available on the server as provided
.
Use the version of JSTL that can be used with the Servlet version of the web application. You can download JSTL using Can not find the tag library descriptor for “http://java.sun.com/jsp/jstl/core” answer.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다