最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

[Spring MVC] -簡單表單提交實(shí)例

 更新時(shí)間:2016年12月12日 15:34:56   作者:橫渡  
本篇文章主要介紹了[Spring MVC] -簡單表單提交實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。

Spring MVC自帶的表單標(biāo)簽比較簡單,很多時(shí)候需要借助EL和JSTL來完成。

下面是一個(gè)比較簡單的表單提交頁面功能: 

1、User model

package com.my.controller.bean;

import java.util.Date;
import java.util.List;

import javax.validation.constraints.Future;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

public class User {
  
  private long id;
  
  @Length(min=2, max=50, message="User name length range = 2-50")
  private String name;
  
  @Future(message="時(shí)間不能小于今天")
  private Date createTime;
  
  @NotEmpty(message="Customer不能為空")
  private List<Customer> customers;
  
  @NotNull(message="Girl不能為空")
  private boolean girl;
  
  private String[] cbx;
  
  @NotNull(message="Age can NOT be Null")
  @Min(value=18, message="最小18歲")
  @Max(value=100, message="最大100歲")
  private int age;
  
  @Email(message="Email格式不正確")
  private String email;
  
  public long getId() {
    return id;
  }
  public void setId(long id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public Date getCreateTime() {
    return createTime;
  }
  public void setCreateTime(Date createTime) {
    this.createTime = createTime;
  }
  public List<Customer> getCustomers() {
    return customers;
  }
  public void setCustomers(List<Customer> customers) {
    this.customers = customers;
  }
  public boolean isGirl() {
    return girl;
  }
  public void setGirl(boolean girl) {
    this.girl = girl;
  }
  public String[] getCbx() {
    return cbx;
  }
  public void setCbx(String[] cbx) {
    this.cbx = cbx;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public String getEmail() {
    return email;
  }
  public void setEmail(String email) {
    this.email = email;
  }

}

2、Controller

package com.my.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.my.controller.bean.Customer;
import com.my.controller.bean.User;

@Controller
@RequestMapping(value="/post")
public class TestPostController {
  
  private static List<User> users = new ArrayList<User>();
  {
    //-----------------------------------------------
    // 設(shè)置Entity
    // -----------------------------------------------
    users.add(new User());
    User user = users.get(0);
    user.setId(1);
    user.setName("Robin");
    user.setCreateTime(new Date());
    user.setGirl(true);
    user.setCbx(new String[] {"1", "2", "3"});
    user.setAge(18);
    user.setEmail("abcd@abc.com");
    
    user.setCustomers(new ArrayList<Customer>());
    Customer customer1 = new Customer();
    customer1.setId(1);
    customer1.setCompany("Company - 1");
    customer1.setCreateTime(new Date());
    customer1.setUser(user);
    user.getCustomers().add(customer1);
    
    Customer customer2 = new Customer();
    customer2.setId(1);
    customer2.setCompany("Company - 2");
    customer2.setCreateTime(new Date());
    customer2.setUser(user);
    user.getCustomers().add(customer2);
  }
  
  @RequestMapping
  public ModelAndView index() {
    ModelAndView view = new ModelAndView("TestPost/index");
    view.addObject("users", users);
    return view;
  }
  
  @RequestMapping(value="/addUser", method=RequestMethod.POST)
  public ModelAndView addUser(@ModelAttribute @Valid User user, BindingResult result) {
    ModelAndView view = new ModelAndView("redirect:/post");
    
    if(result.hasErrors()) {
      List<FieldError> errors = result.getFieldErrors();
      for(FieldError err : errors) {
        System.out.println("ObjectName:" + err.getObjectName() + "\tFieldName:" + err.getField()
            + "\tFieldValue:" + err.getRejectedValue() + "\tMessage:" + err.getDefaultMessage());
      }
      view.addObject("users", users);
      return view;
    }
    
    user.setId(users.size() + 1);
    user.getCustomers().get(0).setId(1);
    user.getCustomers().get(0).setUser(user);
    users.add(user);
    view.addObject("users", users);
    return view;
  }
  
}

3、View

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.my.controller.bean.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib prefix="st" uri="http://www.springframework.org/tags" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index</title>
</head>
<body>
  <fmt:setLocale value="zh_cn" />
  <form action="<st:url value="/post/addUser"></st:url>" method="post">
    <c:forEach items="${users}" var="user">
      User:${user.name}<br/>
      Create time:<fmt:formatDate value="${user.createTime}"/><br/>
      Is girl:
      <c:choose>
        <c:when test="${user.girl}">Yes</c:when>
        <c:when test="${!user.girl}">No</c:when>
        <c:otherwise>N/A</c:otherwise>
      </c:choose>
      <br/>
      Checkboxs:
      <c:forEach items="${user.cbx}" var="item">
        ${item},
      </c:forEach>
      <br/>
      Age:${user.age}<br/>
      E-mail:${user.email}<br/>
      <hr/>
    
      <table style="width:100%;border:1px solid #ccc;">
        <thead>
          <tr style="text-align:left; background-color:#eee;">
            <th>Company name</th>
            <th>User</th>
            <th>Create time</th>
          </tr>
        </thead>
        <tbody>
          <c:forEach items="${user.customers}" var="item">
          <tr>
            <td>${item.company}</td>
            <td>${item.user.name}</td>
            <td><fmt:formatDate value="${item.createTime}" pattern="yyyy-MM-dd"/></td>
          </tr>
          </c:forEach>
        </tbody>
      </table>
      <hr/>
    </c:forEach>
    
    User name:
    <input type="text" name="name" id="name" /><br/>
    Is girl:
    <input type="radio" name="girl" id="isGirl" value="true" checked="checked" /><label for="isGirl">Yes</label>
    <input type="radio" name="girl" id="noGirl" value="false" /><label for="noGirl">No</label><br/>
    Checkboxs:
    <input type="checkbox" name="cbx" id="cbx1" value="1" /><label for="cbx1">1</label>
    <input type="checkbox" name="cbx" id="cbx2" value="2" /><label for="cbx2">2</label>
    <input type="checkbox" name="cbx" id="cbx3" value="3" /><label for="cbx3">3</label>
    <br/>
    Age:<input type="text" name="age" id="age" /><br/>
    E-mail:<input type="text" name="email" id="email" /><br/>
    Create time:
    <input type="text" name="createTime" id="createTime" /><br/>
    Company:
    <input type="text" name="customers[0].company" id="customers[0].company" /><br/>
    <input type="submit" value="add" />
    <sf:errors path="*"></sf:errors>
  </form>
  <hr/>
</body>
</html>

4、測試結(jié)果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring中的事務(wù)操作、注解及XML配置詳解

    Spring中的事務(wù)操作、注解及XML配置詳解

    這篇文章主要給大家介紹了關(guān)于Spring中事務(wù)操作、注解及XML配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • java自定義填充excel并導(dǎo)出的方法代碼實(shí)例

    java自定義填充excel并導(dǎo)出的方法代碼實(shí)例

    這篇文章主要給大家介紹了關(guān)于java自定義填充excel并導(dǎo)出的相關(guān)資料,使用Java在Spring框架中實(shí)現(xiàn)一個(gè)接口,該接口可以將JSON數(shù)據(jù)導(dǎo)出為Excel文件,文章涵蓋了從加載Excel模板、創(chuàng)建單元格樣式到填充數(shù)據(jù)并返回響應(yīng)的整個(gè)過程,需要的朋友可以參考下
    2024-12-12
  • SpringMVC文件上傳 多文件上傳實(shí)例

    SpringMVC文件上傳 多文件上傳實(shí)例

    這篇文章主要介紹了SpringMVC文件上傳 多文件上傳實(shí)例,有需要的朋友可以參考一下
    2014-01-01
  • 解決java.sql.SQLException:The?server?time?zone?value?'?D1ú±ê×?ê±??'?is?unrecognized問題

    解決java.sql.SQLException:The?server?time?zone?value?&apo

    這篇文章主要介紹了解決java.sql.SQLException:The?server?time?zone?value?'?D1ú±ê×?ê±??'?is?unrecognized問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Java通過word模板實(shí)現(xiàn)創(chuàng)建word文檔報(bào)告

    Java通過word模板實(shí)現(xiàn)創(chuàng)建word文檔報(bào)告

    這篇文章主要為大家詳細(xì)介紹了Java如何通過word模板實(shí)現(xiàn)創(chuàng)建word文檔報(bào)告的教程,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下
    2022-09-09
  • SpringBoot開啟Swagger并配置基本信息方式

    SpringBoot開啟Swagger并配置基本信息方式

    這篇文章主要介紹了SpringBoot開啟Swagger并配置基本信息方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • SpringCloud入門實(shí)驗(yàn)環(huán)境搭建

    SpringCloud入門實(shí)驗(yàn)環(huán)境搭建

    這篇文章主要介紹了SpringCloud入門實(shí)驗(yàn)環(huán)境搭建的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用SpringCloud,感興趣的朋友可以了解下
    2021-04-04
  • 詳解Spring系列之@ComponentScan自動掃描組件

    詳解Spring系列之@ComponentScan自動掃描組件

    這篇文章主要介紹了Spring @ComponentScan自動掃描組件使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • 淺談Java多線程實(shí)現(xiàn)及同步互斥通訊

    淺談Java多線程實(shí)現(xiàn)及同步互斥通訊

    下面小編就為大家?guī)硪黄獪\談Java多線程實(shí)現(xiàn)及同步互斥通訊。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • Java如何正確處理下載文件時(shí)HTTP頭的編碼問題

    Java如何正確處理下載文件時(shí)HTTP頭的編碼問題

    這篇文章主要介紹了Java如何正確處理下載文件時(shí)HTTP頭的編碼問題,
    通常HTTP消息包括客戶機(jī)向服務(wù)器的請求消息和服務(wù)器向客戶機(jī)的響應(yīng)消息,今天來講解下正確處理下載文件時(shí)HTTP頭的編碼問題,需要的朋友可以參考下
    2023-07-07

最新評論

东至县| 武义县| 博白县| 同江市| 桐乡市| 什邡市| 九龙坡区| 安西县| 炎陵县| 永登县| 景洪市| 壶关县| 神池县| 江阴市| 广德县| 安图县| 大邑县| 武邑县| 洮南市| 德江县| 连城县| 扎鲁特旗| 高碑店市| 威宁| 乐东| 玉龙| 肥西县| 慈溪市| 邢台县| 个旧市| 娄底市| 古田县| 航空| 交城县| 东平县| 应用必备| 寿宁县| 汝州市| 洮南市| 磐安县| 六安市|