java中的接口是类吗
238
2023-04-18
SpringMVC通过注解获得参数的实例
SpringMVC可以通过RequestParam注解来映射获得参数,具体用法如下:
例子:
配置过程省略
1.新建controller类
package com.loger.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RequestParam {
public static final String SUCCESS = "success";
@RequestMapping(value="/requestparam")
public String requestParam(@org.springframework.web.bind.annotation.
RequestParam(value="username") String un,
http:// @org.springframework.web.bind.annotation.RequestParam(value="age") Integer age){
System.out.println(un + " " + age);
return SUCCESS;
}
}
2.index.jsp
运行结果:
补充:如果表单名跟方法的参数名一致的话,无需再用@RequestParam注解来映射。
如改为
@RequestMapping(value="/requestparam")
public String requestParam(String username,Integer age)即可!
用类作为参数,且包含级联属性的参数获取方法:
1.新建Adress类
package com.loger.bean;
public class Address {
private String province;
private String city;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Address [province=" + province + ", city=" + city + "]";
}
}
2.新建User类
package com.loger.bean;
public class User {
private String name;
private int age;
FIYEYprivate Address address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", address=" + address + "]";
}
}
3.controller
4.表单
User有级联属性Address,表单传入的参数是address.city address.province
姓名:
年龄:
城市:
省份:
运行结果:
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~