【问题标题】:Upload a file with year and month in jsp. And get year ,month ,check box value and file in controller在jsp中上传带有年份和月份的文件。并在控制器中获取年、月、复选框值和文件
【发布时间】:2016-08-11 12:04:55
【问题描述】:

我有一个 jsp 文件,它上传一个文件并包含两个选择年份和月份的下拉菜单和一个复选框。提交后,我希望这些值在控制器中。我正在使用 java spring MVC 框架。我知道应该使用 post 方法发送文件类型,但我怎样才能使用其他值?

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="sp" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!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=ISO-8859-1">
<title>Upload File</title>
<link href="<c:url value="/resources/bootstrap.css" />"  rel="stylesheet" type="text/css"></link>
<link href="<c:url value="/resources/bootstrap.min.css" />"  rel="stylesheet" type="text/css"></link>
<link href="<c:url value="/resources/payslip-style.css" />" rel="stylesheet" type="text/css"></link>
  </head>
  <body>
  <form action ="verifyFile" id="myform" enctype="multipart/form-data">
				<div class="dropdown dropdown-inline">
					<select class="form-control" id="dd1" name="selectYear" >
						<option value="" label="Select Year"/>
							<c:forEach var="year" items="${years}">
								<option value="${year}"><c:out value="${year}"></c:out>
								</option>
							</c:forEach>
              		</select>
              	</div>
				<div class="dropdown dropdown-inline">
					<select  class="form-control" id="dd2" name="dd2">
						<option value="" label="select Month"/>
							<c:forEach items="${monthsList}" var="months">
								<option value="${months}"><c:out value="${months}"></c:out>
								</option>
							</c:forEach>
                	</select>
                </div><br/><br/>
				<div class="upload-file">
					<b>Header :</b><input type="checkbox" name="header"></input><br/><br/>
					<label class="btn btn-default btn-file">
    				Browse File  <input type="file" name="file" style="display: none;" accept=".csv" id="f1" ></input>
					</label>
					<label><span class='label label-info' id="upload-file-info"></span></label><br/><br/>
					<input class="btn btn-primary" type="submit" id="Upload" value="Upload" onclick="validate();"></input>
					<input class="btn btn-primary" type="reset" name="Reset"></input><br/><br/>
				</div>
			</form>
</body>
</html>

【问题讨论】:

    标签: java spring jsp spring-mvc model-view-controller


    【解决方案1】:

    首先确保这个表单方法是 POST。

    <form action ="verifyFile" id="myform" enctype="multipart/form-data" method="POST">
    

    然后在你的控制器中为这篇文章创建一个方法:

    @RequestMapping(value = "/verifyFile", method = RequestMethod.POST)
       public String addStudent(@RequestParam("dd1") String dd1, @RequestParam("dd2") String dd2,@RequestParam("file") MultipartFile file) {
    
       }
    

    【讨论】:

    • 它给出'必需的字符串参数'dd1'不存在'错误。
    • 我遇到了同样的错误。必需的字符串参数“selectYear”不存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多