带有胡子的控制器中不会收到弹簧形式数据

时间:2016-09-18 16:21:28

标签: spring forms spring-mvc post mustache

我使用带有小胡子的简单Spring表单。但是,Spring控制器中未收到数据。 login.getId(), login.getPass()始终在控制器中作为null接收。如果必须在模板或控制器中修复某些内容,有什么线索吗?

我的模板和控制器代码如下。

<form class="form-signin" id="loginForm" action="{{{appCtxt}}}/login" method="post">
    <label for="inputEmail" class="sr-only">Email address</label>
    <input type="email" name="{{id}}" id="id" class="form-control" placeholder="Email address" required autofocus>
    <label for="inputPassword" class="sr-only">Password</label>
    <input type="password" name="{{pass}}" id="pass" class="form-control" placeholder="Password" required>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>

控制器

@Controller
public class LoginController {

    private LoginService loginService;

    @Autowired
    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request, Model model) {
        ModelAndView result = new ModelAndView();
        model.addAttribute("login", new Login());
        result.addObject("resources", request.getContextPath() + "/resources");
        result.addObject("appCtxt", request.getContextPath());
//      return "redirect:/users";
        result.setViewName("home");
        return result;
    }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String login(Login login, HttpServletRequest request){
        boolean status = loginService.verifyLogin(login.getId(), login.getPass());
        if(status == true) {
            return "redirect:/users";
        }
        else
        {
            return "error";
        }
    }
}

2 个答案:

答案 0 :(得分:2)

假设您的Login类包含一个名称相同的字段,而不是name="{{pass}}",您可以使用name="pass"。另一件事是你需要&#39; @ ModelAttribute&#39; Login参数附近的注释。

为了更容易理解其工作原理,请考虑以下示例:

<强> Student.java

package me.disper.model;

public class Student {
    private String name;
    private String surname;

    public String getName() {
        return name;
    }

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

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }
}

<强> student.html

<!DOCTYPE html>
<html lang="en" xmlns:form="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>Create new student</title>
</head>
<body>
    <form name="student" action="add" method="post">
        Name: <input type="text" name="name" /><br/>
        Surname: <input type="text" name="surname" /><br/>
        <input type="submit" value="Save" />
    </form>
</body>
</html>

<强> MyMustacheController.java

package me.disper;

import me.disper.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyMustacheController {

    @GetMapping("/student")
    public ModelAndView createStudent(){
        ModelAndView modelAndView = new ModelAndView("student", "student", new Student());
        return modelAndView;
    }

    @PostMapping("/add")
    public ModelAndView addStudent(@ModelAttribute Student student){
        ModelAndView modelAndView = new ModelAndView("created", "student", student);
        return modelAndView;
    }

}

<强> created.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Student created</title>
</head>
<body>
{{#student}}
Hello {{name}} {{surname}}
{{/student}}
</body>
</html>

答案 1 :(得分:0)

查看以下教程:A guide to forms in Spring MVC。有一些有用的提示,如@ModelAttribute