有人可以帮助我在百里香中进行多次下拉吗?

时间:2019-11-17 23:54:08

标签: mysql spring-boot thymeleaf html-select

我真的想弄清楚春季靴子,但我总是被卡在某个地方。我需要下拉选择的帮助。我有类别,子类别和视频实体。我想通过选择第一个类别添加视频,我应该在子类别之间进行选择。但是我在子类别字段中什么也没得到。我做错了什么吗?还是我做得很好,但不确定如何完成。

@Entity
@Table(name = "categories")
public class Categories {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_category")
    private long id_category;
    @Column(name = "category_name")
    private String category_name;


@Entity
@Table(name = "subcategories")
public class Subcategories {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_subcategory")
    private long id_subcategory;

    @Column(name="subcategory_name")
    private String subcategory_name;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "categories_id_category")
    private Categories categories;


@Entity
@Table(name = "videos")
public class Videos {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_video")
    private long id_video;

    @Column(name = "title")
    private String title;

    @Column(name = "link")
    private String link;

    @Column(name = "length")
    private String length;

    @Column(name = "description")
    private String description;

    @Column(name = "added")
    @Temporal(TemporalType.DATE)
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    private Date added = new Date();

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "categories_id_category")
    private Categories categories;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "subcategories_id_subcategory")
    private Subcategories subcategories;

这是控制者

        @RequestMapping("/newVideos")
    public String addVideos(Model model, String Categories) {

        Videos theVideos = new Videos();
        Subcategories theSubcategories = new Subcategories();
        Categories thecategories = new Categories();
        theVideos.setCategories(thecategories);
        List<Categories> getCategories = categoriesService.findAll();
        theVideos.setSubcategories(theSubcategories);

        List<Subcategories> getSubcategories = subcategoriesService.findSubcategoriesByCategory();

        model.addAttribute("videos", theVideos);
        model.addAttribute("subcategories", getSubcategories);
        model.addAttribute("categories", getCategories);
        return "admin/admin_newVideos";
    }

服务

    public List<Subcategories> findSubcategoriesByCategory() {
        return subcategoriesRepository.findSubcategoriesByCategory();
    }

和存储库 (我也尝试过使用 =?1 并在Servis和控制器中进行了更改,但是我总是必须自己插入类别索引,当然,它与类别下拉列表无关)

    @Query(value = "select * from subcategories sub where categories_id_category=2", nativeQuery = true)
    List<Subcategories> findSubcategoriesByCategory();

HTML

<form action="#" th:action="@{/admin/saveVideos}" th:object="${videos}" method="post">
                  <table border="0" cellpadding="10" class="data">
                        <tr>
                            <td>title:</td>
                            <td><input type="text" th:field="*{title}" /></td>
                        </tr>
                        <tr>
                            <td>link:</td>
                            <td><input type="text" th:field="*{link}" /></td>
                        </tr>
                        <tr>
                            <td>duration:</td>
                            <td><input type="text" th:field="*{length}" /></td>
                        </tr>
                        <tr>
                            <td>added:</td>
                            <td><input type="text" th:field="*{added}" /></td>
                        </tr>
                        <tr>
                            <td>description</td>
                            <td><input type="text" th:field="*{description}" /></td>
                        </tr>
                        <tr>
                            <td>category:</td>
                            <td>
                                <select th:field="*{categories}" id="thisCat">
                                    <option th:each="categories : ${categories}"
                                            th:value="${categories.id_category}"
                                            th:utext="${categories.category_name}"/>
                                </select>
                            </td>
                        </tr>

                        <tr>
                            <td>subcategory</td>
                            <td>
                                <select th:field="*{subcategories}" id="thisSub">
                                    <option th:each="subcategories : ${subcategories}"
                                            th:value="${subcategories.id_subcategory}"
                                            th:utext="${subcategories.subcategory_name}"/>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2"><button type="submit">Save</button></td>
                        </tr>
                    </table>
                </form>

1。有什么简单的方法不需要在存储库或控制器中手动插入类别索引?我尝试了两种方式。

2。我需要有关此2下拉菜单的一些建议或代码。我找到了一些示例,但没有找到数据库

0 个答案:

没有答案