如何将控制器类中的变量值赋予JSP页面

时间:2017-12-01 20:14:10

标签: java jsp spring-mvc

我的控制器类中有一个变量inputfilepath,这个变量保存了上传文件的路径。我想在JSP页面中使用路径用于某些目的。请告诉我该怎么做。下面是控制器方法代码:

@RequestMapping(value="/UploadFile", method = RequestMethod.POST)
public @ResponseBody
ModelAndView uploadFileHandler(@RequestParam("file") MultipartFile file) throws IOException, ParseException{
String fileName=file.getOriginalFilename();
byte[] bytes = file.getBytes();
File serverFile = new File(dir.getAbsolutePath()+ File.separator + fileName);
BufferedOutputStream st = new BufferedOutputStream(new FileOutputStream(serverFile));
st.write(bytes);
st.close();
String inputFilePath = serverFile.getAbsolutePath(); \\want to use this variable in uploadfile.jsp

以下是jsp页面的代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>File upload Demo</title>
</head>
<body>
<form method ="post" action="UploadFile" enctype="multipart/form-data">
File to upload: <input id="file" type="file" name="file" />
<input type="submit" value="upload" />
 //want to add a javascript function here, which uses the value held by inputFilePath variable from controller
</form>
</body>
</html>

0 个答案:

没有答案