1.非注解的处理器映射器(HandlerMapping)
1.访问:
http://localhost:8080/queryItems.action
<bean name="/queryItems.action" class="spring.ssm.controller.ItemsController"/>
<!-- 处理器映射器
将bean的name作为url进行查找,需要在配置Handler时指定beanname(就是url)
所有的映射器都实现了HandlerMapping接口
-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
2.简单URL映射
访问:http://localhost:8080/huan.action
http://localhost:8080/ma.action
<bean name="/queryItems.action" class="spring.ssm.controller.ItemsController"/>
<!--简单URL映射-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props >
<prop key="/huan.action">itemsController</prop>
<prop key="/ma.action">itemsController</prop>
</props>
</property>
</bean>
具体配置内容见:SpringMVC框架入门--(学习笔记)
2.非注解的适配器(HandlerAdapter)
1.适配器
SimpleControllerHandlerAdapter
- 要求所有的
Handler
实现Controller
接口
<!--要求所有的Handler实现Controller接口-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
package spring.ssm.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import spring.ssm.pojo.Items;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ubuntu on 17-7-6.
*/
//实现Controller接口的处理器
public class ItemsController implements Controller{
public ModelAndView handleRequest(HttpServletRequest Request, HttpServletResponse Response) throws Exception {
List<Items> list = new ArrayList<Items>();
Items items_1 = new Items();
items_1.setName("小米笔记本");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430 小米笔记本电脑!");
Items items_2 = new Items();
items_2.setName("小米手机");
items_2.setPrice(5000f);
items_2.setDetail("mix小米手机!");
list.add(items_1);
list.add(items_2);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsList",list);
modelAndView.setViewName("WEB-INF/items/itemsList.jsp");
return modelAndView;
}
}
2.适配器HttpRequestHandlerAdapter
- 要求编写的Handler实现
HttpRequestHandler
接口
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
package spring.ssm.controller;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import spring.ssm.pojo.Items;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ubuntu on 17-7-6.
*/
//实现Controller接口的处理器
public class ItemsController2 implements HttpRequestHandler {
public void handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
List<Items> list = new ArrayList<Items>();
Items items_1 = new Items();
items_1.setName("小米笔记本");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430 小米笔记本电脑!");
Items items_2 = new Items();
items_2.setName("小米手机");
items_2.setPrice(5000f);
items_2.setDetail("mix小米手机!");
list.add(items_1);
list.add(items_2);
httpServletRequest.setAttribute("itemsList",list);
httpServletRequest.getRequestDispatcher("WEB-INF/items/itemsList.jsp").forward(httpServletRequest,httpServletResponse);
}
}
总结:映射器就是通过URL找Handler(Controller)
适配器就是按照要求的规则执行Handler(Controller)
.
上一篇文章:SpringMVC框架入门--(学习笔记)