天天看點

ssm之路(13)各種映射器和擴充卡的配置(聲明式與注解式開發配置)+視圖解析器配置前字尾

與12中的 <!--處理器映射器-->

   <!-- 将bean的name作為url進行查找,我的bean就是handler,,故配置handler時要指定beanname(就是url),配置内容在上文-->

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

對應的另一種映射器是:簡單url映射器:(多個路徑通路統一類或方法),結論:多url可以共存。

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!--   配置handler-->
    <bean id="itemsController1" name="/queryItems.action" class="cn.itcast.ssm.controller.ItemsController1"/>
    <bean id="itemsController2" class="cn.itcast.ssm.controller.ItemsController2"/>
    <!-- 配置另一個handler-->
   <!-- <bean id="itemsController2" class="cn.itcast.ssm.controller.ItemsController2"/>-->

  <!-- 處理器映射器&ndash;&gt;
   &lt;!&ndash; 将bean的name作為url進行查找,我的bean就是handler,,故配置handler時要指定beanname(就是url),配置内容在上文&ndash;&gt;-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/queryItems1.action">itemsController1</prop>
                <prop key="/queryItems2.action">itemsController1</prop>
                <prop key="/queryItems3.action">itemsController2</prop>
            </props>
        </property>
    </bean>
 <!--  &lt;!&ndash; 注解映射器:&ndash;&gt;
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
   &lt;!&ndash; 注解擴充卡:&ndash;&gt;
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>-->
   <!-- 使用mvc:annotation-driver可完全代替上邊注解映射器和注解擴充卡的配置,并且mvc:annotation-driver預設加載很多的參數綁定方法
    比如json的轉換的解析器,它就預設将其加載了-->
   <!-- <mvc:annotation-driven></mvc:annotation-driven>-->



 <!--   處理器擴充卡  所有處理器擴充卡都實作HandlerAdapter接口
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>-->
    <!--HttpRequestHandlerAdapter擴充卡,要求編寫handler實作HttpRequestHandler-->
    <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>

   <!-- 視圖解析器-->
  <!--  解析jsp,預設使用jstl,classpath下的有jstl的包,視圖解析器的有代碼://classname     javax.servlet.jsp.jstl.core.Config-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>

</beans>
           

上篇文章的SimpleControllerHandlerAdapter是非注解處理器擴充卡,需要實作Controller接口

下面是另一種擴充卡:HttpRequestHandlerAdapter擴充卡,要求編寫handler實作HttpRequestHandler:

springmvc.xml代碼如下:

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!--   配置handler-->
    <bean id="itemsController1" name="/queryItems.action" class="cn.itcast.ssm.controller.ItemsController1"/>
    <bean id="itemsController2" class="cn.itcast.ssm.controller.ItemsController2"/>
    <!-- 配置另一個handler-->
   <!-- <bean id="itemsController2" class="cn.itcast.ssm.controller.ItemsController2"/>-->

  <!-- 處理器映射器&ndash;&gt;
   &lt;!&ndash; 将bean的name作為url進行查找,我的bean就是handler,,故配置handler時要指定beanname(就是url),配置内容在上文&ndash;&gt;-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/queryItems1.action">itemsController1</prop>
                <prop key="/queryItems2.action">itemsController1</prop>
                <prop key="/queryItems3.action">itemsController2</prop>
            </props>
        </property>
    </bean>
 <!--  &lt;!&ndash; 注解映射器:&ndash;&gt;
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
   &lt;!&ndash; 注解擴充卡:&ndash;&gt;
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>-->
   <!-- 使用mvc:annotation-driver可完全代替上邊注解映射器和注解擴充卡的配置,并且mvc:annotation-driver預設加載很多的參數綁定方法
    比如json的轉換的解析器,它就預設将其加載了-->
   <!-- <mvc:annotation-driven></mvc:annotation-driven>-->



 <!--   處理器擴充卡  所有處理器擴充卡都實作HandlerAdapter接口
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>-->
    <!--HttpRequestHandlerAdapter擴充卡,要求編寫handler實作HttpRequestHandler-->
    <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>

   <!-- 視圖解析器-->
  <!--  解析jsp,預設使用jstl,classpath下的有jstl的包,視圖解析器的有代碼://classname     javax.servlet.jsp.jstl.core.Config-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>

</beans>
           

修改上一篇的ItemsController1類的代碼如下:

