項目測試目錄
1、響應傳回值之String類型
controller的方法傳回的字元串可以指定邏輯視圖的名稱,根據視圖解析器為實體視圖位址。
@Controller
@RequestMapping(path = "con")
public class controller {
@RequestMapping(path = "test")
public String test(){
System.out.println("test.....");
return "success";
}
}
2、響應傳回值之void類型
預設會去找testVoid.jsp這個檔案。
@RequestMapping(path = "testVoid")
public void testVoid(){
System.out.println("test.....");
}
可以利用請求轉發,然後去跳轉到success.jsp頁面,重定向因為是兩次請求,是以無法通路WEB-INF中的内容
@RequestMapping(path = "testVoid")
public void testVoid(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException {
System.out.println("test.....");
//請求轉發
// 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;
}
}
3、響應傳回值之ModelAndView類型
controller方法傳回字元串可以指定邏輯視圖的名稱,根據視圖解析器為實體視圖的位址。
@RequestMapping(path = "testModel")
public ModelAndView testModel(){
System.out.println("test.....");
ModelAndView mv = new ModelAndView();
User user = new User();
//把user對象存儲到mv對象中去,也會把user對象存儲到request對象中
mv.addObject("user",user);
//跳轉到哪個頁面
mv.setViewName("success");
return mv;
}
4、響應之使用forward和redirect進行頁面跳轉
這兩個方法用不了視圖解析器,需要自己去寫路徑。
- forward轉發:controller方法在提供了String類型的傳回值之後,預設就是請求轉發。關鍵字forward。
@RequestMapping(path = "testString")
public String testString(){
System.out.println("test.....");
//return "forward:/WEB-INF/pages/success.jsp";
return "redirect:index.jsp";
}
5、@ResponseBody響應json資料
我們知道當使用者發送一個請求的時候,就會攔截所有的資源,導緻的一個問題就是靜态資源(img、css、js)也會被攔截到,進而不能被使用。解決問題就是需要配置靜态資源不進行攔截,在springMVC.xml配置檔案添加如下配置。
-
mvc:resources标簽配置不過濾
location元素表示webapp目錄下的包下的所有的檔案。
mapping元素表示以/static開頭的所有請求路徑,如:/static/a或者/static/b
<mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
<mvc:resources mapping="/images/**" location="/images/"></mvc:resources>
<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
-
擷取json資料:利用RequestBody這個注解。
ajax
$(".btn")[0].onclick = function(){
$.ajax({
type : "post",
url : "con/testAjax",
cache : false,
data : {
"username":"hhhh",
"password":"bbbb"
},
dataType : "text",
success : function(msg) {
var user = JSON.parse(msg);
alert(user.username);
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert("擷取資料失敗!");
}
})
}
有三種擷取的方式:
1、簡單的,這樣可以擷取資料。
@RequestMapping(path = "testAjax")
public String testAjax(User user){
System.out.println(user);
return "success";
}
執行結果:
2、直接為這個字元串
@RequestMapping(path = "testAjax")
public String testAjax(@RequestBody String body){
System.out.println(body);
return "success";
}
3、導入json的相關jar包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
利用@ResponseBody注解,該注解可以将對象轉換成字元串傳回給前端的success回調函數。
@RequestMapping(path = "testAjax")
public @ResponseBody User testAjax(User user){
System.out.println(user);
System.out.println(user.getUsername());
user.setUsername("aaaa");
user.setPassword("aaaa");
return user;
}