无法将参数从html发送到控制器

时间:2017-09-06 11:57:22

标签: java spring spring-mvc

我有一个HTML:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
  </head>
  <body>
    <form name="AppE" method="post" action="http://10.18.9.10:8280/Ey/lin">
        <input type="text" name="userIdd" id="userIdd"><br/>
        <input type="text" name="passwordd" id="passwordd"><br/>
        <input type="text" name="appSerialNon" id="appSerialNon"><br/>
        <input type="submit" name="Submit">
    </form>
  </body>
</html>

/ lin去了这个控制器:

@RequestMapping(value = "/lin", method = RequestMethod.GET)
public String login(@RequestParam(required=false, value="userIdd")String userIdd, @RequestParam(required=false, value="passwordd")String passwordd,@RequestParam(required=false, value="appSerialNon")String appSerialNon ) {
    System.out.println(userIdd+" "+passwordd+" "+appSerialNon); 
    return "login/login"
}

在访问html并填充值并提交后,我被重定向到所需的页面,但我在控制台上获取空值,即我无法将参数从html发送到控制器类。

2 个答案:

答案 0 :(得分:4)

您的login()方法响应HTTP GET请求,但表单发送HTTP POST。

使用RequestMethod.POST

答案 1 :(得分:0)

您的表单正在发送帖子请求

<form name="AppE" method="post" action="http://10.18.9.10:8280/Ey/lin">

因此,让控制器接受POST次请求,将method = RequestMethod.GET更改为method = RequestMethod.POST

@RequestMapping(value = "/lin", method = RequestMethod.POST)
public String login(@RequestParam(required=false, value="userIdd")String userIdd, @RequestParam(required=false, value="passwordd")String passwordd,@RequestParam(required=false, value="appSerialNon")String appSerialNon ) {
    System.out.println(userIdd+" "+passwordd+" "+appSerialNon); 
    return "login/login"
}