简体   繁体   English

提交表单时没有找到接口 java.util.List 的主要或默认构造函数?

[英]Receive No primary or default constructor found for interface java.util.List when submitting form?

I'm in the Thymeleaf swamp once again.我又一次进入了 Thymeleaf 沼泽。

I receive我收到

There was an unexpected error (type=Internal Server Error, status=500).出现意外错误(类型=内部服务器错误,状态=500)。 No primary or default constructor found for interface java.util.List找不到接口 java.util.List 的主要或默认构造函数

when I click my addRow button "+".当我单击我的 addRow 按钮“+”时。

home template:主页模板:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link rel="stylesheet" type="text/css" media="all"

          href="../static/css/home-style.css"
          th:href="@{/css/home-style.css}"/>

    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--------------------------------------------------------------------------->
<h1 id="page-title"/>
<h3 id="recipe-id"/>
<h3 id="recipe-name"/>
<!--------------------------------------------------------------------------->
<form id="get-recalculated-recipe" action="#" method="post">
    <table>
        <tr>
            <th th:text="#{table.label.quantity}">Quantity</th>
            <th>Unit</th>
            <th>Ingredient Name</th>
            <th>Multiplier</th>
        </tr>
<!---------------------------------------------------------------------------->
        <div>
            <h2 th:text="#{text.new.recipe}">Add new Recipe</h2>

            <!--   @{/home}   replace via home th xml-->
            <!--    Replace ${recipe} via home th xml-->
            <form action="#" th:action="@{/home}" th:object="${recipe}" method="post">


                <legend th:text="${recipename}">Legend text</legend>


                <div>
                    <!--                message text-->
                    <label th:text="#{text.ingredients}">Ingredients</label>
                    <table>
                        <!--------------------------------------------------------------------------------------------->
                        <thead>
                        <tr>
                            <th th:text="#{text.ingredient.quantity}">Qty</th>
                            <th th:text="#{text.ingredient.unit}">Unit</th>
                            <th th:text="#{text.ingredient.name}">Name</th>
                            <th>
                                <button type="submit" name="addRow" th:text="#{text.submit.addrow}">Add row</button>
                            </th>
                        </tr>
                        </thead>
                        <!--------------------------------------------------------------------------------------------->
                        <tbody>
                        <!--- Property or field 'ingredients' cannot be found on object of type 'com.potatospy.reciply.web.model.response.Recipe' - maybe not public or not valid? --->
                        <tr th:each="ingredient : *{ingredientList}">
                            <!--                  <td th:text="${rowStat.count}">1</td>-->
                            <td>
                                <input type="text" th:field="*{ingredientName}"
                                       th:errorclass="fieldError"/>
                            </td>
                            <!--------------------------------------------------------------------------------------------->
                            <td>
                                <input type="text" th:field="*{quantity}"
                                       th:errorclass="fieldError"/>
                            </td>
                            <!--------------------------------------------------------------------------------------------->
                            <td>
                                <select th:field="*{unit}">
                                    <option th:each="units : ${allUnits}" th:value="${units.id}" th:text="${var.name}">
                                        Thymus Thymi
                                    </option>
                                </select>
                            </td>
                            <!--------------------------------------------------------------------------------------------->
                            <td>
                                <button type="submit" name="removeRow" th:value="${rowStat.index}"
                                        th:text="#{text.submit.removerow}">Remove row
                                </button>
                            </td>
                            <!--------------------------------------------------------------------------------------------->
                        </tr>
                        </tbody>
                    </table>
                </div>
                <!--------------------------------------------------------------------------------------------->
                <div class="submit">
                    <button type="submit" name="save" th:text="#{text.submit.newrecipe}">Add New Recipe</button>
                </div>
            </form>
        </div>
    </table>
</form>


</body>
</html>

Controller:控制器:

package com.potatospy.reciply.web.controller;

import com.potatospy.reciply.business.service.RecipeService;
import com.potatospy.reciply.business.util.AttributeNames;
import com.potatospy.reciply.business.util.ViewNames;
import com.potatospy.reciply.web.model.response.Unit;
import com.potatospy.reciply.web.model.response.Ingredient;
import com.potatospy.reciply.web.model.response.Recipe;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.potatospy.reciply.business.util.Mappings;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Controller
@Slf4j
@RequestMapping("/")
public class ReciplyController {


    @Autowired
    RecipeService svc;


    // == Model attributes == todo
    @ModelAttribute
    public List<Recipe> recipeData(){ return svc.getAllRecipes(); } // Todo For populating a menu of recipes

    // Menu Text for recipe row Unit selection
    @ModelAttribute("allUnits")
    public List<Unit> unitList() {
        return Arrays.asList(Unit.ALL);
    }


