我必须在jsp中的url中传递相当多的参数,这些参数从jsp发送到spring mvc控制器。其中一些可以组合成一个对象,而另一些则不能。我正在使用一个表单来组合一些参数,这样我就不会弄乱我的控制器。
@RequestMapping(value = "/action")
public @ResponseBody List<RandomObject> myAction(
@RequestParam(value = "abc", required = false) int abc,
@RequestParam(value = "cat", required = false) String cat,
@RequestParam(value = "dog", required = false) String dog,
@RequestParam(value = "reindeer", required = false) String reindeer,
@RequestParam(value = "deer", required = false) int deer) { ... }
所以除了abc之外,其他的都可以转换成以猫、狗、驯鹿、鹿为属性的对象。
public class RandomObject {
private String cat;
private String dog;
private String deer;
private String reindeer;
//Getters and setters
...
}
我的问题是如何在控制器中接收它?我没有使用 AJAX。我徒劳地尝试了类似的东西
@RequestMapping(value = "/action")
public @ResponseBody List<RandomObject> myAction(
@RequestParam(value = "page", required = false) int page,
RandomObject object,) {
和
@RequestMapping(value = "/action")
public @ResponseBody List<RandomObject> myAction(
@RequestParam(value = "page", required = false) int page,
@RequestParam(value = "object", required = false) RandomObject object,) {
和
@RequestMapping(value = "/action")
public @ResponseBody List<RandomObject> myAction(
@RequestParam(value = "page", required = false) int page,
@ModelAttribute(value = "object", required = false) RandomObject object,) {
但没有一个奏效。请注意我没有使用 AJAX。