栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

[Spring MVC]

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[Spring MVC]

Spring MVC自带的表单标签比较简单,很多时候需要借助EL和JSTL来完成。

下面是一个比较简单的表单提交页面功能: 

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="时间不能小于今天")
  private Date createTime;
  
  @NotEmpty(message="Customer不能为空")
  private List 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 getCustomers() {
    return customers;
  }
  public void setCustomers(List 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 users = new ArrayList();
  {
    //-----------------------------------------------
    // 设置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 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 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" %>





Index


  
  
  

4、测试结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/148429.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号