我必须在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。