SpringMVC入门笔记(二)

  之前的博客里,我们通过一个入门的案例程序,大致了解了SpringMVC的执行过程。这里,我们先对其进行一个小小的总结(补充)。


处理器映射器

BeanNameUrlHandlerMapping

功能:寻找Controller

    根据url请求去匹配beanname属性url,从而获取Controller



SimpleUrlHandlerMaping

功能寻找Controller

   根据浏览器url匹配简单urlkeykeyControllerid找到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



封装参数


我们都知道,不论是Struts2还是SpringMVC,都是web(controller)层的框架,存在的意义主要是与前端进行数据交互、视图跳转。那么问题来了,SpringMVC如何封装参数呢?

首先一点,我们必须明确。SpringMVC是没有成员变量的,是把需要传递的参数对象放入方法中,当你请求这个方法的时候,这个方法里面的对象会被自动创建,需要封装的参数,会自动封装进承载它的对象之中。

我们来分析一下:

接受参数类型:

基本类型:intString等等基本类型。

Pojo类型

包装类型

 

Springmvc默认支持类型:

HttpSessionHttpRequstServletModel等等

 

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的话,只需要创建方法里的几个对象。对象是局部变量,使用完毕后就销毁。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章