首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将mysql数据库数据插入html表单

将mysql数据库数据插入html表单
EN

Stack Overflow用户
提问于 2018-08-06 02:45:46
回答 1查看 228关注 0票数 0

我的mysql数据库中有产品的记录(我说的是一个使用hibernate的spring MVC java项目)。

我有一个html (和css)形式的搜索屏幕,下面是它的代码:

<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <style>
        * {box-sizing: border-box;}

        body {
            margin: 0;
            font-family: Arial, Helvetica, sans-serif;
        }

        .topnav {
            overflow: hidden;
            background-color: #e9e9e9;
        }

        .topnav a {
            float: left;
            display: block;
            color: black;
            text-align: center;
            padding: 14px 16px;
            text-decoration: none;
            font-size: 17px;
        }

        .topnav a:hover {
            background-color: #ddd;
            color: black;
        }

        .topnav a.active {
            background-color: #2196F3;
            color: white;
        }

        .topnav .search-container {
            float: right;
        }

        .topnav input[type=text] {
            padding: 6px;
            margin-top: 8px;
            font-size: 17px;
            border: none;
        }

        .topnav .search-container button {
            float: right;
            padding: 6px 10px;
            margin-top: 8px;
            margin-right: 16px;
            background: #ddd;
            font-size: 17px;
            border: none;
            cursor: pointer;
        }

        .topnav .search-container button:hover {
            background: #ccc;
        }

        @media screen and (max-width: 600px) {
            .topnav .search-container {
                float: none;
            }
            .topnav a, .topnav input[type=text], .topnav .search-container button {
                float: none;
                display: block;
                text-align: left;
                width: 100%;
                margin: 0;
                padding: 14px;
            }
            .topnav input[type=text] {
                border: 1px solid #ccc;
            }
        }
    </style>
</head>
<div class="topnav">
    <a class="active" href="#home">Home</a>
    <a href="#about">About</a>
    <a href="#contact">Contact</a>
    <div class="search-container">
        <form action="/search1">
            <input type="text" placeholder="Search.." name="search">
            <button type="submit"><i class="fa fa-search"></i></button>
        </form>
    </div>
</div>

<div style="padding-left:16px">
</div>

<style>
    table {
        border-collapse: collapse;
        border-spacing: 0;
        width: 100%;
        border: 1px solid #ddd;
    }

    th, td {
        text-align: left;
        padding: 16px;
    }

    tr:nth-child(even) {
        background-color: #f2f2f2
    }
</style>
</head>
<body>

<h2>Search Results</h2>

<table>
    <tr>
        <th>Product ID</th>
        <th>Product Name</th>
        <th>Product Price</th>

</table>
</body>
</html>

我还有一个java函数,它连接到我的数据库并获取正确的搜索结果(目前它只是将结果打印到控制台),下面是这个函数:

@RequestMapping("/results")
    public String results(UserSearch search){
        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml");
        SessionFactory factory = cfg.buildSessionFactory();
        Session session = factory.openSession();

        ArrayList<ProductsEntity> li = (ArrayList<ProductsEntity>)session.createQuery("from ProductsEntity").list();
        for (ProductsEntity c: li) {
            if (c.getProductName().toLowerCase().contains(search.getSearch().toLowerCase())){
                System.out.println(c.getProductName());
            }
        }
        session.close();
        factory.close();

        return "results";
    }

我想要做的是将那些来自java函数的结果插入到html表单中(这样html将包含一个带有搜索结果的表),有人知道是这样的吗?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-06 03:19:32

我想说的是,HTML页面中的表单必须是一个标记<form:form>并分配了一个modelattribute。我相信它会是一个ProductsEntity

here解释了form标签的绑定。

您需要包含taglib库才能使标记正常工作。

<!DOCTYPE html><html>标记之间包含<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51697296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档