【问题标题】:Can't display the data from SQL in the correct order, HTML JSP无法以正确的顺序显示来自 SQL 的数据,HTML JSP
【发布时间】:2014-05-19 21:05:04
【问题描述】:

我目前正在处理一个小组项目,我们应该处理 Building 和 Room 之间的关系。该关系是 OnetoMany,我们目前能够显示来自 sql 的相关数据,但我们无法按预期将其显示在美观的电子表格或表格中。我们希望将建筑物和房间分类成一张桌子,每个房间都显示与相关建筑物的连接。我们如何将我们的 HTML 代码放在我们的 (ShowRooms.jsp) jsp 中以获取用户选择的建筑物的所有房间,然后将该建筑物的所有房间显示在一个好看的表格中?在这种状态下,我们从我们的 sql 数据库中获取数据,但只是在一条直线上,而不是通过将每个房间连接到相关建筑物的表格。提前致谢!

这是我们的代码:BUILDING:

@Entity
@Table(name = "Building")
public class Building {
private String bname;
private List<Room> rooms; // Building can have many Rooms

@Id
@Column(name = "Bname")
public String getBname() {
return bname;
}

public void setBname(String bname) {
this.bname = bname;
}

@OneToMany(mappedBy = "building", fetch = FetchType.EAGER)
public List<Room> getRooms() {
return rooms;
}

public void setRooms(List<Room> rooms) {
this.rooms = rooms;
}   
}

房间:

@NamedQueries({
@NamedQuery(name="Room.findByBname",
query="SELECT r FROM Room r WHERE r.bname LIKE :bname"),
})

@Entity
@Table(name = "Room")
public class Room implements Serializable {
/**
* 
*/
private static final long serialVersionUID = 1L;
private RoomId id;
private String bname;
private Building building;

public String getBname() {
return bname;
}

public void setBname(String bname) {
this.bname = bname;
}

@Id
public RoomId getId() {
return id;
}

public void setId(RoomId id) {
this.id = id;
}

@ManyToOne
@JoinColumn(name = "Bname", insertable = false, updatable = false)
public Building getBuilding() {
return this.building;
}

public void setBuilding(Building building) {
this.building = building;
}
}

ROOMID:

@Embeddable
public class RoomId implements Serializable {

private String bname;
private String rcode;

public RoomId() {
}

public RoomId(String bname, String rcode) {
this.bname = bname;
this.rcode = rcode;
}

@Column(name = "Bname", nullable = false)
public String getbname() {
return bname;
}

public void setbname(String bname) {
this.bname = bname;
}

@Column(name = "Rcode", nullable = false)
public String getrcode() {
return rcode;
}

public void setrcode(String rcode) {
this.rcode = rcode;
}

public boolean equals(Object other) {
if ((this == other)) {
    return true;
}

if ((other == null)) {
    return false;
}

if (!(other instanceof RoomId)) {
    return false;
}

RoomId castOther = (RoomId) other;

return ((this.getbname() == castOther.getbname()) || (this.getbname() != null
        && castOther.getbname() != null &&

this.getbname().equals(castOther.getbname())))

        &&

((this.getrcode() == castOther.getrcode()) ||                                              (this.getrcode() != null               && castOther.getrcode() != null &&

        this.getrcode().equals(castOther.getrcode())));
}

public int hashCode() {
return super.hashCode();
}
}

大厦:

@Stateless
public class BuildingEAOImpl implements BuildingEAOImplLocal {
@PersistenceContext(unitName = "LabEJBSql")
private EntityManager em;

public BuildingEAOImpl() {
// TODO Auto-generated constructor stub
}

public Building findByBname(String bname) {
return em.find(Building.class, bname);
}
}

外观:

@Stateless
public class Facade implements FacadeRemote, FacadeLocal {
@EJB
BuildingEAOImplLocal BuildingEAO;
@EJB
RoomEAOImplLocal RoomEAO;

public Facade() {
// TODO Auto-generated constructor stub
}

public List<Room> findRoomsByBname(String bname) { 
 return RoomEAO.findByBname(bname); 
 }
}

伺服器:

@WebServlet("/TestClientServlet")
public class TestClientServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB
private FacadeLocal facade;

/**
* @see HttpServlet#HttpServlet()
*/
public TestClientServlet() {
super();
// TODO Auto-generated constructor stub
}

protected void doGet(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {

PrintWriter out = response.getWriter();
out.println("TestClientServlet-doGet");
out.close();

}

protected void doPost(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {

String url = null;
// Get hidden field
String operation = request.getParameter("operation");

if (operation.equals("showrooms")) {
    String bname = request.getParameter("txtBname");

    List<Room> r = facade.findRoomsByBname(bname);
    request.setAttribute("rooms", r);
    url = "/ShowRooms.jsp";
} else if (operation.equals("searchbuilding")) {
    System.out.println("TestClientServlet-searchbuilding");
    url = "/SearchBuilding.jsp";
} else {
    url = "/SearchBuilding.jsp";
}
System.out.println(url);

RequestDispatcher dispatcher = getServletContext()
        .getRequestDispatcher(url);
dispatcher.forward(request, response);
}
*/
}  

SEARCHBUILDING.JSP:

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1"> 
<title>Search Building</title> 
</head> 
<body> 
<form action="/BuildRoomClientProject/TestClientServlet" method="post"> 

<table cellspacing="0" cellpadding="0" border="0" align="left"> 
<tr> 
<td><h2>Search Building:</h2></td> 
</tr> 
<tr> 
<td> 

<input type= "text" name= "txtBname" size ="25" maxlength="25">
<input type="submit" name="submit" value="Search" /> 
</td> 
<td></td> 
</tr> 
</table> 

<input name="operation" value="showrooms" type="hidden"> 

</form> 
</body> 
</html> 

SHOWROOMS.JSP:

<%@ page contentType="text/html;charset=windows-1252"%> 
<%@ page import = "org.ics.ejb.Building" %> 
<%@ page import = "org.ics.ejb.Room" %> 
<%@ page import = "org.ics.ejb.RoomId" %> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title> 
Show Rooms
</title> 
</head> 
<body> 
<h2> 
Rooms: 
</h2> 
<%List<Room> r = (List<Room>)request.getAttribute("rooms"); %>
<% for (Room r1 : r){
out.println(r1.getBname() + " " + r1.getId().getrcode());
}%> 
<p>
</p>
<form action="/BuildRoomClientProject/TestClientServlet" method="post"> 
<input type="submit" name="submit" value="Tillbaka"> 
<input name="operation" value="searchbuilding" type="hidden"> 
</form> 
</body> 
</html>

【问题讨论】:

  • 你应该使用JSTL。

标签: java sql jsp servlets model-view-controller


【解决方案1】:

对于您的 jpql,您不是将 GROUPBY 与 GROUPBY 指令结合使用吗? 然后对于每个表条目,您将 bname (= BuildingName ??) 呈现为第一行。 然后将您检索到的列表呈现为表格,一个简短的示例可能是这样的:

<table>
     <c:forEach var="o" items="${objects}">
        <tr>
            <td>${o.bname}</td>
            <td>${o.id}</td>
            <td>${o.name}</td>
            <td>${o.descriptio}</td>   
        </tr>
    </c:forEach>
</table>

其实我刚找到这个:displaying a list of entities in jsp file by using java搜索“jsp,listview,table”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 2019-07-24
    • 1970-01-01
    • 2016-07-23
    相关资源
    最近更新 更多