2016-09-18 52 views
2

我用一个简单的春天形式与胡子。但是,在Spring控制器中没有收到数据。控制器中始终接收为空的login.getId(), login.getPass()。任何线索,如果必须在模板或控制器中修复?弹簧形式的数据没有收到控制器与胡子

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

<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"; 
     } 
    } 
} 

回答

0

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

+0

如果我使用一个JSP(I可以在控制器使用它)我可以使用的ModelAttribute。在我的情况,我在HTML(模板)。 – sandeep

1

而不是name="{{pass}}"你可以使用name="pass"假设你的登录类包含一个同名的字段。另一件事是你需要Login参数附近的'@ModelAttribute'注解。

,为了便于理解如何它的工作原理,请考虑下面的例子:

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> 
+0

在互联网上搜索了很多东西后,这是我发现用胡子张贴表格数据的最佳解释。 –