1.Jsp頁面配置:html
<a href="dlfile/?fileName=a.doc">下載</a>
2.後臺:java
@RequestMapping("dlfile")
public ResponseEntity<byte[]> dlfile(String fileName) throws IOException {
//文件路徑
String path="E:\\第三階段\\day08";
//定義要下載的文件
File file=new File(path,"day08.doc");
HttpHeaders headers=new HttpHeaders();
//headers指定文件下載的方式以及在瀏覽器顯示的名稱
headers.setContentDispositionFormData("attachment",fileName);
//將文件以流的方式響應到頁面。
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//參數一:文件對應的二進制字節數組
(FileUtils.readFileToByteArray(file),文件轉二進制數組)
//參數二:設置響應頭
(headers)
//參數三:設置響應狀態
(HttpStatus.OK,狀態正常(200)))
return new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
}
複製代碼
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
Object o, Exception e) {
ModelAndView mv = new ModelAndView();
if(e instanceof NullPointerException){
mv.setViewName("error");
mv.addObject("msg","空指針異常");
return mv;
}
mv.setViewName("error");
mv.addObject("msg","其餘異常");
return mv;
}
複製代碼
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
複製代碼
<bean class="com.springmvc.utils.MyHandlerExpResolver"/>
public class LoginIter implements HandlerInterceptor {
/*
在執行handler以前來執行的,
用途:用於用戶認證校驗、用戶權限校驗
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
HttpSession session = request.getSession();
User user= (User) session.getAttribute("user");
System.out.println("第一個攔截器,在處理器執行前的方法");
//判斷用戶是否登錄
if(user!=null){
System.out.println("true");
//登錄成功
return true;
}
System.out.println("false");
request.getRequestDispatcher("/login.jsp").forward(request,response);
return false;
}
/*
在執行handler返回modelAndView以前來執行
用途:若是須要向頁面提供一些公用 的數據或配置一些視圖信息,
使用此方法實現 從modelAndView入手
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {
System.out.println("第一個攔截器,在執行handler返回modelAndView以前來執行");
}
/*
執行handler以後執行此方法
用途:1.做爲系通通一異常處理,進行方法執行性能監控,
在preHandle中設置一個時間點,在afterCompletion設置一個時間,
兩個時間點的差就是執行時長。
2.實現系統 統一日誌記錄
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) throws Exception {
System.out.println("第一個攔截器,執行handler以後執行此方法");
}
複製代碼
${loginError}//顯示登錄失敗的信息
<form action="${pageContext.request.contextPath}/login" method="get">
輸入帳戶:<input type="text" name="uname"><br>
<input type="submit" value="登錄">
</form>
複製代碼
@RequestMapping("login")
public String login(Model m,User user, HttpSession session){
if(user.getUname().equals("admin")){
session.setAttribute("user",user);
return "index";
}
m.addAttribute("loginError","從新登錄");
return "login";
}
複製代碼
<!--配置攔截器 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- "/**"能夠攔截路徑無論多少層 -->
<mvc:mapping path="/**"/>
<!--選擇不攔截登錄方法-->
<mvc:exclude-mapping path="/login"/>
<!--實例化LoginIter類-->
<bean class="com.springmvc.utils.LoginIter"/>
</mvc:interceptor>
</mvc:interceptors>
複製代碼
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<a href="/addUser">添加用戶</a>
<form:form action="saveUsers" method="post" modelAttribute="user">
<form:label path="uname">名字:</form:label> <br>
<form:input path="uname"/><br>
<form:label path="uid">ID:</form:label><br>
<form:input path="uid"/><br>
<form:label path="birthday">生日:</form:label><br>
<form:input path="birthday"/><br>
愛好: <br>
<form:checkbox path="hobbies" value="1"/>聊天1<br>
<form:checkbox path="hobbies" value="2"/>聊天2<br>
<form:checkbox path="hobbies" value="3"/>聊天3<br>
<hr>
<form:checkboxes path="jobs" items="${jlist}" itemValue="jid" itemLabel="jname" />
<form:button>保存</form:button>
</form:form>
複製代碼
@RequestMapping("addUser")
public String addUser(Model m){
User user = new User();
user.setUname("李四");
List<Job> list=new ArrayList<>();
list.add(new Job("1","高級工程師"));
list.add(new Job("2","項目經理"));
list.add(new Job("3","人事總監"));
m.addAttribute("user",user);
m.addAttribute("jlist",list);
return "form";
}
複製代碼
@RequestMapping("saveUsers")
@ResponseBody
public User saveUser(User user){
System.out.println(user);
return user;
}複製代碼
public class User {
@NotEmpty(message = "用戶名不能爲空")
private String uname;
private Integer uid;
@Length(min = 3,max = 6,message = "密碼介於3-6位之間")
private String password;
//@DateTimeFormat(pattern = "yyyy-MM-dd")//日期格式化註解
private Date birthday;
private String hobbies;
@NotEmpty(message = "職位不能爲空")
private List<Integer> jobs;
}
複製代碼
public String saveUser(Model m, @ModelAttribute("user") @Valid User user, BindingResult br)
@RequestMapping("saveUsers")
public String saveUser(Model m, @ModelAttribute("user") @Valid User user, BindingResult br){
//@ModelAttribute至關於:m.addAttribute("user"),
//@Valid:用於驗證註解是否符合要求,直接加在變量user以前,在變量中添加驗證信息的要求,當不符合要求時就會在方法中返回message 的錯誤提示信息。
if(br.hasErrors()){//若是有錯誤信息
List<Job> list=new ArrayList<>();
list.add(new Job("1","高級工程師"));
list.add(new Job("2","項目經理"));
list.add(new Job("3","人事總監"));
m.addAttribute("jlist",list);
return "form";
}
System.out.println(user);
return "main";
}
複製代碼
<form:form action="saveUsers" method="post" modelAttribute="user">
<form:label path="uname">名字:</form:label> <br>
<form:input path="uname"/><br>
<form:errors path="uname"/><!--驗證名字-->
<form:label path="password">密碼:</form:label><br>
<form:password path="password"/><br>
<form:errors path="password"/><!--驗證密碼-->
<form:label path="birthday">生日:</form:label><br>
<form:input path="birthday"/><br>
愛好: <br>
<form:checkbox path="hobbies" value="1"/>聊天1<br>
<form:checkbox path="hobbies" value="2"/>聊天2<br>
<form:checkbox path="hobbies" value="3"/>聊天3<br>
<hr>
<form:checkboxes path="jobs" items="${jlist}" itemValue="jid" itemLabel="jname" />
<form:errors path="jobs"/><!--驗證職位-->
<form:button>保存</form:button>
</form:form>
複製代碼
第一次寫文章,有錯誤還請你們幫忙指出,萬分感謝
spring