将mysql数据库数据插入到html表单中 [英] Inserting mysql database data into a html form

查看:153
本文介绍了将mysql数据库数据插入到html表单中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have records of products in my mysql database (I'm talking about a spring MVC java project using hibernate).

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

I have a html (and css) form of a search screen, here is its code:

<!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函数连接到我的数据库并获取正确的搜索结果(目前它只是将结果打印到控制台),这是函数:

I also have a java function that connect to my database and fetches the correct search results (currently it just prints the results to the console) , here is that function:

@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将包含一个包含搜索结果的表),有谁知道呢?

What I want to do is to insert those results from the java function into the html form (so that the html will contain a table with the search results), does anyone knows are?

谢谢!

推荐答案

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

I would say, your form in the HTML page must be a tag <form:form> and assigned a modelattribute. I believe it will be a ProductsEntity.

表单的绑定标签这里

您将包含taglib库以使标记生效。

You will have include the taglib library for tag to work.

包括<%@ taglib uri =http://www.springframework.org/tags/formprefix =form%> 之间<!DOCTYPE html> < html> 代码。

这篇关于将mysql数据库数据插入到html表单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