SpringMVC【开发Controller】详解(三)

简介: 本文主要是讲解在Controller中的开发

接收JavaBean

我们处理表单的参数,如果表单带过来的数据较多,我们都是用JavaBean对其进行封装的。那么我们在SpringMVC也是可以这么做的。

  • 创建Javabean
  • javaBean属性与表单带过来的名称相同
  • 在业务方法上写上Javabean的名称

创建JavaBean,javaBean属性与表单带过来的名称相同

public class User {
    private String id;
    private String username;
    public User() {
    }
    public User(String id, String username) {
        this.id = id;
        this.username = username;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", username='" + username + '\'' +
                '}';
    }
}

在业务方法参数上写入Javabean

@RequestMapping(value = "/hello.action")
    public String hello(Model model,User user) throws Exception {
        System.out.println(user);
        model.addAttribute("message", "你好");
        return "/index.jsp";
    }

微信图片_20220411233230.jpg


收集数组

收集数组和收集普通的参数是类似的,看了以下的代码就懂了。

<form action="${pageContext.request.contextPath}/hello.action" method="post">
    <table align="center">
        <tr>
            <td>用户名:</td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td>爱好</td>
            <td><input type="checkbox" name="hobby" value="1">篮球</td>
            <td><input type="checkbox" name="hobby" value="2">足球</td>
            <td><input type="checkbox" name="hobby" value="3">排球</td>
            <td><input type="checkbox" name="hobby" value="4">羽毛球</td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="提交">
            </td>
        </tr>
    </table>
</form>

业务方法获取参数

@RequestMapping(value = "/hello.action")
    public String hello(Model model,int[] hobby) throws Exception {
        for (int i : hobby) {
            System.out.println("喜欢运动的编号是:" + i);
        }
        model.addAttribute("message", "你好");
        return "/index.jsp";
    }

效果:

微信图片_20220411233338.jpg

收集List<JavaBean>集合

我们在Spring的业务方法中是不可以用List这样的参数来接收的,SpringMVC给了我们另一种方案!

我们使用一个JavaBean把集合封装起来,给出对应的set和get方法。那么我们在接收参数的时候,接收的是JavaBean

/**
 * 封装多个Emp的对象 
 * @author AdminTC
 */
public class Bean {
    private List<Emp> empList = new ArrayList<Emp>();
    public Bean(){}
    public List<Emp> getEmpList() {
        return empList;
    }
    public void setEmpList(List<Emp> empList) {
        this.empList = empList;
    }
}

业务方法接收JavaBean对象

/**
     * 批量添加员工
     */
    @RequestMapping(value="/addAll",method=RequestMethod.POST)
    public String addAll(Model model,Bean bean) throws Exception{
        for(Emp emp:bean.getEmpList()){
            System.out.println(emp.getUsername()+":"+emp.getSalary());
        }
        model.addAttribute("message","批量增加员工成功");
        return "/jsp/ok.jsp";
    }

在JSP页面直接写上empList[下表].

<form action="${pageContext.request.contextPath}/emp/addAll.action" method="POST">
        <table border="2" align="center">
            <caption><h2>批量注册员工</h2></caption>
            <tr>
                <td><input type="text" name="empList[0].username" value="哈哈"/></td>
                <td><input type="text" name="empList[0].salary" value="7000"/></td>
            </tr>
            <tr>
                <td><input type="text" name="empList[1].username" value="呵呵"/></td>
                <td><input type="text" name="empList[1].salary" value="7500"/></td>
            </tr>
            <tr>
                <td><input type="text" name="empList[2].username" value="班长"/></td>
                <td><input type="text" name="empList[2].salary" value="8000"/></td>
            </tr>
            <tr>
                <td><input type="text" name="empList[3].username" value="键状哥"/></td>
                <td><input type="text" name="empList[3].salary" value="8000"/></td>
            </tr>
            <tr>
                <td><input type="text" name="empList[4].username" value="绿同学"/></td>
                <td><input type="text" name="empList[4].salary" value="9000"/></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" value="批量注册"/>
                </td>
            </tr>
        </table>
    </form>

