資料處理及跳轉
5、資料處理及跳轉
現在我們來看看SpringMVC引數接收處理和結果跳轉處理吧!
5.1 跳轉方式
ModelAndView
設定ModelAndView物件 , 根據view的名稱 , 和檢視解析器跳到指定的頁面 .
頁面 : {檢視解析器字首} + viewName +{檢視解析器字尾}
<!-- 檢視解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 字首 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 字尾 --> <property name="suffix" value=".jsp" /> </bean>
對應的controller類
public class ControllerTest1 implements Controller { public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { //返回一個模型檢視物件 ModelAndView mv = new ModelAndView(); mv.addObject("msg","ControllerTest1"); mv.setViewName("test"); return mv; } }
ServletAPI
通過設定ServletAPI , 不需要檢視解析器 .
1、通過HttpServletResponse進行輸出
2、通過HttpServletResponse實現重定向
3、通過HttpServletResponse實現轉發
@Controller public class ResultGo { @RequestMapping("/result/t1") public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException { rsp.getWriter().println("Hello,Spring BY servlet API"); } @RequestMapping("/result/t2") public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException { rsp.sendRedirect("/index.jsp"); } @RequestMapping("/result/t3") public void test3(HttpServletRequest req, HttpServletResponse rsp) throws Exception { //轉發 req.setAttribute("msg","/result/t3"); req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,rsp); } }
SpringMVC
通過SpringMVC來實現轉發和重定向 - 無需檢視解析器;
測試前,需要將檢視解析器註釋掉
@Controller
public class ResultSpringMVC {
@RequestMapping("/rsm/t1")
public String test1(){
//轉發
return "/index.jsp";
}
@RequestMapping("/rsm/t2")
public String test2(){
//轉發二
return "forward:/index.jsp";
}
@RequestMapping("/rsm/t3")
public String test3(){
//重定向
return "redirect:/index.jsp";
}
}
通過SpringMVC來實現轉發和重定向 - 有檢視解析器;
重定向 , 不需要檢視解析器 , 本質就是重新請求一個新地方嘛 , 所以注意路徑問題.
可以重定向到另外一個請求實現 .
@Controller
public class ResultSpringMVC2 {
@RequestMapping("/rsm2/t1")
public String test1(){
//預設轉發
return "test";
}
@RequestMapping("/rsm2/t2")
public String test2(){
//重定向
return "redirect:/index.jsp";
//return "redirect:hello.do"; //hello.do為另一個請求/
}
}
5.2 處理提交資料
1、提交的域名稱和處理方法的引數名一致
提交資料 : http://localhost:8080/hello?name=kuangshen
處理方法 :
@RequestMapping("/hello")
public String hello(String name){
System.out.println(name);
return "hello";
}
後臺輸出 : kuangshen
2、提交的域名稱和處理方法的引數名不一致
提交資料 : http://localhost:8080/hello?username=kuangshen
處理方法 :
//@RequestParam("username") : username提交的域的名稱 .
@RequestMapping("/hello")
public String hello(@RequestParam("username") String name){
System.out.println(name);
return "hello";
}
後臺輸出 : kuangshen
3、提交的是一個物件
要求提交的表單域和物件的屬性名一致 , 引數使用物件即可
1、實體類
public class User {
private int id;
private String name;
private int age;
//構造
//get/set
//tostring()
}
2、提交資料 : http://localhost:8080/mvc04/user?name=kuangshen&id=1&age=15
3、處理方法 :
@RequestMapping("/user")
public String user(User user){
System.out.println(user);
return "hello";
}
後臺輸出 : User { id=1, name='kuangshen', age=15 }
說明:如果使用物件的話,前端傳遞的引數名和物件名必須一致,否則就是null。
5.3 資料顯示到前端
第一種 : 通過ModelAndView
我們前面一直都是如此 . 就不過多解釋
public class ControllerTest1 implements Controller {
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//返回一個模型檢視物件
ModelAndView mv = new ModelAndView();
mv.addObject("msg","ControllerTest1");
mv.setViewName("test");
return mv;
}
}
第二種 : 通過ModelMap
ModelMap
@RequestMapping("/hello")
public String hello(@RequestParam("username") String name, ModelMap model){
//封裝要顯示到檢視中的資料
//相當於req.setAttribute("name",name);
model.addAttribute("name",name);
System.out.println(name);
return "hello";
}
第三種 : 通過Model
Model
@RequestMapping("/ct2/hello")
public String hello(@RequestParam("username") String name, Model model){
//封裝要顯示到檢視中的資料
//相當於req.setAttribute("name",name);
model.addAttribute("msg",name);
System.out.println(name);
return "test";
}
對比
就對於新手而言簡單來說使用區別就是:
Model 只有寥寥幾個方法只適合用於儲存資料,簡化了新手對於Model物件的操作和理解;
ModelMap 繼承了 LinkedMap ,除了實現了自身的一些方法,同樣的繼承 LinkedMap 的方法和特性;
ModelAndView 可以在儲存資料的同時,可以進行設定返回的邏輯檢視,進行控制展示層的跳轉。
當然更多的以後開發考慮的更多的是效能和優化,就不能單單僅限於此的瞭解。
請使用80%的時間打好紮實的基礎,剩下18%的時間研究框架,2%的時間去學點英文,框架的官方文件永遠是最好的教程。
5.3 亂碼問題
測試步驟:
1、我們可以在首頁編寫一個提交的表單
<form action="/e/t" method="post">
<input type="text" name="name">
<input type="submit">
</form>
2、後臺編寫對應的處理類
@Controller
public class Encoding {
@RequestMapping("/e/t")
public String test(Model model,String name){
model.addAttribute("msg",name); //獲取表單提交的值
return "test"; //跳轉到test頁面顯示輸入的值
}
}
3、輸入中文測試,發現亂碼
不得不說,亂碼問題是在我們開發中十分常見的問題,也是讓我們程式猿比較頭大的問題!
以前亂碼問題通過過濾器解決 , 而SpringMVC給我們提供了一個過濾器 , 可以在web.xml中配置 .
修改了xml檔案需要重啟伺服器!
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>