• SpringMvc表单标签库


    HTML密码框

    <td><form:label path="password">密码:</form:label></td>
    <td><form:password path="password" /></td>

    呈现HTML文本内容

     <td><form:label path="address">地址:</form:label></td>
     <td><form:textarea path="address" rows="5" cols="30" /></td>

    呈现HTML复选框

    <td><form:label path="receivePaper">订阅新闻?</form:label></td>
    <td><form:checkbox path="receivePaper" /></td>

    呈现HTML单选框

    <form:radiobutton path="gender" value="M" label="男" />
    <form:radiobutton path="gender" value="F" label="女" />

    多选单选按钮

    <form:radiobuttons path="favoriteNumber" items="${numbersList}" />

    下拉框

    <tr>
                    <td><form:label path="country">所在国家:</form:label></td>
                    <td><form:select path="country">
                            <form:option value="NONE" label="请选择..." />
                            <form:options items="${countryList}" />
                        </form:select></td>
    </tr>

    Spring MVC隐藏字段域

    <tr>
            <td></td>
            <td><form:hidden path="id" value="1000" /></td>
    </tr>

    错误处理

     <td><form:errors path="name" cssClass="error" /></td>

    文件上传

    package com.com.tanlei.Model;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.util.FileCopyUtils;
    import org.springframework.validation.BindingResult;
    import org.springframework.validation.annotation.Validated;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.multipart.MultipartFile;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.ServletContext;
    import java.io.File;
    import java.io.IOException;
    
    @Controller
    public class FileUploadController {
    
        @Autowired
        ServletContext context;
    
        @RequestMapping(value = "/fileUploadPage", method = RequestMethod.GET)
        public ModelAndView fileUploadPage(){
            FileModel file=new FileModel();
            ModelAndView modelAndView=new ModelAndView("fileUpload", "command", file);
            return modelAndView;
        }
    
        @RequestMapping(value="/fileUploadPage", method = RequestMethod.POST)
        public String fileUpload(@Validated FileModel file, BindingResult result, ModelMap model){
            if (result.hasErrors()){
                System.out.println("validation errors");
                return  "fileUploadPage";
            }else{
                System.out.println("Fetching file");
                MultipartFile multipartFile = file.getFile();
                String uploadPath = context.getRealPath("") + File.separator + "temp" + File.separator;
                //Now do something with file...
                try {
                    FileCopyUtils.copy(file.getFile().getBytes(), new File(uploadPath+file.getFile().getOriginalFilename()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String fileName = multipartFile.getOriginalFilename();
                model.addAttribute("fileName", fileName);
                return "success";
    
            }
    
    
        }
    }
    package com.com.tanlei.Model;
    
    import org.springframework.web.multipart.MultipartFile;
    
    public class FileModel {
        private MultipartFile file;
    
        public MultipartFile getFile() {
            return file;
        }
    
        public void setFile(MultipartFile file) {
            this.file = file;
        }
    }
    <%@ page contentType="text/html; charset=UTF-8"%>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    <html>
    <head>
        <title>Spring MVC上传文件示例</title>
    </head>
    <body>
    <form:form method="POST" modelAttribute="fileUpload" enctype="multipart/form-data">
        请选择一个文件上传 :
        <input type="file" name="file" />
        <input type="submit" value="提交上传" />
    </form:form>
    </body>
    </html>
    <%@ page contentType="text/html; charset=UTF-8"%>
    <html>
    <head>
        <title>Spring MVC上传文件示例</title>
    </head>
    <body>
    文件名称 :
    <b> ${fileName} </b> - 上传成功!
    </body>
    </html>
  • 相关阅读:
    python例子-开始一个Django项目
    python例子-Django常用命令
    PHP
    Yii 关于 find findAll 查找出制定的字段的方法
    new static() 和 new self() 的区别异同
    Android
    PHP里10个鲜为人知但却非常有用的函数
    PHP 各种函数
    Yii 框架里数据库操作详解-[增加、查询、更新、删除的方法 'AR模式']
    PHP中使用curlL实现GET和POST请求的方法
  • 原文地址:https://www.cnblogs.com/tanlei-sxs/p/10038533.html
Copyright © 2020-2023  润新知