我有一个上传文件的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>
&#13;
答案 0 :(得分:0)
首先确保此表单方法是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) {
}