
post 请求获取body内容
注意:>
1通过>@RequestMapping(value="/index1")
public String helloaction1(>}
2通过参数名获得:
@RequestMapping(value="/index1")public String helloaction1(String nnn){ //这里名字要与前端元素名字一致才能获得
Systemoutprintln(nnn);
return "index";
}
3通过@RequestParam注解获得:
@RequestMapping(value="/index")public String helloaction(@RequestParam(value="nnn",required=false)String nnn1, Model model){ //nnn要与前端一致,在此处可以理解为参数nnn1的别名
Systemoutprintln(nnn1);
modeladdAttribute("hello", "这是用action传过来的值:"+nnn1);
return "index";
}
4SpringMvc还能通过将vo作为参数获得vo的各个属性:
@RequestMapping(value="/index2")public String helloaction2(User user){
Systemoutprintln(usergetAccount());
Systemoutprintln(usergetPassword());
return "index";
}
package commikan;
import javalangannotation;
/
@author Mikan
@date 2015-08-04 23:39
/
@Target(ElementTypePARAMETER)
@Retention(RetentionPolicyRUNTIME)
@Documented
public @interface Param {
String value();
}
获取注解中的参数名的工具类:
package commikan;
import javalangannotationAnnotation;
import javalangreflectMethod;
/
@author Mikan
@date 2015-08-05 00:26
/
public class ParameterNameUtils {
/
获取指定方法的参数名
@param method 要获取参数名的方法
@return 按参数顺序排列的参数名列表
/
public static String[] getMethodParameterNamesByAnnotation(Method method) {
Annotation[][] parameterAnnotations = methodgetParameterAnnotations();
if (parameterAnnotations == null || parameterAnnotationslength == 0) {
return null;
}
String[] parameterNames = new String[parameterAnnotationslength];
int i = 0;
for (Annotation[] parameterAnnotation : parameterAnnotations) {
for (Annotation annotation : parameterAnnotation) {
if (annotation instanceof Param) {
Param param = (Param) annotation;
parameterNames[i++] = paramvalue();
}
}
}
return parameterNames;
}
}
测试类:
package commikan;
import javalangreflectMethod;
import javautilArrays;
/
@author Mikan
@date 2015-08-04 23:40
/
public class ParameterNameTest {
public void method1(@Param("parameter1") String param1, @Param("parameter2") String param2) {
Systemoutprintln(param1 + param2);
}
public static void main(String[] args) throws Exception {
Class<ParameterNameTest> clazz = ParameterNameTestclass;
Method method = clazzgetDeclaredMethod("method1", Stringclass, Stringclass);
String[] parameterNames = ParameterNameUtilsgetMethodParameterNamesByAnnotation(method);
Systemoutprintln(ArraystoString(parameterNames));
}
}
应该这么用
当你使用了使用@Param注解来声明参数时,如果使用 #{} 或 ${} 的方式都可以。
@Select("select column from table where userid = ${userid} ") public int selectColumn(@Param("userid") int userid);
属于重点,在系统中用到注解权限时非常有用,可以精确控制权限的粒度
注意:要想使用反射去读取注解,必须将Retention的值选为Runtime Java代码import javalangannotationAnnotation;import javalangreflectMethod;//读取注解信息public class ReadAnnotationInfoTest { public static void main(String[] args) throws Exception { // 测试AnnotationTest类,得到此类的类对象 Class c = ClassforName(comiwtxokhtdannotationAnnotationTest); // 获取该类所有声明的方法 Method[] methods = cgetDeclaredMethods(); // 声明注解集合 Annotation[] annotations; // 遍历所有的方法得到各方法上面的注解信息 for (Method method : methods) { // 获取每个方法上面所声明的所有注解信息 annotations = methodgetDeclaredAnnotations(); // 再遍历所有的注解,打印其基本信息 Systemoutprintln(methodgetName()); for (Annotation an : annotations) { Systemoutprintln(方法名为: + methodgetName() + 其上面的注解为: + anannotationType()getSimpleName()); Method[] meths = anannotationType()getDeclaredMethods(); // 遍历每个注解的所有变量 for (Method meth : meths) { Systemoutprintln(注解的变量名为: + methgetName()); } } } }}
一、mybatis传多个参数(不使用@param注解情况和使用@param注解的情况)
转自 >
以上就是关于springboot获取请求参数的几种方式可选参数使用全部的内容,包括:springboot获取请求参数的几种方式可选参数使用、后羿框架-公共子框架-报文封装、SpringMVC怎么获取前台传来的数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)