    // == FIRST STOP ==

    /* This view doesn't require any attributes or params passed to it. It's for first visits only. */
    @GetMapping(Mappings.HOME)
    public String getHomeView(Model model) {    // Took out params and attribs since they dont exist in the model
                                                // until the controller creates them

        log.info("Inside getMainView");


        Recipe recipe = svc.initNewRecipe(); // Create and initialize a new recipe

//////////////////////////////////////////////////////////////////////////////////////////////////
        model.addAttribute(AttributeNames.RECIPE, recipe);
        model.addAttribute(AttributeNames.RECIPE_ID, recipe.getRecipeId()); // should I put 0 here or what LOL
        model.addAttribute(AttributeNames.RECIPE_NAME, recipe.getRecipeName()); // "" ?
        model.addAttribute(AttributeNames.RECIPE_DATA, recipeData());
        model.addAttribute("ingredients", recipe.getIngredientList());

        log.info("recipe ingredient list size: {}", recipe.getIngredientList().size());
        //model.addAttribute(AttributeNames.INGREDIENT_LIST, recipe.getIngredientList());

        return ViewNames.HOME;
    }


    // == ADD ROW ==

    // Add Row, creates and initializes a new Recipe, adds it to the DB and List
    // and adds the attribute.
    @RequestMapping(value=Mappings.ADD_ROW)
    public String addRow(Model model
            /*final Recipe recipe, */
            /*final BindingResult bindingResult*//*holds the result of the validation and binding and contains errors that may have occurred*/) {


        svc.initNewRecipe();

        model.addAttribute(AttributeNames.RECIPE_DATA, recipeData());

        return ViewNames.HOME;
    }
/*@RequestMapping(value="/seedstartermng", params={"addRow"})
    public String addRow(final SeedStarter seedStarter, final BindingResult bindingResult) {
        seedStarter.getRows().add(new Row());
        return "seedstartermng";



    }*/

    @GetMapping("homelink")
    public String getHomeViewShortcut(Model model){
        log.info("Inside getHomeViewShortcut");
        Recipe recipe1 = new Recipe();
        recipe1.setIngredientList(new ArrayList<>());

        model.addAttribute("RECIPE",recipe1);

        return ViewNames.HOME;
    }


    // == AFTER SUBMIT ==

    @PostMapping(Mappings.SUBMIT)
    public String submit(Model model/*, @ModelAttribute(AttributeNames.RECIPE*/){

        //model.addAttribute(AttributeNames.RECIPE, recipe);
        model.addAttribute(AttributeNames.RECIPE_ID, 0); // should i put 0 here or what LOL
        model.addAttribute(AttributeNames.RECIPE_NAME, ""); // "" ?
        model.addAttribute(AttributeNames.RECIPE_DATA, recipeData());

        return "";
    }


    // == AFTER SUBMIT ==

    @PostMapping(Mappings.HOME)
    public String getRecipe(
            @ModelAttribute(AttributeNames.RECIPE) Recipe recipe,
            @ModelAttribute(AttributeNames.INGREDIENT_LIST) List<Ingredient> ingredientList){


        return "";
    }
}

Service:服务:

package com.potatospy.reciply.business.service;

