How to map request "ab" to a command object in Spring MVC

I used "@RequestParam (" ab ") String foo" to get the parameter "ab" from the HTTP request.

now I want to switch to Command Object, but I do not get this parameter, I tried the following four forms: "ab", "aB", "a_b", "a_B", but it does not work, for example, the following code will look like

URL: http: // localhost: 8080 / test1? Ab = 1
Result:Foo{ab='null', aB='null', a_b='null', a_B='null'}

Thanks in advance

@Controller
public class TestController {

@RequestMapping("/test1")
public String test1(
    Foo foo,
    HttpServletResponse response
) throws IOException {
    response.setContentType("text/plain");
    response.getOutputStream().write(foo.toString().getBytes("UTF-8"));
    return null;
}


public static class Foo {

    private String ab;
    private String aB;
    private String a_b;
    private String a_B;

    // getter and setter
    ...

    @Override
    public String toString() {
        return "Foo{" +
                "ab='" + ab + '\'' +
                ", aB='" + aB + '\'' +
                ", a_b='" + a_b + '\'' +
                ", a_B='" + a_B + '\'' +
                '}';
    }
}





}
+3
source share
1 answer

- a-b, , , ? Java , , Spring MVC , , , .

, , WebBindingInitializer, WebDataBinder Initialization Spring, a-b Foo . , , WebBindingInitializers ...

+1

Source: https://habr.com/ru/post/1726302/


All Articles