spring框架的好處,就是能用註解乾的事情,就不用寫代碼。代碼分離,各司其職。html
1.導包,仍是4大核心架包,加上web和webmvc 加日誌記錄 logging.前端
2.配置文件:springmvc.xml 同上,因此就不寫了web
web.xml 第一個配置前端控制器,負責處理全部應用程序請求,已經說過了,ajax
<!-- Map all requests to the DispatcherServlet for handling -->將全部請求映射到DispatcherServlet處理spring
來講說第二個<!-- 配置一個支持rest風格的filter --> ctrl+shift+t 而後輸入hidden 複製全類名mvc
HiddenHttpMethodFilter 翻譯: 隱藏的Http方法過濾 做用:
<!-- 配置一個支持rest風格的filter --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <!-- 攔截全部請求 --> <url-pattern>/*</url-pattern> </filter-mapping>
二、在頁面發請求的時候,指定咱們自定義的請求方式app
index.jsp 核心代碼:框架
<body>
<a href="hello">hello</a><br/>
<a href="rm/handle01?user=123">規定請求參數</a><br/>
<a href="rm/handle02">規定請求頭</a><br/>
<a href="rm/handle03">規定請求方式</a>
<form action="rm/handle03"method="post">
<input type="submit" value="POST提交" />
</form><br/>
<a href="handle01">路徑模糊匹配handle01</a>
<hr/>
<h3>測試Rest風格的URL</h3>
<a href="book/1">查詢圖書</a><br/>
update:
<form action="book/1" method="post">
<input name="_method" value="PUT"/>
<input type="submit" value="修改圖書"/>
</form><br/>
delete:
<form action="book/1" method="post">
<input name="_method" value="DELETE"/>
<input type="submit" value="刪除圖書"/>
</form><br/>
<form action="book" method="post">
<input type="submit" value="新增圖書"/>
</form><br/>
jsp
</body>post
測試以下:
@Controller public class PathVariableTest { @RequestMapping("/deletebook/{bookid}") public String handle01(@PathVariable("bookid") Integer bookid){ System.out.println("刪除的 圖書id:"+bookid); return "success"; } }
註解:
@Controller:告訴Spring這是一個控制器,是用來控制跳轉邏輯的
@RequestMapping("/lookmm"):/表明從當前項目根目錄下找
@RequestMapping兩個位置
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * 路徑的模糊匹配 * @author lfy * * SpringMVC:RequestMapping進行路徑映射容許寫通配符;Ant風格的路徑 * ?:通配一個字符 * *:通配0/多個字符:也能夠匹配一層路徑 * **:通配多層路徑 * 精確優先 * 最精確------?------*-------** * */ @Controller public class TestRequestMapping2Controller { /** * 精確匹配: * 精確優先 * @return */ @RequestMapping("/handle01") public String handle01(){ System.out.println("handle01..."); return "success"; } @RequestMapping("/handle0?") public String handle02(){ System.out.println("handle02..."); return "success"; } @RequestMapping("/handle0*") public String handle03(){ System.out.println("handle03..."); return "success"; } @RequestMapping("/**/handle04") public String handle04(){ System.out.println("handle04..."); return "success"; } }
增刪改查:GET 查詢、DELETE 刪除、PUT 更新、POST 新增
@Controller public class RestTestController { @RequestMapping(value="/book",method=RequestMethod.POST) public String addBook() { System.out.println("圖書添加完成"); return "success"; } @RequestMapping(value="/book/{id}",method=RequestMethod.PUT) public String updateBook(@PathVariable("id")Integer id) { System.out.println("圖書修改完成" + id); return "success"; } @RequestMapping(value="/book/{id}",method=RequestMethod.DELETE) public String deleteBook(@PathVariable("id")Integer id) { System.out.println("圖書刪除完成" + id); return "success"; } @RequestMapping(value="/book/{id}",method=RequestMethod.GET) public String getBook(@PathVariable("id")Integer id) { System.out.println("圖書查詢完成" + id); return "success"; } }
解決響應亂碼問題:加在web.xml配置文件中 ,CharacterEncodingFilter 字符編碼過濾器
<!--字符編碼的filter必定要放在最前面 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!-- 配置encoding,告訴咱們指定的編碼格式 --> <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>