import com.potatospy.reciply.business.dao.entity.RecipeEntity;
import com.potatospy.reciply.business.dao.repository.RecipeRepository;
import com.potatospy.reciply.business.util.IDManager;
import com.potatospy.reciply.web.model.response.Unit;
import com.potatospy.reciply.web.model.response.Ingredient;
import com.potatospy.reciply.web.model.response.Recipe;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class RecipeServiceImpl implements RecipeService{


    @Value("${id.length}")
    private int idLength;

    @Autowired
    private RecipeRepository recipeRepository;

    @Autowired
    IDManager idManager;

    List<Recipe> inMemRecipeList = new ArrayList<>();  // In-memory list of recipes


    // Get all Recipes from DB, load into list of Recipes, return list
    @Override
    public List<Recipe> getAllRecipes() {

        // "Clear" the list
        inMemRecipeList = new ArrayList<>();


        // Retrieve recipes from DB and add to In-memory list
        for(RecipeEntity recipeDbEntity : recipeRepository.findAll()){

            Recipe recipe = new Recipe();
            BeanUtils.copyProperties(recipeDbEntity, recipe);

            inMemRecipeList.add(recipe);
        }


        log.info("Returning list from getAllRecipes : {}", inMemRecipeList);
        return inMemRecipeList;
    }


    // Save a recipe and log
    @Override
    public RecipeEntity addRecipeToDb(RecipeEntity recipeToSave) {

        log.info("Saved recipe with ID: " + recipeToSave.getRecipeId());

        return recipeRepository.save(recipeToSave);
    }


    // == Create new blank Recipe, save to DB and memory and return ==

    public Recipe initNewRecipe(){

        // Create a new recipe
        Recipe newRecipe = new Recipe();
        newRecipe.setRecipeId(generateId());
        newRecipe.setRecipeName("Hamblargers");

        // Add a blank ingredient FOR TESTING to the Recipe
        Ingredient blankIngredient = new Ingredient(
                0L,
                0.0,
                Unit.CUPS,
                ""
        );

        newRecipe.getIngredientList().add(blankIngredient);


        // Save it to DB and return the result
        RecipeEntity newRecipeEntity = new RecipeEntity();
        BeanUtils.copyProperties(newRecipe, newRecipeEntity);

        Recipe returnableRecipe = new Recipe();
        BeanUtils.copyProperties(addRecipeToDb(newRecipeEntity), returnableRecipe);

        getAllRecipes();    // Update memory TODO This should be all automatic!


        log.info("New recipe created with id {}", returnableRecipe.getRecipeId());

        return returnableRecipe;
    }


    // Delete a recipe and log
    @Override
    public void delete(Recipe recipeToDelete) {

        RecipeEntity recipeEntityToDelete = new RecipeEntity();

        log.info("Deleting recipe with ID: {}  There is no undo at this time.",
                recipeToDelete.getRecipeId());

        recipeRepository.delete(recipeEntityToDelete);
    }


    @Override
    public Long generateId() {
        log.info("Service Generating Generic ID with length {}", idLength);

        return (idManager.generateId(idLength));   // Return string via service
    }
}

My Recipe object Ingredient ArrayList was size 0!我的 Recipe 对象 Ingredient ArrayList 的大小为 0!

My initing in the Service doesnt seem to work.我在服务中的初始化似乎不起作用。

不兼容的类型。 必需的 java.util.List<object> 找到 java.util.List<integer><div id="text_translate"><p> 我需要在 Android Studio 中为 Android 项目使用列表列表,但任何时候尝试声明一个我都会突出显示此错误。 我尝试制作演员表,我尝试使用 generics 和几乎所有东西,但每次我都会突出显示此错误。 以下是发送此错误的一种代码示例:</p><pre> List&lt;List&lt;Object&gt;&gt; p = Arrays.asList( Arrays.asList(1,2,3) );</pre><p> 最奇怪的是,在 IntelliJ 中,这段代码运行得非常好。 所以,我想知道如何解决这个问题。</p></div></integer></object> - Incompatible types. Required java.util.List<Object> found java.util.List<Integer>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有为接口 java.util.List 找到主构造函数或默认构造函数 - No primary or default constructor found for interface java.util.List Rest API Spring boot 无效的@BsonCreator 构造函数。 预期 class java.lang.String,找到接口 java.util.List - Invalid @BsonCreator constructor. Expected class java.lang.String, found interface java.util.List 不兼容的类型; 找到:接口java.util.List <java.lang.Object> ,必需:接口java.util.List <test.entity.Emp> - Incompatible types; found: interface java.util.List<java.lang.Object>, required: interface java.util.List<test.entity.Emp> 找不到AttributedString(java.util.List <java.lang.String> ) - no suitable constructor found for AttributedString(java.util.List<java.lang.String>) 不兼容的类型。 必需的 java.util.List<object> 找到 java.util.List<integer><div id="text_translate"><p> 我需要在 Android Studio 中为 Android 项目使用列表列表,但任何时候尝试声明一个我都会突出显示此错误。 我尝试制作演员表,我尝试使用 generics 和几乎所有东西,但每次我都会突出显示此错误。 以下是发送此错误的一种代码示例:</p><pre> List&lt;List&lt;Object&gt;&gt; p = Arrays.asList( Arrays.asList(1,2,3) );</pre><p> 最奇怪的是,在 IntelliJ 中,这段代码运行得非常好。 所以,我想知道如何解决这个问题。</p></div></integer></object> - Incompatible types. Required java.util.List<Object> found java.util.List<Integer> 无法解析构造函数&#39;ArrayList(java.util.List <T> )`在Java中 - Cannot resolve constructor 'ArrayList(java.util.List<T>)` in Java java java.util.List接口中不同参数类型的含义 - Meaning of the different parameter types in java java.util.List interface java.util.List是一个接口,JAXB无法处理接口 - java.util.List is an interface, and JAXB can't handle interfaces ORMLite不知道如何存储接口java.util.List - ORMLite does not know how to store interface java.util.List Hibernate 无法实例化抽象类或接口:java.util.List - Hibernate Cannot Instantiate Abstract class or interface: java.util.List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM