上接lesson1
重要部分
1.springmvc.xml 配置了处理器映射器,处理器(handler),适配器,视图解析器等
多个处理器映射器,适配器等可以共存
2.Handler的实现类 ItemsController1,java , ItemsController2,java
1.springmvc.xml 配置了处理器映射器,处理器(handler),适配器,视图解析器等
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 处理器Handler: 处理器映射器映射的具体的处理器 -->
<bean id="itemController1" name="/queryItems.action" class="cn.itcast.ssm.controller.ItemsController1"/>
<bean id="itemController2" class="cn.itcast.ssm.controller.ItemsController2"/>
<!-- 处理器适配器: 所有处理器适配器都实现 HandlerAdapter接口 -->
<bean
class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<!-- 处理器适配器:(第二种) 实现 -->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />
<!-- 处理器映射器 : 将Bean的 name的 url进行查找,需要在配置Handler指定Beanname(就是url) -->
<bean
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/queryItems1.action">itemController1</prop>
<prop key="/queryItems2.action">itemController1</prop>
<prop key="/queryItems3.action">itemController2</prop>
</props>
</property>
</bean>
<!-- 视图解析器 -->
<!-- ViewResolver
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>
</beans>
2.Handler的实现类
ItemsController1,java ,
package cn.itcast.ssm.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import cn.itcast.ssm.po.Items;
public class ItemsController1 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
List<Items> itemsList = new ArrayList<>();
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("iphone6苹果手机!");
itemsList.add(items_1);
itemsList.add(items_2);
// 返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
// 相当于request 的 setAttribute, 在 jsp 页面中通过 itemList 取数据
modelAndView.addObject("itemsList", itemsList);
// 指定视图
modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
return modelAndView;
}
}
ItemsController2,java
package cn.itcast.ssm.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.HttpRequestHandler;
import cn.itcast.ssm.po.Items;
public class ItemsController2 implements HttpRequestHandler {
@Override
public void handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
List<Items> itemsList = new ArrayList<>();
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("iphone6苹果手机!");
itemsList.add(items_1);
itemsList.add(items_2);
request.setAttribute("itemsList", itemsList);
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串");
request.getRequestDispatcher("/WEB-INF/jsp/items/itemsList.jsp")
.forward(request, response);
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串");
}
}