通过@pathvariabl注解获取路径中传递参数java
@requestmapping(value = "/{id}/{str}")
public modelandview helloworld(@pathvariable string id, @pathvariable string str) {
system.out.println(id);
system.out.println(str);
return new modelandview("/helloworld");
}
用@modelattribute注解获取post请求的form表单数据jsp
<form method="post" action="hao.do">
a: <input id="a" type="text" name="a"/>
b: <input id="b" type="text" name="b"/>
<input type="submit" value="submit" />
form>
java pojo
public class pojo{
private string a;
private int b;
}
java controller
@requestmapping(method = requestmethod.post)
public string processsubmit(@modelattribute("pojo") pojo pojo) {
return "helloworld";
}
直接用httpservletrequest获取java
@requestmapping(method = requestmethod.get)
public string get(httpservletrequest request, httpservletresponse response) {
system.out.println(request.getparameter("a"));
return "helloworld";
}
用注解@requestparam绑定请求参数a到变量a当请求参数a不存在时会有异常发生,可以通过设置属性required=false解决,
例如: @requestparam(value="a", required=false)
java
@requestmapping(value = "/requestparam", method = requestmethod.get)
public string setupform(@requestparam("a") string a, modelmap model) {
system.out.println(a);
return "helloworld";}