1

when i enter:http://localhost:8080/sys_manager/admin/sys/resource/update/1009 it's can enter into this method,but the id value is null,:

    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String showUpdateForm(@PathVariable("id") ID id, Model model) {
        M m = baseService.findOne(id);
        if (permissionList != null) {
            this.permissionList.assertHasUpdatePermission();
        }
        setCommonData(model);
        model.addAttribute(Constants.OP_NAME, "修改");
        model.addAttribute("m", m);
        return viewName("editForm");
    }

One picture more than thousands of words,here is the snapshot:https://plus.google.com/photos/109577783306525329699/albums/6135767537581420673

kryger
  • 12,906
  • 8
  • 44
  • 65
vincent
  • 142
  • 1
  • 2
  • 7
  • See here http://stackoverflow.com/questions/5000876/spring-mvc-referencing-params-variable-from-requestmapping – rakeeee Apr 09 '15 at 16:01
  • thanks,I know the api.The problem actually is that it worked well yesterday, i think i havn't modified nothing about this class and it's relative jsp, – vincent Apr 10 '15 at 02:20
  • @vincent Change pathvariable type to String and try again. I think it will work – Harshal Patil Apr 10 '15 at 05:08

3 Answers3

3

thanks everyone,I had fixed this problem,it because by override. the super method

@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
public String showUpdateForm(@PathVariable("id") ID id, Model model) {
    M m = baseService.findOne(id);
    if (permissionList != null) {
        this.permissionList.assertHasUpdatePermission();
    }
    setCommonData(model);
    model.addAttribute(Constants.OP_NAME, "修改");
    model.addAttribute("m", m);
    return viewName("editForm");
}

this subclass's method as blow:

@Override
public String showUpdateForm(@PathVariable("id") Long id, Model model) {
    return super.showUpdateForm(id, model);
}

it will visit child's "showUpdateForm"(even though child'method does't has @RequestMapping).My mistake is that I don't add @PathVariable in child's method .

vincent
  • 142
  • 1
  • 2
  • 7
1

The simplest solution is to change your controller action like this

public String showUpdateForm(@PathVariable("id") Integer id, Model model) {
....

If you really want to use use a Custom class object as the PathVariable. then you have to registered a custom editor to your controller as below.

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(ID.class, new PropertyEditorSupport() {
        @Override
        public String getAsText() {
            return ((ID) this.getValue()).toString();
        }

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(new ID(text));
        }
    });
}

And also note you have to add avariable to store the text value parsed inside that class or inside this initBinder, as per your choice.

Faraj Farook
  • 14,385
  • 16
  • 71
  • 97
  • sorry,'ID' here is a generic Type,actually it's "Long" when running.and i had edit it to Long ,it won't work too.thanks – vincent Apr 10 '15 at 02:24
0

I would use the a Spring Data JPA DomainClassConverter, that way you save yourself the findOne lookup, e.g.

@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
public String showUpdateForm(@PathVariable("id") M m, Model model) {

http://docs.spring.io/spring-data/jpa/docs/1.8.0.RELEASE/reference/html/#_domainclassconverter

Brian Kates
  • 480
  • 4
  • 14
  • not working i am getting this :Cannot convert value of type [java.lang.String] to required type []: no matching editors or conversion strategy found – Ashu Jan 24 '17 at 06:24