之前的博客裏,我們通過一個入門的案例程序,大致瞭解了SpringMVC的執行過程。這裏,我們先對其進行一個小小的總結(補充)。
處理器映射器
BeanNameUrlHandlerMapping
功能:尋找Controller
根據url請求去匹配bean的name屬性url,從而獲取Controller
SimpleUrlHandlerMaping
功能:尋找Controller
根據瀏覽器url匹配簡單url的key,key又Controller的id找到Controller
<!--簡單url映射 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!-- 對itemsController1進行url映射,url是/queryItems1.action -->
<prop key="/queryItems1.action">itemsController1</prop>
<prop key="/queryItems2.action">itemsController1</prop>
<prop key="/queryItems3.action">itemsController2</prop>
</props>
</property>
</bean>
ControllerClassNameHandlerMapping
功能:尋找Controller
根據類名(MyController)類名.do來訪問,類名首字母小寫
這裏一下子有了三個處理器映射器,那麼問題來了,三個映射器是可以共存的嗎?答案是可以的!
處理器適配器
SimpleControllerHandlerAdapter(默認的)
功能:執行controller
調用controller裏面方法,返回modelAndView。
源碼:
HttpRequestHandlerAdapter
功能:執行controller
同樣的,這兩個適配器(Adapter)也是可以共存的。
RequestMapping寫法小結
requestMapping(“hello”)
requestMapping(“/hello.do”)
requestMapping(value=”/hello.do”)
requestMapping(value=”/hello.do”,method=RequestMethod.GET)
requestMapping(value=”/hello.do”,method=RequestMethod.POST)
requestMapping(value=”/hello.do”,method={RequestMethod.POST, RequestMethod.GET})
注意:瀏覽器直接訪問,a標籤都是get請求;表單提交(指定post),ajax指定post提交,post提交。
或者你也可以自己指定根路徑,like this:
項目名/user/hello.do
封裝參數
首先一點,我們必須明確。SpringMVC是沒有成員變量的,是把需要傳遞的參數對象放入方法中,當你請求這個方法的時候,這個方法裏面的對象會被自動創建,需要封裝的參數,會自動封裝進承載它的對象之中。
我們來分析一下:
接受參數類型:
基本類型:int,String等等基本類型。
Pojo類型
包裝類型
Springmvc默認支持類型:
HttpSession,HttpRequstServlet,Model等等。
Struts2參數:基於屬性封裝。
Springmvc參數封裝:基於方法進行封裝。
1)封裝int類型參數
前臺jsp
<hr size="12" color="blue"/>
<form action="${pageContext.request.contextPath }/user/recieveInt.do" method="post">
ID:<input type="text" name="id" id="id">
<input type="submit" value="提交">
</form>
接收參數方法
//接收int類型
@RequestMapping("/receiveInt")
public String receiveInt(Integer id){
System.out.println(id);
return "success";
}
2)封裝String類型參數
前臺jsp
<hr size="12" color="blue"/>
<form action="${pageContext.request.contextPath }/user/recieveStr.do" method="post">
姓名:<input type="text" name="username" id="username">
<input type="submit" value="提交">
</form>
接收參數方法
//接收String類型
@RequestMapping("/receiveStr")
public String receiveStr(String username) {
System.out.println(username);
return "success";
}
接受數組
分析:批量刪除:checkbox複選框。Value必須有值。
前臺jsp頁面
<hr size="12" color="blue"/>
<form action="${pageContext.request.contextPath }/user/recieveArray.do" method="post">
ID:<input type="checkbox" name="ids" value="1" id="ids">
ID:<input type="checkbox" name="ids" value="2" id="ids">
ID:<input type="checkbox" name="ids" value="3" id="ids">
<input type="submit" value="提交">
</form>
接收參數方法
//接受數組類型參數
@RequestMapping("recieveArray")
public String recieveArray(Integer[] ids){
System.out.println(ids);
return "success";
}
接受Pojo
前臺jsp頁面
<hr size="12" color="blue"/>
<form action="${pageContext.request.contextPath }/user/recieveUserCustom.do" method="post">
姓名:<input type="text" name="user.username" id="username">
生日:<input type="text" name="user.birthday" id="birthday">
性別:<input type="text" name="user.sex" id="sex">
地址:<input type="text" name="user.address" id="address">
<input type="submit" value="提交">
</form>
接收參數方法
//接受包裝類型參數
@RequestMapping("recieveUserCustom")
public String recieveUserCustom(UserCustom userCustom){
System.out.println(userCustom);
return "success";
}
接受集合類型參數
接受list集合
首先在包裝類裏定義集合:
public class UserCustom {
private User user;
private List<User> userList;
private Map<String,Object> maps = new HashMap<String, Object>();
...
前臺jsp頁面
<hr size="12" color="blue"/>
<form action="${pageContext.request.contextPath }/user/recieveList.do" method="post">
姓名:<input type="text" name="userList[0].username" id="username">
地址:<input type="text" name="userList[0].address" id="address">
姓名:<input type="text" name="userList[1].username" id="username">
地址:<input type="text" name="userList[1].address" id="address">
<input type="submit" value="提交">
</form>
接收參數方法
//接受集合類型參數
@RequestMapping("recieveList")
public String recieveList(UserCustom userCustom){
System.out.println(userCustom);
return "success";
}
接受map
前臺jsp頁面
<span style="font-family:Calibri;"><span style="font-family:Calibri;"><span style="font-family:SimSun;font-size:14px;"><hr size="12" color="blue"/>
<form action="${pageContext.request.contextPath }/user/recieveMap.do" method="post">
姓名:<input type="text" name="maps['username']" id="username">
地址:<input type="text" name="maps['address']" id="address">
<input type="submit" value="提交"></span></span></span>
接收參數方法
<span style="font-family:Calibri;"><span style="font-family:Calibri;"><span style="font-family:SimSun;font-size:14px;"> //接受集合類型參數
@RequestMapping("recieveMap")
public String recieveMap(UserCustom userCustom){
System.out.println(userCustom);
return "success";
}
return "success";
}</span></span></span>
SpringMVC與Struts2對比
我們必須思考一個問題,SpringMVC爲什麼能夠取代Struts2?
- 實現機制:
Struts2是基於過濾器實現的。
Springmvc基於servlet實現。Servlet比過濾器快。
- 運行速度:
Struts2是多例
請求來了以後,struts2創建多少個對象:
ActionContext,valuestack,UserAction,ActionSuport,ModelDriven
userAction裏面屬性:User對象,userlist集合等
Springmvc是單例。
- 參數封裝來分析:
Struts基於屬性進行封裝。
Springmvc基於方法封裝。
怎麼理解呢?
如果小明訪問了n次某一個動作類(UserAction),如果是Struts2的話,則會創建n個UserAction、ActionSupport、ModelDriven、user、list、ActionContext、valueStack等。但是如果是SpringMVC的話,只需要創建方法裏的幾個對象。對象是局部變量,使用完畢後就銷燬。