怎样分析SpringMVC中的HandlerAdapter

这篇文章给大家介绍怎样分析SpringMVC中的HandlerAdapter,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

DispatcherServletHandlerAdapter关系

处理器映射器和处理器适配器的配置方式有三种:

①  xml配置
<!-- 配置处理器适配器 SimpleControllerHandlerAdapter-处理器需要实现Controller接口 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<!-- 配置处理器映射器 BeanNameUrlHandlerMapping-使用bean的名字进行映射 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- 配置处理器适配器 -->
<bean name="/test.action" class="com.undergrowth.controller.TestController1"></bean>
<bean id="testController1" class="com.undergrowth.controller.TestHttpRequestController"></bean>
 
②xml配置
<!-- HttpRequestHandlerAdapter-处理器需要实现HttpRequestHandler接口 -->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>
<!-- SimpleUrlHandlerMapping-使用bean的id和路径进行映射 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/test1.action">testController1</prop>
</props>
</property>
</bean>
 

③通过注解的方式

<!-- 使用注解方式配置处理器映射器和处理器适配器 -->
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.undergrowth.controller"></context:component-scan>

来看第一种方式的实现:

/**
 * 自定义处理器
 *  实现Controller接口
 */

 

public class TestController1 implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest request,                             
                              HttpServletResponse response) throws Exception {
        //创建数据
        List<String> list=new ArrayList<>();
        list.add("qq");
        list.add("ww");
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("list", list);
        modelAndView.setViewName("/WEB-INF/test/test.jsp");
        return modelAndView;
    }
}

 

第二种方式实现:

/**
 * 实现HttpRequestHandler接口 

 */
public class TestHttpRequestController implements HttpRequestHandler {
    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 创建数据
        List<String> list = new ArrayList<>();
        list.add("qq");
        list.add("ww");
        list.add("ee");
        request.setAttribute("list", list);
        request.getRequestDispatcher("/WEB-INF/test/test.jsp").forward(request, response);
    }
}

第三种方式实现:

/**
 * 测试注解处理器映射器与处理器适配器
 */
@Controller
public class TestAnnotationController {
    @RequestMapping("/test3.action")
    public ModelAndView test(){
        List<String> list=new ArrayList<>();
        list.add("qq");
        list.add("ww");
        list.add("ee");
        list.add("rr");
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("list", list);
        modelAndView.setViewName("/WEB-INF/test/test.jsp");
        return modelAndView;
    }
}

关于怎样分析SpringMVC中的HandlerAdapter就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接