其实这种方法看起来也没有那么难理解,我们就是向上封装了一层【与接收普通的JavaBean类似的】


收集多个模型

我们有可能在JSP页面上即有User模型的数据要收集,又有Emp模型的数据要收集….并且User模型的属性和Emp模型的属性一模一样….此时我们该怎么办呢???

我们也是可以在User模型和Emp模型上向上抽象出一个Bean,该Bean有Emp和User对象

/**
 * 封装User和Admin的对象
 * @author AdminTC
 */
public class Bean {
    private User user;
    private Admin admin;
    public Bean(){}
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public Admin getAdmin() {
        return admin;
    }
    public void setAdmin(Admin admin) {
        this.admin = admin;
    }
}

在JSP页面收集的时候,给出对应的类型就行了。

<form action="${pageContext.request.contextPath}/person/register.action" method="POST">
        <table border="2" align="center">
            <tr>
                <th>姓名</th>
                <td><input type="text" name="user.username" value="${user.username}"/></td>
            </tr>
            <tr>
                <th>月薪</th>
                <td><input type="text" name="user.salary" value="${user.salary}"></td>
            </tr>
            <tr>
                <th>入职时间</th>
                <td><input 
                        type="text" 
                        name="user.hiredate" 
                        value='<fmt:formatDate value="${user.hiredate}" type="date" dateStyle="default"/>'/></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" value="普通用户注册" style="width:111px"/>
                </td>
            </tr>
        </table>    
    </form>

目录
相关文章
|
10天前
|
XML 存储 前端开发
手动开发-实现SpringMVC底层机制--小试牛刀
手动开发-实现SpringMVC底层机制--小试牛刀
8 0
|
5月前
|
存储 JSON 前端开发
利用Spring MVC开发程序2
利用Spring MVC开发程序
45 1
|
5月前
|
JSON 网络架构 数据格式
SpringMVC -- REST风格开发,RESTful快速开发、RESTful注解开发
SpringMVC -- REST风格开发,RESTful快速开发、RESTful注解开发
68 2
|
5月前
|
设计模式 JSON 前端开发
利用Spring MVC开发程序1
利用Spring MVC开发程序
53 0
|
5月前
|
JSON 前端开发 Java
开发必备技能:探索Spring MVC请求映射和参数绑定的奇妙之旅!
开发必备技能:探索Spring MVC请求映射和参数绑定的奇妙之旅!
|
5月前
|
设计模式 前端开发 Java
Spring Boot之Spring MVC的工作原理 以及使用eclipse开发Spring MVC的Web应用实战(附源码)
Spring Boot之Spring MVC的工作原理 以及使用eclipse开发Spring MVC的Web应用实战(附源码)
85 0
|
5月前
|
前端开发
SpringMVC-RESTful快速开发及案例:基于RESTful页面数据交互
SpringMVC-RESTful快速开发及案例:基于RESTful页面数据交互
73 0
|
消息中间件 前端开发 Java
GitHub标星30k!基于Spring MVC Mybatis分布式开发系统-zheng项目
zheng项目不仅仅是一个开发架构,而是努力打造一套从 前端模板 - 基础框架 - 分布式架构 - 开源项目 - 持续集成 - 自动化部署 - 系统监测 - 无缝升级 的全方位J2EE企业级开发解决方案。
|
SQL JSON 前端开发
SpringMvc+Spring+MyBatis+Maven+Ajax+Json注解开发 利用Maven的依赖导入不使用架包模式 (实操十二)
SpringMvc+Spring+MyBatis+Maven+Ajax+Json注解开发 利用Maven的依赖导入不使用架包模式 (实操十二)
71 0
|
JSON 前端开发 Java
SpringMvc+Spring+MyBatis+Maven+Ajax+Json注解开发 利用Maven的依赖导入不使用架包模式 (实操十一)
SpringMvc+Spring+MyBatis+Maven+Ajax+Json注解开发 利用Maven的依赖导入不使用架包模式 (实操十一)
83 0