public class ItemsController1 implements HttpRequestHandler {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //調用service查找資料庫,查詢商品清單,這裡使用靜态資料模拟
        List<Items>itemsList=new ArrayList<Items>();
        Items items_1=new Items();
        items_1.setName("第三方");
        items_1.setPrice(23.3f);
        try {
            items_1.setCreatetime(sdf.parse("2018-11-07 09:48:00"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        items_1.setDetail("iphone受i及");
        Items items_2=new Items();
        items_2.setName("第三方");
        items_2.setPrice(23.3f);
        try {
            items_2.setCreatetime(sdf.parse("2018-11-07 09:48:00"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        items_2.setDetail("iphone受i及");
        itemsList.add(items_1);
        itemsList.add(items_2);
        //傳回modelAndView
        request.setAttribute("itemsList",itemsList);
        request.getRequestDispatcher("/WEB-INF/jsp/items/itemsList.jsp").forward(request,response);
        //模型裡有視圖,這裡modelAndView.addObject相當于request的setAttribute,
        // 要在addObject()裡指定key,指定值,在jsp中就可以通過鍵(itemsList)取資料了
    /*    modelAndView.addObject("itemsList",itemsList);
        //指定視圖
        modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
        return modelAndView;*/
    }
}
           
ItemsController2類代碼:
           
public class ItemsController2 implements HttpRequestHandler {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       
        //調用service查找資料庫,查詢商品清單,這裡使用靜态資料模拟
        List<Items>itemsList=new ArrayList<Items>();
        Items items_1=new Items();
        items_1.setName("第三方");
        items_1.setPrice(23.3f);
        try {
            items_1.setCreatetime(sdf.parse("2018-11-07 09:48:00"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        items_1.setDetail("iphone受i及");
        Items items_2=new Items();
        items_2.setName("第三方");
        items_2.setPrice(23.3f);
        try {
            items_2.setCreatetime(sdf.parse("2018-11-07 09:48:00"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        items_2.setDetail("iphone受i及");
        itemsList.add(items_1);
        itemsList.add(items_2);
        //傳回modelAndView
      request.setAttribute("itemsList",itemsList);
      request.getRequestDispatcher("/WEB-INF/jsp/items/itemsList.jsp").forward(request,response);
        //模型裡有視圖,這裡modelAndView.addObject相當于request的setAttribute,
        // 要在addObject()裡指定key,指定值,在jsp中就可以通過鍵(itemsList)取資料了
    /*    modelAndView.addObject("itemsList",itemsList);
        //指定視圖
        modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
        return modelAndView;*/
    }
}
           

浏覽器通路/queryItems.action,/queryItems1.action,/queryItems2.action 。。。都能得到相應的映射:

此種擴充卡的hadleRequest方法沒有傳回ModelView,可通過response修改定義響應的内容,比如傳回json資料,

//。。。。。。。。。。。。
  itemsList.add(items_1);
        itemsList.add(items_2);
        //傳回modelAndView
request.setAttribute("itemsList",itemsList);
response.setCharecterEncoding("utf-8");

respose.setContentType("application/json;chartset=utf-8");

response.getWriter().write("json串");
           

下面是注解式的映射器,擴充卡:spring3.1後引入:

為支援注解式<mvc:annotation-driven></mvc:annotation-driven>的開發配置:需要在springmvc.xml中開頭的<bean xml>中增加配置成如下:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
           

注解式開發的配置檔案:

修改springmvc.xml檔案如下:

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

   <!-- 注解映射器:-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
   <!-- 注解擴充卡:-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
   <!-- 使用mvc:annotation-driver可完全代替上邊注解映射器和注解擴充卡的配置,并且mvc:annotation-driver預設加載很多的參數綁定方法
    比如json的轉換的解析器,它就預設将其加載了
    <mvc:annotation-driven></mvc:annotation-driven>
-->
    <!--對于注解的handler可以單個配置,可以每次手動的在配置檔案中添加bean注冊,但建議使用元件掃描方式注冊bean-->
    <context:component-scan base-package="cn.itcast.ssm.controller"/>
    <!--HttpRequestHandlerAdapter擴充卡,要求編寫handler實作HttpRequestHandler-->
    <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>

   <!-- 視圖解析器-->
  <!--  解析jsp,預設使用jstl,classpath下的有jstl的包,視圖解析器的有代碼://classname     javax.servlet.jsp.jstl.core.Config-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>

</beans>
           
ItemsController3類的代碼:
           
@Controller
public class ItemsController3 {
    @RequestMapping("/queryItems")
    public ModelAndView queryItems() throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Items> itemsList = new ArrayList<Items>();
        Items items_1 = new Items();
        items_1.setName("第三方");
        items_1.setPrice(23.3f);
        try {
            items_1.setCreatetime(sdf.parse("2018-11-07 09:48:00"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        items_1.setDetail("iphone受i及");
        Items items_2 = new Items();
        items_2.setName("第三方");
        items_2.setPrice(23.3f);
        try {
            items_2.setCreatetime(sdf.parse("2018-11-07 09:48:00"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        items_2.setDetail("iphone受i及");
        itemsList.add(items_1);
        itemsList.add(items_2);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
        modelAndView.addObject("itemsList",itemsList);
        return modelAndView;
    }

}
           

浏覽器通路http://localhost:8080/queryItems.action即可看到結果。

注解擴充卡和注解映射器是配對使用的,注解擴充卡不能使用非注解映射器進行映射。

下面在視圖解析器中配置前字尾:

視圖解析器中代碼變為:

<!-- 視圖解析器-->
  <!--  解析jsp,預設使用jstl,classpath下的有jstl的包,視圖解析器的有代碼://classname     javax.servlet.jsp.jstl.core.Config-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp"/>
        <property name="suffix" value=".jsp"/>
    </bean>
           
ItemsController3的代碼相應為:
           
//。。。。。。。。。 
modelAndView.setViewName("items/itemsList");
        modelAndView.addObject("itemsList",itemsList);
        return modelAndView;
           

繼續閱讀