Spring mvc:如何将表单数据从jsp传递给控制器

时间:2016-04-19 09:12:56

标签: java jsp spring-mvc

假设我在jsp中有这个表单

<form action="save" method="post">
 <input type="text" name="name" value="${person.name}">
 <input type="text" name="age" value="${person.age}">
 <input type="submit" value="Save">
</form>

和Person类

public class Person {
 private String name;
 private String age;
 //getters and setters
}

控制器类

public class Controller {
 @RequestMapping(value="/save")
 public ModelAndView save(Person person){
  //save person
 }
}

这可能吗?或者我需要添加一些代码才能使其工作?我使用的是spring mvc 4.0.3。 提前致谢。 :)

编辑:我尝试了@ModelAttribute,但是我收到错误400.尝试使用@RequestBody但错误415.是否可以不使用注释?

编辑:控制器实施

@Controller
public class PersonController {

    @Autowired
    private PersonService personService;

    @RequestMapping(value="/", method=RequestMethod.GET)
    public ModelAndView list(){
        List<PersonDto> personDtos = personService.getAllPersons();
        ModelAndView model = new ModelAndView("index");
        model.addObject("persons",personDtos);
        model.addObject("roles",personService.getRoles());
        return model;
    }

    @RequestMapping(value="view/{personId}", method = RequestMethod.POST)
    public ModelAndView view(@PathVariable String personId){
        PersonDto personDto = new PersonDto();
        if (personId != null){
            personDto = personService.getPersonById(Integer.parseInt(personId));
            ModelAndView model = new ModelAndView("person");
            model.addObject("person",personDto);
            return model;
        }
        else{
            return new ModelAndView("redirect:/?message=Please select one person");
        }
    }   


    @RequestMapping(value="/add")
    public ModelAndView add(){
        ModelAndView model = new ModelAndView("person");
        return model;
    }

    @RequestMapping(value="/update", method = RequestMethod.GET)
    public ModelAndView update(@RequestParam(value="personId",required=false) int[] personId){
        PersonDto personDto = new PersonDto();
        if (personId != null && personId.length == 1){
            personDto = personService.getPersonById(personId[0]);
            ModelAndView model = new ModelAndView("person");
            model.addObject("person",personDto);
            return model;
        }
        else{
            return new ModelAndView("redirect:/?message=Please select one person");
        }
    }

    @RequestMapping(value="/delete", method = RequestMethod.POST)
    public ModelAndView delete(@RequestParam(value="personId",required=false) int[] personId){
        if(personId != null){   
            for(int id : personId){
                personService.deletePerson(id);
            }
            return new ModelAndView("redirect:/?message=Person/s deleted");
        }else{
            return new ModelAndView("redirect:/?message=Please select person/s to delete");
        }
    }

    @RequestMapping(value="/search", method = RequestMethod.GET)
    public ModelAndView search(@RequestParam(value="firstName",required=false) String firstName,
        @RequestParam(value="middleName",required=false) String middleName,
        @RequestParam(value="lastName",required=false) String lastName,
        @RequestParam(value="roles",required=false) String roles){
        ModelAndView model = new ModelAndView("index");
        List<PersonDto> personDtos = null;
        if (lastName.isEmpty() && firstName.isEmpty() && middleName.isEmpty() && roles.isEmpty()){
            personDtos = personService.getAllPersons();
        }
        else{
            personDtos = personService.searchPerson(lastName,firstName,
            middleName,roles);
        }
        model.addObject("persons",personDtos);
        model.addObject("roles",personService.getRoles());
        return model;
    }

    @RequestMapping(value="/test",method = RequestMethod.POST)
    public ModelAndView test(PersonDto personDto){
        System.out.println(personDto);
        return new ModelAndView("redirect:/?message="+"Person saved");
    }
}

调用其他方法我没有问题但是测试。

1 个答案:

答案 0 :(得分:1)

你的Controller方法和类应该是这样的

@Controller
public class Controller {

 @RequestMapping(value="/save", method=RequestMethod.POST")
 public ModelAndView save(Person person){
  //save person

 return new ModelAndView("your url where you want to redirect after saing person");
 }
}