【问题标题】:How to check if @RequestParam is empty如何检查@RequestParam 是否为空
【发布时间】:2021-11-23 12:40:33
【问题描述】:

我想知道如何检查 RequestParams 以查看它们是否为空,以便我可以转发到另一个 html 页面,现在我只是通过检查每个参数是否为空来解决这个问题。问题是如果用户尝试提交一个完全空的表单并且如果他们这样做我的程序将崩溃,我想转发到另一个页面。它将崩溃,因为程序试图将 int Price 转换为字符串,因为这是以空表单提交的内容。我知道我可以将 Int Price 设为字符串,但我想知道是否还有其他灵魂。

@Controller
public class HomeController {
    
    @Autowired
    private GlassesList glassesList;

    
    @GetMapping("/")
    public String goHome(Model model) {
        return "index";
    }
    
    @GetMapping("/addGlasses")
    public String addSunglasses(Model model) {
        return "addGlasses";
    }
    
    
    @GetMapping("/searchGlasses")
    public String searchSunglasses() {
        return "searchGlasses";
    }
    
    @GetMapping("/displayGlasses")
    public String displaySunglasses() {
        return "displayGlasses";
    }
    @PostMapping("/formPost")public String processForm (
            Model model,
            @RequestParam String productName,
            @RequestParam String brandName,
            @RequestParam String frameType,
            @RequestParam String color,
            @RequestParam String size,
            @RequestParam String lensType,
            @RequestParam int price
            ) {
if (productName.equals("") && brandName.equals("") && frameType.equals("") && color.equals("") && size.equals("") && lensType.equals("") && price == 0) {
            
            return "displayGlasses";
        }
        else {
        Glasses glasses = new Glasses(productName, brandName,frameType,
       color, size, lensType, price);
        glassesList.getGlassesList().add(glasses); 
        model.addAttribute("glassesList", glassesList);
        System.out.println(model.getAttribute(lensType));
        System.out.print(glassesList.getGlassesList());
        
        
        return "addGlasses";
        
        
        
    }
}

【问题讨论】:

  • productName.equals("") 或方法定义中的必需参数
  • 改用@ModelAttribute,你可以在那个类上写一个干净的方法。
  • 好吧,我想要的是,如果我的表单中的所有字段都是空白的,那么 porgram 应该自动转发到 displayGlasses.html,如果我实现了所需的参数,那么它将要求我填写表单。如果我误解了你的回答,我深表歉意我对 java 和 spring-boot 很陌生。
  • 由于您的参数都是字符串,请不要使用“==”,而应该使用字符串类中的“equals”方法来比较值。如果您只需要重定向,您可以继续使用这些方法或编写一个实用方法来检查您的全部或部分请求参数。

标签: java html spring spring-boot


【解决方案1】:

您可以使用 RequestBody 并在该请求中发送这些参数,而不是处理过多的请求参数,如下所示: 定义一个代表所有请求参数的类:

public class AllParams{
       @NotNull
       @NotEmpty
       private String productName;
       @NotNull
       @NotEmpty
       private String brandName;
       @NotNull
       @NotEmpty 
       private String frameType;
       @NotNull
       @NotEmpty
       private String color;
       @NotNull
       @NotEmpty
       private String size; 
       @NotNull
       @NotEmpty
       private String lensType;
    
       @Min(1)
       private int price;
    
      //getters and setters
    }

现在,在你的方法中使用它并绑定错误:

 @PostMapping("/formPost")public String processForm (@Valid @RequestBody AllParams requestParam, BindingResult bindingResult,
            Model model){

     //check if there is any error[based on your annotations]
     if(bindingResult.hasErrors()) {
        return "displayGlasses";
       
     }

//otherwise, write your usual code here
  ....
...
}

关于注解请参考:Difference Between @NotNull, @NotEmpty, and @NotBlank Constraints in Bean Validation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-15
    • 2020-05-29
    • 2018-04-02
    • 2016-04-05
    • 2018-01-08
    • 2020-09-25
    • 2012-01-15
    • 2016-07-16
    相关资源
    最近更新 更多