前端页面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登录</title>
</head>
<body>
<form action="/SpringMVC5_pojo_param/login.action">
姓名:<input type="text" name="name"> <br />
国家:<input type="text" name="address.country"> <br />
城市:<input type="text" name="address.city"> <br />
<button type="submit">登录</button> <br />
</form>
</body>
</html>
Bean对象
UserBean :
package com.project.bean;
public class UserBean {
private String name;
private AddressBean address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public AddressBean getAddress() {
return address;
}
public void setAddress(AddressBean address) {
this.address = address;
}
@Override
public String toString() {
return "UserBean [name=" + name + ", address=" + address + "]";
}
}
AddressBean :
package com.project.bean;
public class AddressBean {
private String country;
private String city;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "AdressBean [country=" + country + ", city=" + city + "]";
}
}
action
package com.project.action;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.project.bean.UserBean;
@Controller // sping容器要加载这个注解,才能将当前类识别为控制器
public class LoginAction{
// 配置映射关系
@RequestMapping("/login.action")
public String login(UserBean user) { // 参数课按需求自定义
System.out.println("访问到 login ");
System.out.println(user);
// 跳转的页面
return "index.jsp";
}
}
前端 name
属性的值,与参数对象的属性一致,spring会自动转型并赋值(只是简单类型会自动转换)
简单参数获取
前端
<body>
<form action="/SpringMVC4_param/login.action">
<input type="text" name="id"> <br />
<input type="text" name="name"> <br />
<input type="password" name="pwd"> <br />
<button type="submit">登录</button> <br />
</form>
</body>
action
@Controller // sping容器要加载这个注解,才能将当前类识别为控制器
public class LoginAction{
// 配置映射关系
@RequestMapping("/login.action")
public String login(String name, String pwd, int id) { // 参数课按需求自定义
System.out.println("访问到 login ");
System.out.printf("name=%s, pwd=%s, id=%s", name, pwd, id);
// 跳转的页面
return "index.jsp";
}
}
网友评论