html 验证Thymeleaf中的输入

yvt65v4c  于 2022-12-16  发布在  其他
关注(0)|答案(2)|浏览(136)

我有这样的输入:

Masa: <input type="number"  class="form-control form-text"   name="masa"/>
    <div class="text col-sm-12 error" th:if="${wzrost}" >
        <p class="text text-center">
            To pole jest wymagane
        </p>
    </div>
    Wzrost: <input type="number" class="form-control form-text "   name="wzrost"/>
    <div class="text col-sm-12 error" th:if="${wzrost}" >
        <p class="text text-center">
            To pole jest wymagane
        </p>
    </div>

而这个控制器;

String x = String.valueOf(masa);
        String y = String.valueOf(wzrost);

        if(x==null ){
        model.addAttribute("wzrost",true);
        return"views/success";
    }
        if(y==null ){
            model.addAttribute("wzrost",true);
            return"views/success";
        }

当我单击表单提交按钮时,总是出现错误nullpointerexception。
如何验证输入,以便在输入为空时弹出消息

qcbq4gxm

qcbq4gxm1#

@PostMapping("/cal-bmi")
public String calculateBmiForm(Model model, Integer masa, Integer wzrost) {

String x = String.valueOf(masa);
    String y = String.valueOf(wzrost);

    if(x==null ){
    model.addAttribute("wzrost",true);
    return"views/success";
}
    if(y==null ){
        model.addAttribute("wzrost",true);
        return"views/success";
    }
}

当我从masa和wzrost得到一个值时,我从null开始检查,我点击提交,因为是null指针异常

<form th:action="@{/cal-bmi}" method="post">

    <ul class="gender-options">
        <input id="man" type="radio" name="gender" value="male" required  />
        <label for="man">mężczyzna</label> &frasl;
        <input id="woman" type="radio" name="gender" value="female"/>
        <label for="woman">kobieta</label>
    </ul>

    Masa: <input type="number" class="form-control form-text" required placeholder="(kg)" name="masa"/>

    <!--<div class="text col-sm-12 error" th:if="${wzrost}">-->
        <!--<p class="text text-center">-->
            <!--To pole jest wymagane-->
        <!--</p>-->
    <!--</div>-->
    Wzrost: <input type="number" class="form-control form-text " required placeholder="(cm)" name="wzrost"/>

    <!--<div class="text col-sm-12 error" th:if="${wzrost}">-->
        <!--<p class="text text-center">-->
            <!--To pole jest wymagane-->
        <!--</p>-->
    <!--</div>-->
    <input type="submit" class="col-lg-10 btn btn-primary" value="Oblicz"/>
</form>

现在我使用了必需的,但不是很好的解决方案

ohtdti5x

ohtdti5x2#

看起来您想要实现服务器端验证。为此,最好的方法是使用验证器及其bindingResult。实现服务器端验证的步骤是
1.具有模型

public class PersonForm {
  private String name;

 public String getName() {
    return this.name;
}

 public void setName(String name) {
    this.name = name;
}
}

1.在html中使用表单模型

<form action="#" th:action="@{/personForm}" th:object="${personForm}" method="post">
<table>
    <tr>
        <td><label th:text="#{label.name}+' :'"></label></td>
        <td><input type="text" th:field="*{name}" /></td>
        <td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Generic Error</td>
    </tr>
    <tr>
        <td><button type="submit">Submit</button></td>
    </tr>
</table>
</form>

1.具有验证程序类
@Component

public class PersonFormValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
    return PersonForm.class.equals(clazz);
}

@Override
public void validate(Object target, Errors errors) {
    ValidationUtils.rejectIfEmpty(errors, "name", "field.name.empty");
    PersonForm p = (PersonForm) target;

    if (p.getName().equalsIgnoreCase("XXX")) {
        errors.rejectValue("name", "Name cannot be XXX");
    }
}}

1.将验证器绑定到控制器,让Spring发挥作用。
@控制器

public class WebController {
@Autowired
PersonFormValidator personFormValidator;

@InitBinder("personForm")
protected void initPersonFormBinder(WebDataBinder binder) {
binder.addValidators(personFormValidator);
}

@PostMapping("/personForm")
public String checkPersonInfo(@Validated PersonForm personForm, BindingResult bindingResult, final RedirectAttributes redirectAttributes) {
if (bindingResult.hasErrors()) {
    return "personForm";
}
redirectAttributes.addFlashAttribute("personResult",  apiClientService.getPersonResult(personForm));
return "redirect:/spouseForm";
}
}

相关问题