【问题标题】:org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'username' is not presentorg.springframework.web.bind.MissingServletRequestParameterException:必需的字符串参数“用户名”不存在
【发布时间】:2021-03-28 19:48:13
【问题描述】:

我正在尝试使用 JSP 和 Springboot v.4 构建登录页面。即使从 login.JSP 正确传递了参数,用户名参数也变为空。我尝试使用 get 方法,可以在查询参数中正确看到用户名值,但仍然是相同的异常。为什么用户名没有传递正确的值?以下是例外情况。

2020-12-18 10:49:10.826 DEBUG 6364 --- [nio-8080-exec-8] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.nive.springboot.todoapp.controller.LoginController#showWelcomePage(String, String, ModelMap)
2020-12-18 10:49:10.832 DEBUG 6364 --- [nio-8080-exec-8] .w.s.m.m.a.ServletInvocableHandlerMethod : Could not resolve parameter [0] in public java.lang.String com.nive.springboot.todoapp.controller.LoginController.showWelcomePage(java.lang.String,java.lang.String,org.springframework.ui.ModelMap): Required String parameter 'username' is not present
2020-12-18 10:49:10.835  WARN 6364 --- [nio-8080-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'username' is not present]
2020-12-18 10:49:10.835 DEBUG 6364 --- [nio-8080-exec-8] o.s.web.servlet.DispatcherServlet        : Completed 400 BAD_REQUEST 

代码:

<%@ include file="common/header.jspf" %>
<%@ include file="common/navigation.jspf" %>

<div class="container">

    <font color="red">${errorMessage}</font>
    <form action="/login" method="post">
        Name : <input type="text" name="name" />
        Password : <input type="password" name="password" /> 
        <input type="submit" />
    </form>
</div>
<%@ include file="common/footer.jspf" %>   
@Controller
@SessionAttributes("username")
public class LoginController {
    
    @Autowired
    LoginService service;
    
    @RequestMapping(value="/login", method = RequestMethod.GET)
    public String showLoginPage(ModelMap model){
        return "login";
    }
    
    @RequestMapping(value="/login", method = RequestMethod.POST)
    public String showWelcomePage(@RequestParam String username,@RequestParam String password,ModelMap model){
        
        int isValidUser = service.validateUser(username, password);
        
        switch(isValidUser) {
        case 0: model.put("errorMessage", "User doesn't exist");
        return "register";
        case -1:model.put("errorMessage", "invalid credentials");
        return "login";
        }
    
        model.put("username", username);
        model.put("password", password);
        
        return "welcome";
    }

}
@Entity
@Table(name = "user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "user_id")
    private Long id;

    @Column(name = "email", unique = true, nullable = false)
    @Email(message = "*Please provide a valid Email")
    @NotEmpty(message = "*Please provide an email")
    private String email;

    @Column(name = "password", nullable = false)
    @Length(min = 5, message = "*Your password must have at least 5 characters")
    @NotEmpty(message = "*Please provide your password")
    @JsonIgnore
    private String password;

    @Column(name = "username", nullable = false, unique = true)
    @Length(min = 5, message = "*Your username must have at least 5 characters")
    @NotEmpty(message = "*Please provide your name")
   
    private String username;

    @Column(name = "name")
@NotEmpty(message = "*Please provide your name")
    private String name;

    @Column(name = "last_name")
   @NotEmpty(message = "*Please provide your last name")
    private String lastName;

    @Column(name = "active", nullable = false)
    private int active;
    
    @OneToMany(mappedBy = "user")
    private Collection<Todo> todo;
    
    public User() {
        
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getName() {
        return name;
    }

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

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getActive() {
        return active;
    }

    public void setActive(int active) {
        this.active = active;
    }   
}

【问题讨论】:

    标签: java spring-boot jsp


    【解决方案1】:

    在表单中提交数据时,后端代码会读取 name 属性的值。 您可以像这样修改代码:

    <%@ include file="common/header.jspf" %>
    <%@ include file="common/navigation.jspf" %>
    
    <div class="container">
    
        <font color="red">${errorMessage}</font>
        <form action="/login" method="post">
            Name : <input type="text" name="username" />
            Password : <input type="password" name="password" /> 
            <input type="submit" />
        </form>
    </div>
    <%@ include file="common/footer.jspf" %>   
    ``
    

    【讨论】:

      【解决方案2】:

      修改jsp:Name : &lt;input type="text" name="name" /&gt;Name : &lt;input type="text" name="username" /&gt;

      或 爪哇:

      public String showWelcomePage(@RequestParam String username,@RequestParam String password,ModelMap model){
      

      public String showWelcomePage(@RequestParam("name") String username,@RequestParam String password,ModelMap model){
      

      【讨论】:

        猜你喜欢
        • 2016-09-03
        • 2020-10-16
        • 1970-01-01
        • 2015-11-20
        • 2017-10-26
        • 1970-01-01
        • 1970-01-01
        • 2019-09-23
        • 2021-04-01
        相关资源
        最近更新 更多