響應數據和結果視圖 返回值分類 1.返回值是String 返回值類型是字元串的,會根據返回的字元串去尋找相對應的jsp頁面 2.返回值是Void 預設請求路徑是什麼就會去尋找請求路徑的jsp 編寫請求轉發和重定向的程式和直接響應 返回值是ModelAndView對象 (存JavaBean對象和跳轉頁 ...
響應數據和結果視圖
返回值分類
1.返回值是String
返回值類型是字元串的,會根據返回的字元串去尋找相對應的jsp頁面
@Controller
@RequestMapping("/user")
public class UserController {
//返回值類型是String
@RequestMapping("/testString")
public String testString(Model model){
System.out.println("testString方法執行了");
//模擬從資料庫中查詢出User對象
User user = new User();
user.setAge(20);
user.setPassword("123");
user.setUsername("任我行");
//使用model把對象存起來
model.addAttribute("user",user);
return "success";
}
2.返回值是Void
預設請求路徑是什麼就會去尋找請求路徑的jsp
編寫請求轉發和重定向的程式和直接響應
@Controller
@RequestMapping("/user")
public class UserController {
//返回值類型是String
@RequestMapping("/testString")
public String testString(Model model){
System.out.println("testString方法執行了");
//模擬從資料庫中查詢出User對象
User user = new User();
user.setAge(20);
user.setPassword("123");
user.setUsername("任我行");
//使用model把對象存起來
model.addAttribute("user",user);
return "success";
}
//返回值類型是Void
//請求轉發是一次請求:不用編寫項目的名稱
@RequestMapping("/testVoid")
public void testVoid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("testVoid方法執行了");
//編寫請求轉發的程式
// request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);//轉發
//response.sendRedirect(request.getContextPath()+"/index.jsp");//重定向
//設置中文亂碼
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.getWriter().print("hello,大笨蛋");
return;
}
}
返回值是ModelAndView對象(存JavaBean對象和跳轉頁面)
//返回值類型是ModelAndView
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
ModelAndView mv = new ModelAndView();
System.out.println("testModelAndView執行了");
//模擬從資料庫中查詢出User對象
User user = new User();
user.setAge(20);
user.setPassword("123");
user.setUsername("令狐沖");
//調用mv的方法
//user對象存儲到mv對象中,同時也會把user對象存入到requst對象
mv.addObject("user",user);
//想跳轉的頁面
mv.setViewName("success");
return mv;
}
轉發或重定向
//返回值類型是ModelAndView
@RequestMapping("/testForwardOrRedirect")
public String testForwardOrRedirect(){
System.out.println("testForwardOrRedirect執行了");
// return "forward:/WEB-INF/pages/success.jsp";
//不用加項目名稱,框架已經加好
return "redirect:/index.jsp";
}
響應json數據值過濾靜態資源
<%--
Created by IntelliJ IDEA.
User: Yuan
Date: 2019/7/22
Time: 14:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script src="js/jquery.min.js"></script>
<script>
//頁面載入,綁定單擊事件
$(function(){
$("#btn").click(function(){
alert("hello btn");
})
});
</script>
</head>
<body>
<br>
<button id="btn">發送ajax請求</button>
</body>
</html>
上面的單擊事件無法響應,原因是DispatcherServlet把靜態資源給攔截了
解決方案
告訴前端控制器,哪些靜態資源不攔截
響應jso數據值發送ajax的請求
<%--
Created by IntelliJ IDEA.
User: Yuan
Date: 2019/7/22
Time: 14:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script src="js/jquery.min.js"></script>
<script>
//頁面載入,綁定單擊事件
$(function(){
$("#btn").click(function(){
$.ajax({
//編寫json格式,設置屬性和值
//url:請求伺服器的路徑
url:"user/testAjax",
//contentType:發送內容給伺服器是的編碼類型
contentType:"application/json;charset=UTF-8",
//data:發送到伺服器的數據
data:'{"username":"hehe","password":"123","age":"20"}',
//dataType預期伺服器返回的類型
dataType:"json",
//tpye,請求方式
type:"post",
//success:請求成功後的回調函數
success:function(data){
//data伺服器端響應的json的數據,進行解析
}
});
});
});
</script>
</head>
<body>
<br>
<button id="btn">發送ajax請求</button>
</body>
</html>
@RequestMapping("/testAjax")
public void testAjax(@RequestBody String body){
System.out.println("ajax執行了....");
System.out.println(body);
}
文件上傳之上傳原理分析
文件上傳的必要前提
1.form表單的enctype取值必須是:multipart/form-data
2.method屬性取值必須是Post
3.提供一個文件選擇域