Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 使用spring boot starter web和thymeleaf时填充下拉列表_Spring Mvc_Spring Boot_Thymeleaf - Fatal编程技术网

Spring mvc 使用spring boot starter web和thymeleaf时填充下拉列表

Spring mvc 使用spring boot starter web和thymeleaf时填充下拉列表,spring-mvc,spring-boot,thymeleaf,Spring Mvc,Spring Boot,Thymeleaf,我正在尝试使用SpringBootStarterWeb和thymeleaf创建一个web应用程序。作为起点,我使用Spring的验证表单输入–Getting Started Guide(),因为“这些指南旨在让您尽可能快地提高工作效率–使用Spring团队推荐的最新Spring项目版本和技术。” 我已将年龄输入字段更改为从服务填充的年龄下拉列表。我修改的代码如下: package hello; import java.util.List; import javax.validation.Vali

我正在尝试使用SpringBootStarterWeb和thymeleaf创建一个web应用程序。作为起点,我使用Spring的验证表单输入–Getting Started Guide(),因为“这些指南旨在让您尽可能快地提高工作效率–使用Spring团队推荐的最新Spring项目版本和技术。”

我已将年龄输入字段更改为从服务填充的年龄下拉列表。我修改的代码如下:

package hello;
import java.util.List;
import javax.validation.Valid;
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.PostMapping;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Controller
public class WebController implements WebMvcConfigurer {
    @Autowired
    AgeService ageService;
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/results").setViewName("results");
    }
    @GetMapping("/")
    public String showForm(Model model) {
        PersonForm personForm = new PersonForm();
        model.addAttribute("personForm", personForm);
        List<Integer> validAges = ageService.getValidAges();
        model.addAttribute("validAges", validAges);
        return "form";
    }
    @PostMapping("/")
    public String checkPersonInfo(@Valid PersonForm personForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return "form";
        }
        return "redirect:/results";
    }
}

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
<form action="#" th:action="@{/}" th:object="${personForm}" method="post">
    <table>
        <tr>
            <td>Name:</td>
            <td><input type="text" th:field="*{name}"/></td>
            <td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</td>
        </tr>
        <tr>
            <td>Age:</td>
            <td><select th:field="*{age}">
                <option value="">Please select ...</option>
                <option th:each="validAge:${validAges}" th:value="${validAge}" th:text="${validAge}"></option>
            </select></td>
            <td th:if="${#fields.hasErrors('age')}" th:errors="*{age}">Age Error</td>
        </tr>
        <tr>
            <td>
                <button type="submit">Submit</button>
            </td>
        </tr>
    </table>
</form>
</body>
</html>
包你好;
导入java.util.List;
导入javax.validation.Valid;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.Model;
导入org.springframework.validation.BindingResult;
导入org.springframework.web.bind.annotation.GetMapping;
导入org.springframework.web.bind.annotation.PostMapping;
导入org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
导入org.springframework.web.servlet.config.annotation.WebMVCConfiguer;
@控制器
公共类WebController实现WebMVCConfiguer{
@自动连线
年龄服务年龄服务;
@凌驾
public void addViewController(ViewControllerRegistry注册表){
registry.addViewController(“/results”).setViewName(“results”);
}
@GetMapping(“/”)
公共字符串显示窗体(模型){
PersonForm PersonForm=新PersonForm();
model.addAttribute(“personForm”,personForm);
List validAges=ageService.getValidAges();
model.addAttribute(“有效期”,有效期);
返回“表格”;
}
@邮戳(“/”)
公共字符串checkPersonInfo(@Valid PersonForm PersonForm,BindingResult BindingResult){
if(bindingResult.hasErrors()){
返回“表格”;
}
返回“重定向:/results”;
}
}
姓名:
名称错误
年龄:
请选择。。。
年龄误差
提交
当表单第一次显示时,一切正常,但是如果存在验证错误并且表单被重新显示,则下拉列表中只有“请选择…”选项


建议使用什么技术来实现此功能?

未填充下拉列表,因为bindingResult对象发现错误后,在返回表单之前,似乎没有将有效性列表作为属性添加到模型中

请加上:

模型,作为checkPersonInfo的参数,如图所示

   CheckPersonInfo(Model model, ...) 
在bindingResult.hasErrors块中将ValidAges添加到模型中,如图所示

if(bindingResult.hasErrors()){
   model.addAttribute("validAges", ageService.getValidAges());
   return "form";
}

很抱歉,这给了我一个语法错误。checkPersonInfo方法中没有定义变量模型。@NormanLynch,能否将不带引号的“模型模型”作为参数添加到checkPersonInfo方法中,然后再次测试?