在<%%>中设置html代码时不会出现

时间:2019-03-28 12:44:15

标签: html jsp

代码很好,没有问题 我试图使用out.println()

解决问题

代码一直运行到本节 但随后在浏览器中执行期间会继续使用同一类

该迭代应该显示特定用户的所有产品,以便允许更新商品或删除

<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="org.hibernate.Query"%>
<%@page import="org.hibernate.Session"%>
<%@page import="org.hibernate.service.ServiceRegistryBuilder"%>
<%@page import="entity.Product"%>
<%@page import="entity.Farmer"%>
<%@page import="org.hibernate.cfg.Configuration"%>
<%@page import="org.hibernate.service.ServiceRegistry"%>
<%@page import="org.hibernate.SessionFactory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <%
        String useremail = null;
        //allow access only if session exists
        if(session.getAttribute("useremail") == null){
                response.sendRedirect("farmlogin.jsp");
        }else{
             useremail = (String) session.getAttribute("useremail");
        }
    %>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <%@ include file="header.jsp" %>
        <title>My products</title>
    </head>
    
        <div class = "container">
        <h1><%=useremail%> . Here are your products</h1>
        </div>
        <div class="container-fluid">
            <div class = "row">
            <%
                SessionFactory sessionFactory;
                ServiceRegistry serviceRegistry;

                Configuration configuration = new Configuration();
                configuration.addAnnotatedClass(Farmer.class)
                    .addAnnotatedClass(Product.class)
                    .configure();

                serviceRegistry = new ServiceRegistryBuilder()
                        .applySettings(configuration.getProperties())
                        .configure("hibernate.cfg.xml")
                        .build();        
                sessionFactory = configuration.buildSessionFactory(serviceRegistry);

                Session sess = sessionFactory.openSession();
                String hql = "FROM Product";
                Query queryy = sess.createQuery(hql);
                List<Product> products;
                List results = queryy.list();
                products = results;
                Iterator<Product> itr = products.iterator();
                if (results == null || results.isEmpty()){
                    out.println("<h3>No products available</h3>");
                }else{
                    while(itr.hasNext()){
                        Product g = itr.next();
                        System.out.println(g.vendor.getEmail());
                        String src;
                        if(useremail == g.vendor.getEmail()){
                            int prodid = g.getId();
                            if(g.getType().equals("Pineapples")){
                                System.out.println("Pineapples");
                                src = "pineapple.jpg";
            %>
            <div class="col-sm-4">
                <img src="<%=src%>" alt="image" height="20px" width="20px">
                <p><%= g.getName()%></p>
                <p><%= g.vendor.getUsername()%></p>
                <form action="editprod.jsp">
                <input type="hidden" name="pass" value="<%=prodid%>">
                <button type="submit" class="btn btn-primary">Edit Product</button>
                </form>
            </div>
            <%
                }else if(g.getType().equals("Bananas")){
                    System.out.println("Bananas");
                    src = "banana.jpg";

            %>
            <div class="col-sm-4">
                <img src="<%=src%>" alt="image" height="20px" width="20px">
                <p><%= g.getName()%></p>
                <p><%= g.vendor.getUsername()%></p>
                <form action="editprod.jsp">
                <input type="hidden" name="pass" value="<%=prodid%>">
                <button type="submit" class="btn btn-primary">Edit Product</button>
                </form>
            </div>
            <%
                }else if(g.getType().equals("Avocado")){
                    System.out.println("Avocado");
                    src = "avocado.jpg";

            %>
            <div class="col-sm-4">
                <img src="<%=src%>" alt="image" height="20px" width="20px">
                <p><%= g.getName()%></p>
                <p><%= g.vendor.getUsername()%></p>
                <form action="editprod.jsp">
                <input type="hidden" name="pass" value="<%=prodid%>">
                <button type="submit" class="btn btn-primary">Edit Product</button>
                </form>
            </div>
            <%
                            }
                        }
                    }
                }
            %>
            </div>
        </div>
        <style>
            .col-sm-4{
                border: solid 2px black;
            }
        </style>
    <%@ include file="footer.jsp" %>
</html>

1 个答案:

答案 0 :(得分:0)

问题是如果条件

if(g.vendor.getEmail()。equals(useremail)){}可行

通过if(useremail == g.vendor.getEmail()){}

<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="org.hibernate.Query"%>
<%@page import="org.hibernate.Session"%>
<%@page import="org.hibernate.service.ServiceRegistryBuilder"%>
<%@page import="entity.Product"%>
<%@page import="entity.Farmer"%>
<%@page import="org.hibernate.cfg.Configuration"%>
<%@page import="org.hibernate.service.ServiceRegistry"%>
<%@page import="org.hibernate.SessionFactory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <%
        String useremail = null;
        //allow access only if session exists
        if(session.getAttribute("useremail") == null){
                response.sendRedirect("farmlogin.jsp");
        }else{
             useremail = (String) session.getAttribute("useremail");
        }
    %>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <%@ include file="header.jsp" %>
        <title>My products</title>
    </head>
        <div class = "container">
        <h1><%=useremail%> . Here are your products</h1>
        </div>
        <div class="container-fluid">
            <div class = "row">
            <%
                SessionFactory sessionFactory;
                ServiceRegistry serviceRegistry;
            %>
            <%
                Configuration configuration = new Configuration();
                configuration.addAnnotatedClass(Farmer.class)
                    .addAnnotatedClass(Product.class)
                    .configure();
            %>
            <%
                serviceRegistry = new ServiceRegistryBuilder()
                        .applySettings(configuration.getProperties())
                        .configure("hibernate.cfg.xml")
                        .build();        
                sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            %>
            <%
                Session sess = sessionFactory.openSession();
                String hql = "FROM Product";
                Query queryy = sess.createQuery(hql);
                List<Product> products;
                List results = queryy.list();
            %>
            <%
                if (results == null || results.isEmpty()){
                    out.println("<h3>No products available</h3>");
                }else{
                    products = results;
                    Iterator<Product> itr = products.iterator();
                    while(itr.hasNext()){
                        Product g = itr.next();
                        if(g.vendor.getEmail().equals(useremail)){
                            int prodid = g.getId();
                            if(g.getType().equals("Pineapples")){
            %>
            <div class="col-sm-1">
                <img src="pineapple.jpg" alt="image" height="20px" width="20px">
                <p><%= g.getName()%></p>
                <p><%= g.vendor.getUsername()%></p>
                <form action="editprod.jsp">
                <input type="hidden" name="pass" value="<%=prodid%>">
                <button type="submit" class="btn btn-primary">Edit Product</button>
                </form>
            </div>
            <%
                }else if(g.getType().equals("Bananas")){
            %>
            <div class="col-sm-1">
                <img src="banana.jpg"" alt="image" height="20px" width="20px">
                <p><%= g.getName()%></p>
                <p><%= g.vendor.getUsername()%></p>
                <form action="editprod.jsp">
                <input type="hidden" name="pass" value="<%=prodid%>">
                <button type="submit" class="btn btn-primary">Edit Product</button>
                </form>
            </div>
            <%
                }else if(g.getType().equals("Avocado")){
                    out.println("Avocado");
            %>
            <div class="col-sm-1">
                <img src="avocado.jpg" alt="image" height="20px" width="20px">
                <p><%= g.getName()%></p>
                <p><%= g.vendor.getUsername()%></p>
                <form action="editprod.jsp">
                <input type="hidden" name="pass" value="<%=prodid%>">
                <button type="submit" class="btn btn-primary">Edit Product</button>
                </form>
            </div>
            <%
                            }
                        }
                    }
                }
            %>
            </div>
        </div>
        <style>
            .col-sm-4{
                border: solid 2px black;
            }
        </style>
    <%@ include file="footer.jsp" %>
</html>
感谢所有回答问题的人