带有 Spring MVC 的参数

Args with Spring MVC

我需要帮助在我的控制器中解析一个方法中的参数。

我有一个用于将我的参数发送到函数的表单

    <form method="post" action="/">
<input type="text" id="query" placeholder="file to search ...">
<input type="submit" id="submit" value="fetch!">
    </form>

在我的控制器中:

@RestController
public class mainController {

    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public String index(Model model) throws IOException, GeneralSecurityException {
        DriveQuickstart drive = new DriveQuickstart("c:/temp/credentials.json");
        model.addAttribute("query");
        String res = drive.checkFile("query");

        return res;

查询是通过表单发送的字符串。和 return res 在同一视图中。 你有什么建议吗?

非常感谢你

在Spring MVC中会是这样的:

@Controller
public class mainController {

@PostMapping( "/index")
public String index(@ModelAttribute FormDataObjectClass object) throws IOException, GeneralSecurityException {
    DriveQuickstart drive = new DriveQuickstart("c:/temp/credentials.json");

    //model.addAttribute("query");
    String name = object.getName();
    String address = object.getAddress();
    String res = drive.checkFile("query");

    return res;
}

这里不需要传递 Model 作为参数,因为我们需要使用自定义对象 (FormDataObjectClass)。 根据 HTML form/JQuery post 方法

中的数据创建 class FormDataObjectClass