SpringMVC實現頁面和java模型的資料互動以及檔案上傳下載和資料校驗
阿新 • • 發佈:2019-01-23
1. 專案結構
2. springMVC-servlet.xml 配置檔案
3. web.xml 主配置檔案<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 啟用註解 --> <mvc:annotation-driven/> <context:component-scan base-package="com.zhq.controller"/> <!--配置靜態資源訪問 --> <mvc:resources location="/images/" mapping="/images/**"/> <mvc:resources location="/css/" mapping="/css/**"/> <mvc:resources location="/js/" mapping="/js/**"/> <!--檢視分解器,把controller控制器返回的檢視名稱加上字首及字尾,檢視的路徑最終得到/檢視名.jsp --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--校驗器,配置validator --> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/> <property name="validationMessageSource" ref="reloadableResourceBundleMessageSource"/> </bean> <!-- 配置驗證訊息 --> <bean id="reloadableResourceBundleMessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"></bean> <!--檔案上傳的引數配置 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--10485760位元組,表示所有檔案大小總和不超過10M --> <property name="maxUploadSize" value="10485760"/> <!--設定上傳檔案時快取區的大小 --> <property name="maxInMemorySize" value="4096"/> <!--設定字元編碼 --> <property name="defaultEncoding" value="UTF-8"/> </bean> <!--檔案上傳超過大小時的異常處理,頁面跳轉 --> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <!--配置捕獲異常型別 --> <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">/errorFile</prop> </props> </property> </bean> </beans>
4. com.zhq.entity<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>SpringMVC_01</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!--配置springMVC的啟動 --> <servlet> <!--可以約束springMVC的主配置檔案如果在預設路徑下時的檔名 --> <servlet-name>springMVC</servlet-name> <!--配置前端控制器 --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--當主配置檔案不在預設路徑下,則需要配置檔案的路徑 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/springMVC-servlet.xml</param-value> </init-param> <!--配置springMVC 隨程式啟動而啟動 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <!--/:表示攔截所有請求 --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- springMVC中文亂碼過濾器 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!--初始化 encoding forceEncoding引數--> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <error-page> <error-code>404</error-code> <location>/error.jsp</location> </error-page> </web-app>
package com.zhq.entity; import java.util.List; public class Grade { private int gradeId; private String gradeName; private List<Student> students=null; public Grade() { super(); } public Grade(int gradeId, String gradeName) { super(); this.gradeId = gradeId; this.gradeName = gradeName; } public int getGradeId() { return gradeId; } public void setGradeId(int gradeId) { this.gradeId = gradeId; } public String getGradeName() { return gradeName; } public void setGradeName(String gradeName) { this.gradeName = gradeName; } public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } @Override public String toString() { return "Grade [gradeId=" + gradeId + ", gradeName=" + gradeName + "]"; } }
package com.zhq.entity;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range;
import org.springframework.format.annotation.DateTimeFormat;
import jdk.nashorn.internal.objects.annotations.SpecializedFunction;
public class Student {
private int studentNo;
@Size(min=2,max=10,message="學生姓名為2-10")
private String name;
private String sex;
@Range(min=18,max=45,message="年齡18-45")
private int age;
@NotEmpty(message="手機不能為空")
private String phone;
@NotEmpty(message="地址不能為空")
private String address;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birthday;
@Email(message="郵箱格式不正確")
private String email;
private Grade grade;
public Student() {
super();
}
public Student(int studentNo, String name, String sex, int age, String phone, String address
) {
super();
this.studentNo = studentNo;
this.name = name;
this.sex = sex;
this.age = age;
this.phone = phone;
this.address = address;
}
public int getStudentNo() {
return studentNo;
}
public void setStudentNo(int studentNo) {
this.studentNo = studentNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
@Override
public String toString() {
return "Student [studentNo=" + studentNo + ", name=" + name + ", sex=" + sex + ", age=" + age + ", phone="
+ phone + ", address=" + address + ", birthday=" + sdf.format(birthday) + ", email=" + email
+ "]";
}
}
package com.zhq.entity;
public class Subject {
private int subjectNo;
private String subjectName;
private int classHour;
private Grade grade;
public int getSubjectNo() {
return subjectNo;
}
public void setSubjectNo(int subjectNo) {
this.subjectNo = subjectNo;
}
public String getSubjectName() {
return subjectName;
}
public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}
public int getClassHour() {
return classHour;
}
public void setClassHour(int classHour) {
this.classHour = classHour;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
@Override
public String toString() {
return "Subject [subjectNo=" + subjectNo + ", subjectName=" + subjectName + ", classHour=" + classHour
+ "]";
}
}
5. com.zhq.controller
package com.zhq.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
@Controller
@RequestMapping("/file")
public class FileController {
//檔案上傳
@RequestMapping("springUpload")
public String springUpload(HttpServletRequest request) {
//將當前上下文初始化給CommonsMultipartResolver(多部分解析器)
CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(request.getSession().getServletContext());
//檢查form中是否有multipart/form-data
if(multipartResolver.isMultipart(request)) {
//將request變成多部分request
MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest) request;
//獲取multiRequest中所有檔名
Iterator iter=multiRequest.getFileNames();
//遍歷所有檔案
while(iter.hasNext()) {
MultipartFile file=multiRequest.getFile(iter.next().toString());
if(file!=null&&file.getOriginalFilename()!="") {
String path=request.getSession().
getServletContext().getRealPath("/userfile")+"/"+
file.getOriginalFilename();
//上傳
try {
file.transferTo(new File(path));
System.out.println(path);
request.setAttribute("msg", "上傳成功!");
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}else {
request.setAttribute("msg", "上傳失敗!");
}
return "uploadMsg";
}
//獲取下載檔名列表
@RequestMapping("springFilelist")
public String springFilelist(HttpServletRequest request) {
//實現思路:先獲取userfile目錄下所有檔案的檔名,儲存,然後跳轉到downloadFile.jsp展示
//建立集合Map<包含唯一標記的檔名,簡短檔名>
Map<String,String> fileNames=new HashMap<String,String>();
//獲取上傳目錄,以及其下所有的檔名
String basePath=request.getSession().getServletContext().getRealPath("/userfile")+"/";
//目錄
File file=new File(basePath);
//目錄下,所有檔名
String list[]=file.list();
//遍歷,封裝
if(list!=null&&list.length>0) {
for(int i=0;i<list.length;i++) {
//全名
String fileName=list[i];
//短名
String shortName=fileName.substring(fileName.lastIndexOf("\\")+1);
//封裝
fileNames.put(fileName, shortName);
}
}
//儲存到request域
try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
request.setAttribute("fileNames", fileNames);
return "downloadFile";
}
//根據檔名下載檔案
@RequestMapping("springDownload")
public void springDownload(HttpServletRequest request,HttpServletResponse response) {
//獲取下載檔名稱
String fileName=request.getParameter("fileName");
//GET方式獲得的漢字需要轉碼,框架已經設定所以此處不用
/*fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8");*/
//先獲取上傳目錄路徑
String basePath=request.getSession().getServletContext().getRealPath("/userfile")+"/";
try {
//獲取一個檔案流
InputStream in = new FileInputStream(new File(basePath,fileName));
// 如果檔名是中文,需要進行url編碼
fileName = URLEncoder.encode(fileName, "UTF-8");
// 設定下載的響應頭
response.setHeader("content-disposition", "attachment;fileName=" + fileName);
//獲取response位元組流
OutputStream out = response.getOutputStream();
byte[] b = new byte[1024];
int len = -1;
while ((len = in.read(b)) != -1){
out.write(b, 0, len);
}
// 關閉
out.close();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.zhq.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.zhq.entity.Grade;
@Controller
@RequestMapping("/grade")
public class GradeController {
//方式一傳遞資料到前端
@RequestMapping("/list/map")//預設返回grade/list/map.jsp
public Map<String,Grade> gradeList(){
Map<String,Grade> map=new HashMap<String, Grade>();
Grade grade=new Grade();
grade.setGradeName("一年級");
map.put("a", grade);
return map;
}
//方式二傳遞資料到前端
@RequestMapping("/list")
public String gradeList(Model model){
List<Grade> gradelist=new ArrayList<Grade>();
Grade grade=new Grade();
grade.setGradeName("二年級");
gradelist.add(grade);
model.addAttribute("gradelist", gradelist);
return "grade/gradelist";//返回檢視grade/gradelist.jsp
}
}
package com.zhq.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*import javax.servlet.http.HttpServletRequest;*/
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.zhq.entity.Grade;
import com.zhq.entity.Student;
@Controller
@RequestMapping(value= {"/student","/stu"})
public class StudentController {
@RequestMapping("/list")
public ModelAndView list() {
List<Student> list=new ArrayList<Student>();
list.add(new Student(1000, "張三", "男", 20, "13125283736", "廣東廣州"));
list.add(new Student(1001, "李四", "男", 21, "13838384388", "廣東珠海"));
list.add(new Student(1002, "王婷", "女", 22, "15238384499", "廣東中山"));
return new ModelAndView("studentList","data",list);
}
//method=RequestMethod.GET表示只接收GET請求,一般要初始化資料時呼叫
@RequestMapping(value={"/add","/add1"},method=RequestMethod.GET)
public ModelAndView add() {
List<Grade> list=new ArrayList<Grade>();//初始化年級下拉框資料
list.add(new Grade(1, "一年級"));
list.add(new Grade(2, "二年級"));
list.add(new Grade(3, "三年級"));
return new ModelAndView("studentAdd","glist",list);
}
//method=RequestMethod.POST表示只接收POST請求
//@Validated作用就是將pojo內的註解資料校驗規則生效,BindingResult用於獲取校驗失敗資訊
@RequestMapping(value={"/add","/add2"},method=RequestMethod.POST)
public ModelAndView add(@Validated Student student,BindingResult result,Model model) {
//傳參取值有三種方式(前兩種標籤名和引數名要相同):
//1.ModelAndView add(Student student)自動封裝成物件
//2.ModelAndView add(String name, int gradeId)自動匹配引數
//3.ModelAndView add(HttpServletRequest request)獲取請求
if(result.hasErrors()) {
model.addAttribute("msg", "驗證資訊錯誤!");
Map<String,String> errors=new HashMap<String,String>();
for(FieldError err:result.getFieldErrors()) {//遍歷錯誤提示將錯誤資訊存入Map
errors.put(err.getField(), err.getDefaultMessage());
}
model.addAttribute("errors", errors);
List<Grade> list=new ArrayList<Grade>();//年級下拉框資訊
list.add(new Grade(1, "一年級"));
list.add(new Grade(2, "二年級"));
list.add(new Grade(3, "三年級"));
model.addAttribute("glist", list);
return new ModelAndView("studentAdd");
/*return new ModelAndView("forward:/student/add1");*/
}else {
//轉發到/student/list方法或者使用redirect:/student/list
return new ModelAndView("redirect:/student/list");
}
}
//佔位符方式傳遞引數/del/100
@RequestMapping("/del/{id:\\d+}")//\\d+表示只匹配數字
public ModelAndView delete(@PathVariable(value="id") Integer id) {
System.out.println(id);
return new ModelAndView("redirect:/student/list","id",id);
}
//預設傳遞引數方式/find?id=100
@RequestMapping("/find")
public ModelAndView findStudentByid(Integer id) {
System.out.println(id);
return new ModelAndView("studentAdd","id",id);
}
}
6. 頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="css/index.css" />
</head>
<body>
<img src="images/ws.gif"><br>
<a href="${pageContext.request.contextPath}/student/list">學生資訊列表</a><br>
<a href="${pageContext.request.contextPath}/student/add">新增學生</a><br>
<a href="${pageContext.request.contextPath}/grade/list/map">年級列表1</a><br>
<a href="${pageContext.request.contextPath}/grade/list">年級列表2</a><br>
<a href="${pageContext.request.contextPath}/uploadFile.jsp">多檔案上傳</a><br>
<a href="${pageContext.request.contextPath}/file/springFilelist">檔案下載</a><br>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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=UTF-8">
<title>Insert title here</title>
<style type="text/css">
table {border-collapse:collapse;}
table,th,td {border: 1px solid black;}
</style>
</head>
<body>
歡迎!
<table>
<tr>
<th>姓名</th>
<th>性別</th>
<th>年齡</th>
<th>電話</th>
<th>地址</th>
<th>操作</th>
</tr>
<c:forEach items="${data}" var="student" >
<tr>
<td>${student.name}</td>
<td>${student.sex}</td>
<td>${student.age}</td>
<td>${student.phone}</td>
<td>${student.address}</td>
<td><a href="${pageContext.request.contextPath}/student/del/${student.studentNo}" onclick="return confirm('你確定要刪除嗎?')">刪除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
新增頁面!
<form action="${pageContext.request.contextPath}/student/add" method="post">
<p>姓名:<input name="name">${errors.name}<br></p>
<p>年齡:<input name="age">${errors.age}<br></p>
<p>性別:
<select name="sex">
<option value="男">男</option>
<option value="女">女</option>
</select>
</p>
<p>地址:<input name="address">${errors.address}<br></p>
<p>班級:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
新增頁面!
<form action="${pageContext.request.contextPath}/student/add" method="post">
<p>姓名:<input name="name">${errors.name}<br></p>
<p>年齡:<input name="age">${errors.age}<br></p>
<p>性別:
<select name="sex">
<option value="男">男</option>
<option value="女">女</option>
</select>
</p>
<p>地址:<input name="address">${errors.address}<br></p>
<p>班級:
<select name="grade.gradeId">
<c:forEach items="${glist}" var="grade">
<option value="${grade.gradeId}">${grade.gradeName}</option>
</c:forEach>
</select><br>
</p>
<p><input type="submit" value="儲存">${msg}<br></p>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:forEach items="${map}" var="entry">
${entry.key}:${entry.value.gradeName}<br>
</c:forEach>
${a.gradeName}<br>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:forEach items="${gradelist}" var="grade">
${grade.gradeName}<br>
</c:forEach>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>多檔案上傳</h2>
<form action="${pageContext.request.contextPath}/file/springUpload" name="uploadFile" enctype="multipart/form-data" method="post">
<p><input type="file" name="file1"></p>
<p><input type="file" name="file2"></p>
<p><input type="file" name="file3"></p>
<input type="submit" value="上傳">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
${msg}
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
檔案超出大小10M!上傳失敗!
<a href="${pageContext.request.contextPath}/uploadFile.jsp">返回</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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=UTF-8">
<title>下載頁面</title>
<style type="text/css">
table {border-collapse:collapse;}
table,th,td {border: 1px solid black;}
</style>
</head>
<body>
<h3 align="center">下載列表</h3>
<table align="center">
<tr>
<th>序號</th>
<th>檔名</th>
<th>操作</th>
</tr>
<c:forEach var="en" items="${requestScope.fileNames}" varStatus="vs">
<tr>
<td>${vs.count }</td>
<td>${en.value }</td>
<td>
<%--<a href="${pageContext.request.contextPath }/springDownload?method=down&..">下載</a>--%>
<!-- 構建一個地址 -->
<c:url var="url" value="springDownload">
<c:param name="method" value="down"></c:param>
<c:param name="fileName" value="${en.key}"></c:param>
</c:url>
<!-- 使用上面地址 -->
<a href="${url}">下載</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
錯誤頁面!404
</body>
</html>
7. 最終效果