Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从JSP类检索数据(带Servlet的MVC)_Java_Jsp_Search_Servlets_Model View Controller - Fatal编程技术网

Java 从JSP类检索数据(带Servlet的MVC)

Java 从JSP类检索数据(带Servlet的MVC),java,jsp,search,servlets,model-view-controller,Java,Jsp,Search,Servlets,Model View Controller,我现在正在做一个学校的项目,我和我的团队应该在那里建设 一个MVC项目,我们有教室和教室。这个关系是一间一间的,正在建一间一间的房间。 当我们要搜索建筑名称并稍后显示该建筑中的所有房间时,就会出现问题。当我们在没有jsp的情况下运行servlet时,我们可以提取相关数据,但是当我们通过运行searchBuilding.jsp启动项目时,我们只能键入建筑名称,当我们希望看到连接的房间时,我们反而会得到消息 “java.lang.ClassCastException:java.util.ArrayL

我现在正在做一个学校的项目,我和我的团队应该在那里建设 一个MVC项目,我们有教室和教室。这个关系是一间一间的,正在建一间一间的房间。 当我们要搜索建筑名称并稍后显示该建筑中的所有房间时,就会出现问题。当我们在没有jsp的情况下运行servlet时,我们可以提取相关数据,但是当我们通过运行searchBuilding.jsp启动项目时,我们只能键入建筑名称,当我们希望看到连接的房间时,我们反而会得到消息 “java.lang.ClassCastException:java.util.ArrayList不能强制转换为org.ics.ejb.Room”。 可能所有的代码都有点混乱,但如果有人能帮我发现问题,我会非常感激,因为我明天应该把这个给我的教授看

这是我们的代码: 建筑:

@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;
}   
室友:

@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();
}
}

建筑EAO:

@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); 
     }
 }
@无状态
公共类Facade实现FacadeMote、FacadeLocal{
@EJB
BuildingEAO本地BuildingEAO;
@EJB
RoomEAO本地RoomEAO;
公共立面(){
//TODO自动生成的构造函数存根
}
公共列表FindRoomByName(字符串bname){
返回房间ao.findByBname(bname);
}
}
SERVLET:

@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 service(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE html><html><head>");
    out.println("<title>Lab1</title>");
    out.println("<meta charset=\"ISO-8859-1\">");
    out.println("</head><body>");



    /*//THIS WORKS PERFECTLY FINE
List<Room> rooms = facade.findRoomsByBname("EC2");
    for (Room r : rooms) {
        out.println("<h4>Hittade: " + r.getId().getbname() + " "
                + r.getId().getrcode() + "</h4>");
        out.println("</body></html>");
    }
}*/

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("showbuilding")) {
        String bname = request.getParameter("txtBname");

        List<Room> r = facade.findRoomsByBname(bname);
        request.setAttribute("rooms", r);
        url = "/ShowBuilding.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);
}
*/
}
@WebServlet(“/TestClientServlet”)
公共类TestClientServlet扩展了HttpServlet{
私有静态最终长serialVersionUID=1L;
@EJB
私人立面;
/**
*@参见HttpServlet#HttpServlet()
*/
公共TestClientServlet(){
超级();
//TODO自动生成的构造函数存根
}
受保护的无效服务(HttpServletRequest),
HttpServletResponse响应)引发ServletException,IOException{
PrintWriter out=response.getWriter();
out.println(“”);
out.println(“Lab1”);
out.println(“”);
out.println(“”);
/*//这个很好用
列表房间=facade.findRoomsByBname(“EC2”);
用于(r室:房间){
out.println(“Hittade:+r.getId().getbname()+”)
+r.getId().getrcode()+“”);
out.println(“”);
}
}*/
受保护的无效数据集(HttpServletRequest请求,
HttpServletResponse响应)引发ServletException,IOException{
PrintWriter out=response.getWriter();
out.println(“TestClientServlet-doGet”);
out.close();
}
受保护的void doPost(HttpServletRequest请求,
HttpServletResponse响应)引发ServletException,IOException{
字符串url=null;
//获取隐藏字段
字符串操作=request.getParameter(“操作”);
if(操作等于(“展示建筑”)){
字符串bname=request.getParameter(“txtBname”);
List r=facade.findRoomsByBname(bname);
request.setAttribute(“房间”,r);
url=“/ShowBuilding.jsp”;
}else if(operation.equals(“searchbuilding”)){
System.out.println(“TestClientServlet searchbuilding”);
url=“/SearchBuilding.jsp”;
}否则{
url=“/SearchBuilding.jsp”;
}
System.out.println(url);
RequestDispatcher dispatcher=getServletContext()
.getRequestDispatcher(url);
转发(请求、响应);
}
*/
}
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="showbuilding" type="hidden"> 

</form> 
</body> 
</html> 
<%@ 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 Building 
</title> 
</head> 
<body> 
<%  Room r = (Room)request.getAttribute("rooms"); %>
<% Building b = (Building)request.getAttribute("building"); %>
<% for (int i=0; i<100; i++){
System.out.println(r.getBname() + " " + r.getId().getrcode());
}%> 
<h2> 
Building: 
</h2> 
<p>
<%= r.getId().getrcode()%>
<%= b.getBname()%> 
</p>
<form action="/BuildRoomClientProject/TestClientServlet" method="post"> 
<input type="submit" name="submit" value="Tillbaka"> 
<input name="operation" value="searchbuilding" type="hidden"> 
</form> 
</body> 
</html>

搜索大楼
搜索大楼:
SHOWBUILDING.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="showbuilding" type="hidden"> 

</form> 
</body> 
</html> 
<%@ 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 Building 
</title> 
</head> 
<body> 
<%  Room r = (Room)request.getAttribute("rooms"); %>
<% Building b = (Building)request.getAttribute("building"); %>
<% for (int i=0; i<100; i++){
System.out.println(r.getBname() + " " + r.getId().getrcode());
}%> 
<h2> 
Building: 
</h2> 
<p>
<%= r.getId().getrcode()%>
<%= b.getBname()%> 
</p>
<form action="/BuildRoomClientProject/TestClientServlet" method="post"> 
<input type="submit" name="submit" value="Tillbaka"> 
<input name="operation" value="searchbuilding" type="hidden"> 
</form> 
</body> 
</html>

展览大楼
“rooms”属性被设置为servlet中房间的ArrayList。但是,当您试图在jsp中获取它时,您期望的是一个单人房间。
改变



然后遍历每个房间

<%  List<Room> rooms = (List<Room>)request.getAttribute("rooms"); %>