Java JakartaVelocity 1.7使用Spring MVC提交表单,在控制器中获取空值
我从velocity生成一个表单,并将表单值提交给spring控制器。在spring控制器中,我得到了“null”值 表单:test.vm,使用@RequestMapping/testform和post方法提交调用控制器Java JakartaVelocity 1.7使用Spring MVC提交表单,在控制器中获取空值,java,spring,forms,spring-mvc,velocity,Java,Spring,Forms,Spring Mvc,Velocity,我从velocity生成一个表单,并将表单值提交给spring控制器。在spring控制器中,我得到了“null”值 表单:test.vm,使用@RequestMapping/testform和post方法提交调用控制器 <html> <body> <form method="POST" action="/testform"> Select testtype: <select name="test"> <option va
<html>
<body>
<form method="POST" action="/testform">
Select testtype:
<select name="test">
<option value="test1">test1</option>
<option value="test2">test2</option>
</select>
<p>
<input type="submit" value ="submit">
</form>
</body>
</html>
Controller code:
@RequestMapping(value = "/testform", method = RequestMethod.POST)
public ModelAndView addtest(@ModelAttribute("test")ReadTest test, BindingResult result) {
System.out.println("in test controller");
System.out.println("test:" + test.gettest());
return new ModelAndView("test");
}
}
}
提交表单值后,控制器中的test.gettest()返回“null”
你能帮我找出哪里做错了吗?感谢您的帮助。添加commandName=“test”。它像是:
<form method="POST" action="/testform" commandName ="test">
您好,您必须为它添加命令名,如
<form method="POST" commandname="test" action="/testform">
此外,我认为您缺少ReadTest.java中的setter。请添加setter并检查一次。
请参考此示例代码。谢谢,b/w,这里的测试是什么?是在@modeldattribute(“test”)中定义的名称吗?我仍然在cotroller中看到空值,commandName属性没有解决我的问题,还尝试了modeldattribute。
<form method="POST" commandname="test" action="/testform">