我在Spring MVC中很新。我已经被要求查询字符串应该使用POST方法将数据发送给控制器。我知道它如何与GET协同工作,但我不知道如何使用POST方法将表单值附加到查询字符串中。我正在做这样的事情,使用方法POST将查询字符串从视图发送到控制器
<form action="/user/userId?firstname={firstname}&lastname={lastname}" method="POST">
Enter First Name<input type="text" name = "firstname" />
Enter Last Name<input type="text" name = "lastname" />
<input type="submit" value = "Submit" />
</form>
我想附加firstname和lastname到字符串。我应该怎么做?
这是我的控制器类
@RequestMapping(value = "/user/userId",method = RequestMethod.POST)
public ModelAndView submitForm(@RequestParam Map<String,String> queryUser)
{
System.out.println(queryUser.get("firstName"));
context = new ClassPathXmlApplicationContext("beans.xml");
Service service = (Service) context.getBean(Service.class);
/*service.save(queryUser);*/
ModelAndView model = new ModelAndView("SecondPage");
return model;
}
你为什么使用'Map'?如果你知道你想要的参数,只需直接为它们声明'@ RequestParam'注解参数。您不需要将它们放入URL中,这些是表单参数。 –
为什么你要为每个请求初始化一个新的'ApplicationContext'? –
我忘了删除applicationcontext对象。 我的问题是如何将表单值附加到查询字符串的名字和姓氏?@SotiriosDelimanolis – chan