天天看点

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;
           

继续阅读