1. 程式人生 > >第九章.處理器對映器和介面卡

第九章.處理器對映器和介面卡

 

本次課程在 第八章 基礎上進行的!!!

 

非註解的處理器對映器和介面卡

一.SimpleUrlHandlerMapping

找到springmvc.xml檔案

找到下面的程式碼進行註釋

<!--  
<bean name="/query_test.action" class="cn.com.mvc.controller.FruitControllerTest"></bean>
-->

<!-- 
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
-->

 並且新增程式碼:

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/queryFruits_test1.action">fruitController</prop>
            <prop key="/queryFruits_test2.action
">fruitController</prop> </props> </property> </bean> <bean id="fruitController" class="cn.com.mvc.controller.FruitControllerTest"></bean>

 

 測試程式碼的地址是否可以成功訪問

 

 二.

在controller包下建立新的類,如下圖

 

FruitControllerTest2.java程式碼實現:

public class FruitControllerTest2 implements HttpRequestHandler {
    private FruitsService fruitsService = new FruitsService();
    @Override
    public void handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
        //模擬Service獲取水果商品列表
        List<Fruits> fruitsList = fruitsService.queryFruitsList();
        //設定模型資料
        arg0.setAttribute("fruitsList", fruitsList);
        //設定轉檢視
        arg0.getRequestDispatcher("/WEB-INF/jsp/fruits/fruit.jsp").forward(arg0, arg1);    
    }
}

 

 找到springmvc.xml

新增程式碼

注意紅顏色的部分

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
    <props>
      <prop key="/queryFruits_test1.action">fruitController</prop>
      <prop key="/queryFruits_test2.action">fruitController</prop>


      <prop key="/queryFruits_test3.action">fruitController2</prop>
    </props>
  </property>
</bean>


<bean id="fruitController2" class="cn.com.mvc.controller.FruitControllerTest2"></bean>


<!-- /queryFruits_test3.action -->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>

 

 進行測試:

 

 

三.JSON

修改FruitControllerTest2.java檔案

public class FruitControllerTest2 implements HttpRequestHandler {

    private FruitsService fruitsService = new FruitsService();

    @Override
    public void handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
        //模擬Service獲取水果商品列表
        List<Fruits> fruitsList = fruitsService.queryFruitsList();
        //將fruitsList轉為JSON串
        String josnInfo = convertListToJson(fruitsList);
        //設定返回格式
        arg1.setCharacterEncoding("utf-8");
        arg1.setContentType("application/json;charset=utf-8");
        //寫出JSON
        arg1.getWriter().write(josnInfo);
        
    }
    
    private String convertListToJson(List<Fruits> fruitsList){
        
        StringBuilder builder = new StringBuilder();
        builder.append('[');
        for(Fruits fruits: fruitsList){
            builder.append('{');
            builder.append("\"name\":\"").append(fruits.getName()).append("\",");
            builder.append("\"price\":\"").append(fruits.getPrice()).append("\",");
            builder.append("\"producing_area\":\"").append(fruits.getProducing_area()).append("\",");
            builder.append("},");
        }
        builder.deleteCharAt(builder.length() - 1);
        builder.append("]");    
        return builder.toString();
    }
}

 

其餘不變訪問中的地址

注意此時需要在瀏覽器上測試

http://localhost:8080/SpringMVC_1/queryFruits_test3.action