设为首页 加入收藏

TOP

SpringMVC入门 -- 参数绑定(六)
2019-09-19 11:11:58 】 浏览:123
Tags:SpringMVC 入门 参数 绑定
troller
public class HelloController { @RequestMapping(value = "/testCookieva lue/{id}") public ModelAndView testCookieva lue(@PathVariable("id") Integer id, @Cookieva lue("JSESSIONID") String jsessionId) { ModelAndView model = new ModelAndView("hello"); model.getModel().put("msg", id + "," + jsessionId); model.getModel().put("message", "test Cookieva lue"); return model; } }

 

 

 

 

6、自动给对象绑定请求参数值

(1)简介
  SpringMVC可以根据请求参数名以及对象(POJO,Plain Ordinary Java Object)的属性名 自动匹配,自动为该对象绑定属性值,同时支持级联属性(如:"dept.salary")。

【新建实体类:DeptEntity.javapackage com.lyh.entity;

public class DeptEntity {
    private String deptName;
    private Double salary;

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "DeptEntity{" +
                "deptName='" + deptName + '\'' +
                ", salary=" + salary +
                '}';
    }
}

【新建实体类:PersonEntity.javapackage com.lyh.entity;

public class PersonEntity {
    private String name;
    private String password;
    private DeptEntity dept;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public DeptEntity getDept() {
        return dept;
    }

    public void setDept(DeptEntity dept) {
        this.dept = dept;
    }

    @Override
    public String toString() {
        return "PersonEntity{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", dept=" + dept +
                '}';
    }
}

【index.jsp】
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isErrorPage="true"%>
<html>
<head>
    <title>首页</title>
</head>
<body>
<form action="/testPOJO/1">
    <label for="name">姓名</label>
    <input type="text" id="name" name ="name" placeholder="请输入姓名..." />

    <label for="password">密码</label>
    <input type="password" id="password" name ="password" placeholder="请输入密码..." />

    <label for="deptName">部门名</label>
    <input type="text" id="deptName" name ="dept.deptName" placeholder="请输入部门名..." />

    <label for="salary">工资</label>
    <input type="text" id="salary" name ="dept.salary" placeholder="请输入工资..." />
    <input type="submit" value="test POJO"/>
</form>
</body>
</html>

【hello.jsp】
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isErrorPage="
首页 上一页 3 4 5 6 7 下一页 尾页 6/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇XXLJOB2.1.0数据源配置踩坑记录 下一篇Spring中@Component与@Bean的区别

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目