• 【java框架】SpringMVC(2)--SpringMVC实现Controller方式


    1.   SpringMVC的Controller实现方式

    SpringMVC实现Controller的方式主要有控制器实现方式全注解实现方式,其中全注解实现方式是当前项目中比较常用的一种方式。

    1.1.控制器实现方式

    1.1.1.     实现Controller接口

    创建一个类实现Controller接口:

    /**
     * 实现Controller方式一:
     *      实现一个Controller接口,实现handleRequest方法
     *      并且在Springmvc的配置文件中配置这个bean,指定Demo1Controller的访问路径
    */
    public class Demo1Controller implements Controller {
      @Override
      public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {
        System.out.println("进入demo1Controller视图Model实现方式...");
            //创建ModelAndView对象
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/WEB-INF/views/demo1Controller.jsp");
        return modelAndView;
      }
    }

    配置applicationContext-mvc.xml文件,配置bean交给Spring来管理:

    <?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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!--开启静态资源的访问-->
        <mvc:default-servlet-handler />
        <!--SpringMVC的配置文件:把控制器类交给Spring来管理-->
        <!--name:访问的映射路径-->
        <!--Controller实现方式一配置-->
        <bean name="/demo1Controller" class="cn.yif.controllerImpl01.Demo1Controller"></bean>
    </beans>

    1.1.2.     实现HttpRequestHandler接口

    创建一个类来实现HttpRequestHandler接口,实现handleRequest方法:

    /**
     * 实现Controller方式二:
     *      实现一个HttpRequestHandler接口,实现handleRequest方法
     *      并且在Springmvc的配置文件中配置这个bean,指定Demo2Controller的访问路径
     */
    public class Demo2Controller implements HttpRequestHandler {
        @Override
        public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.out.println("进入demo2Controller视图Model实现方式...");
            //获取参数
            //request.getParameter("name");
            //转发
            request.getRequestDispatcher("/WEB-INF/views/demo2Controller.jsp").forward(request, response);
        }
    }

    配置applicationContext-mvc.xml文件,配置bean交给Spring来管理:

    <?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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!--开启静态资源的访问-->
        <mvc:default-servlet-handler />
        <!--SpringMVC的配置文件:把控制器类交给Spring来管理-->
        <!--name:访问的映射路径-->
        <!--Controller实现方式二配置-->
        <bean name="/demo2Controller" class="cn.yif.controllerImpl02.Demo2Controller"></bean>
    </beans>

    1.1.3.     普通类(POJO)注解实现

    创建一个类,在类中可以提供多个method方法,使用@RequestMapping映射类路径+方法路径,,这样一个类中就可以配置多个访问路径:

    /**
     * 实现Controller方式三:
     *      普通类(POJO)和注解@RequestMapping
     *      配置访问路径:类路径+方法路径
     *      这样一个类中可以配置多个方法、多个方法url映射
     *      同样需要在applicationContext-mvc.xml中配置bean
     *      而且需要开启SpringMVC的注解支持:识别并扫描类上面的注解@RequestMapping
     */
    @RequestMapping("/demo3Controller")
    public class Demo3Controller {
        @RequestMapping("/add")
        public ModelAndView add(){
            System.out.println("进入demo3Controller视图Model的add方法...");
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("/WEB-INF/views/demo3Controller_add.jsp");
            return modelAndView;
        }
    
        @RequestMapping("/del")
        public ModelAndView del(){
            System.out.println("进入demo3Controller视图Model的del方法...");
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("/WEB-INF/views/demo3Controller_del.jsp");
            return modelAndView;
        }
    }

    配置applicationContext-mvc.xml文件,配置bean交给Spring来管理,无需配置name访问路径,注意:必须配置开启SpringMVC注解配置的扫描:

    <?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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!--开启静态资源的访问-->
        <mvc:default-servlet-handler />
        <!--开启SpringMVC对注解的支持-->
        <mvc:annotation-driven />
        <!--SpringMVC的配置文件:把控制器类交给Spring来管理-->
        <!--Controller实现方式三配置:在Controller类中配置了url,这里无需配置-->
        <bean class="cn.yif.controllerImpl03.Demo3Controller"></bean>
    </beans>

    1.2.全注解实现方式

    全注解实现方式较控制器实现方式简单,而且只需要我们普通的一个Controller类上面添加@Controller与@RequestMapping注解即可,是一种项目中常见的注解配置方式。

    主要的步骤如下:

    ①    创建一个普通的类,在类上面配置@Controller、@RequestMapping注解;

    /**
      * 实现Springmvc全注解方式:
      *      写一个普通的Controller类
    *      无需在applicationContext-mvc.xml中配置bean,只需要使用@Controller告诉Spring这是一个bean
    *      通过@RequestMapping在类与方法上映射请求路径,可以映射多个方法的请求路径
    */
    @Controller
    @RequestMapping("/annoController")
    public class AnnotationController {
        @RequestMapping("/add")
        public ModelAndView add(){
            System.out.println("进入AnnotationController中的add方法...");
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("/WEB-INF/views/annoController_add.jsp");
            return modelAndView;
        }
    
        @RequestMapping("/del")
        public ModelAndView del(){
            System.out.println("进入AnnotationController中的add方法...");
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("/WEB-INF/views/annoController_del.jsp");
            return modelAndView;
        }
    }

    ②    在applicationContext-mvc.xml中配置—开启对SpringMVC注解的支持、配置包的扫描(具体到哪个路径下去扫描@Controller)、兼容Spring3.2版本的配置,具体配置如下:

    applicationContext-mvc.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:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
        <!--开启静态资源的访问-->
        <mvc:default-servlet-handler />
        <!--开启SpringMVC对注解的支持-->
        <mvc:annotation-driven />
        <!-- 进行包的扫描,去看类上面是否有相应的标签配置:包含@Component、@Controller、@Service、@Repository -->
        <context:component-scan base-package="cn.yif.controllerallannoImpl" />
        <!-- 这个不是必须的(spring3.2版本前使用) 配上后兼容性好 -->
        <context:annotation-config />
    </beans>

    ③   注意事项:还需要加上spring-aop.jar包,否则在处理注解映射时无法找到对应切面的映射会抛出aop异常:

    Caused by: java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource

    解决方案,导入spring-aop-4.1.2.RELEASE.jar包即可。

    对应方法访问页面/annoController/add与annoController/del:

     

  • 相关阅读:
    png图片在ie不透明的解决方案
    ASP如何查询ACCESS数据库中上一周的所有记录
    使用FSO修改文件夹的名称
    两组字符串数据比较合并相同数据
    1272 并查集
    1232 并查集
    What's New in ASP.NET 4.5 and Visual Web Developer 11 Developer Preview
    knockout.js
    .net training
    Unknown server tag 'asp:ListView'. sharepoint
  • 原文地址:https://www.cnblogs.com/yif0118/p/12494239.html
Copyright © 2020-2023  润新知