简体   繁体   English

为什么程序显示空结果?

[英]Why program shows empty result?

I create data field name "csPreRequest" and add to html form as model attribute like follow. 我创建数据字段名称"csPreRequest"并添加到html表单中,作为模型属性,如下所示。

@GetMapping("/add")
public String addNewCourse(Model model) {

    String csPreRequest = "";
    model.addAttribute("csPreRequest",csPreRequest);

    return  "admin/add-course-module";
}

what i need to do is fill this field with my custom data and return back to controller class.I tried as follows: 我需要做的是用我的自定义数据填充此字段并返回到控制器类。我尝试如下:

@PostMapping("/save")
public String addNewCourse(@ModelAttribute("csPreRequest") String course) {

    System.out.println("\n Pre Reqest Courses : "+course+"\n\n");   

    return "redirect:/courses/add";
}

and my html code is like this: 和我的HTML代码是这样的:

      <input type="text" id="csPreRequest" th:field="${csPreRequest}">

my program work fines but nothing prints for result. 我的程序工作正常,但没有任何打印结果。 what's went wrong here? 这里出了什么问题?

In your GetMapping you're setting the model attribute with the variable csPreRequest which you initialized as an empty string, and an empty string is what gets rendered. 在GetMapping中,您要使用变量csPreRequest设置模型属性,该变量将初始化为空字符串,然后呈现一个空字符串。

What you should be doing is: 1. When POST /save is called, then the value passed should be persisted. 您应该做的是:1.调用POST /save ,应POST /save传递的值。 2. When GET /add is called, it should retrieve the value persisted and set in the model. 2.调用GET /add ,它应该检索持久化的值并在模型中设置。

You should try @RequestParam when return back to controller: 返回控制器时,您应该尝试@RequestParam

@PostMapping("/save")
public String addNewCourse( @RequestParam("csPreRequest") String course ) {

    System.out.println("\n Pre Reqest Courses : "+course+"\n\n");   

    return "redirect:/courses/add";

}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM