我是靠谱客的博主 高大滑板,最近开发中收集的这篇文章主要介绍SpringMvc使用POJO绑定参数,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

使用POJO绑定参数

Address

package com.chenny.entity;

public class Address {
    private Integer id;
    private String name;

    @Override
    public String toString() {
        return "Address{" +
                "id=" + id +
                ", name='" + name + ''' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Address() {
    }

    public Address(Integer id, String name) {
        this.id = id;
        this.name = name;
    }
}

User

package com.chenny.entity;

public class User {
    private Integer id;
    private String name;
    private String age;
    private Address address;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", age='" + age + ''' +
                ", address=" + address +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public User() {
    }

    public User(Integer id, String name, String age, Address address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }
}

UserHandler

package com.chenny.controller;


import com.chenny.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserHandler {

    @RequestMapping("/add")
    public String add(User user){
        System.out.println(user);
        return "index";
    }
}

add.jsp
<%--
  Created by IntelliJ IDEA.
  User: 73981
  Date: 2020/9/12
  Time: 17:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<form action="/user/add" method="post">

    ID: <input type="text" name="id"><br>
    姓名: <input type="text" name="name"><br>
    年龄: <input type="text" name="age"><br>
    地址: <input type="text" name="address.id"><br>
    地址名: <input type="text" name="address.name"><br>
    <input type="submit" value="提交">
</form>
</body>
</html>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

返回目录

最后

以上就是高大滑板为你收集整理的SpringMvc使用POJO绑定参数的全部内容,希望文章能够帮你解决SpringMvc使用POJO绑定参数所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部