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>

猜你喜欢

转载自www.cnblogs.com/tanlei-sxs/p/10038533.html