zoukankan      html  css  js  c++  java
  • Springmvc注解优化

      •  

        复习:

        上一节讲过,启用spring注解主要是两部分:一是启用扫描包,二是开启注解,注入两个bean,没有id,没有name,开启注解后,spring自动去调用这两个包。这两个bean在2、3后被代替了。

        优化配置

        注解驱动替代注解启动的两个包的配置

        因为每次都要使用这两个bean,所以mvc加入一个注解驱动,不用加这两个包,只要加入注解驱动即可。

        <mvc:annotation-driver/>

        Springmvc的配置文件中

        增加mvc注解驱动配置,去掉注解启动的两个包的配置,如下:

        <?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: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-3.0.xsd  

                    http://www.springframework.org/schema/context 

                    http://www.springframework.org/schema/context/spring-context-3.0.xsd  

                    http://www.springframework.org/schema/mvc 

                    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  

             <!-- 注解扫描包 -->

             <context:component-scan base-package="com.tgb.web.controller.annotation" />

             <!-- 开启注解 -->   

             <mvc:annotation-driven/>  

             

             <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>

             <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>-->

             <!-- 静态资源访问 -->

             <mvc:resources location="/img/" mapping="/img/**"/>

            <mvc:resources location="/js/" mapping="/js/**"/>

           <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

           <property name="prefix" value="/"></property>

           <property name="suffix" value=".jsp"></property>

           </bean>

        </beans>  

        controller代码标识

        为了区分,新建一个java的类,名为:User2Controller.java。代码内容不变,只是把模块标识从user改成user2,否则tomcat启动后,访问user标识,服务不知道该返回那个结果页面。

        User2Controller.java代码如下:

        package com.tgb.web.controller.annotation;

        import javax.servlet.http.HttpServletRequest;

        import org.springframework.stereotype.Controller;

        import org.springframework.web.bind.annotation.RequestMapping;

        import org.springframework.web.bind.annotation.RequestMethod;

        import org.springframework.web.servlet.ModelAndView;

        @Controller

        @RequestMapping("/user2")

        public class User2Controller {

            @RequestMapping("/addUser")

        public String addUser(HttpServletRequest request){

            String result = "this is addUser----优化版";

            request.setAttribute("result", result);

        return "/annotation";

        }

            @RequestMapping("/delUser")

        public String delUser(HttpServletRequest request){

            String result = "this is delUser------优化版";

            request.setAttribute("result", result);

        return "/annotation";

        }

            

            @RequestMapping("/toUser")

            public String toUser(HttpServletRequest request){

            return "/annotation";

            }

        }

        部署tomcat,启动访问如下页面

        localhost:8080/springMVC5/user2/addUser

        系统正常提供服务

        Controller类优化,增加根目录

        优化controller,类配置@RequestMapping来区分标签

        类也可以配置@RequestMapping,来区分标签user2。

        Controller类优化:去掉没有必要的参数(去掉请求类型post、get)

        去掉方法标签@RequestMapping里面method方法

        登陆区分get和post,其他地方没有必要区分这么细,还增加工作量。配置下get也能,post也能,这样效率大大提高。

        @RequestMapping把method方法删除掉

        public @RequestMapping(value=“/addUser”,method=RequestMethod.GET)

        改为

        public @RequestMapping(value=“/addUser”)

        public @RequestMapping(value=“/delUser”,method=RequestMethod.GET)

        改为

        public @RequestMapping(value=“/delUser”)

        public @RequestMapping(value=“/toUser”,method=RequestMethod.GET)

        改为

        public @RequestMapping(value=“/toUser”)

        重启tomcat,达到相同的效果

        总结:

        方法的标签RequestMapping去掉发送消息的方法后,同样可以实现post和get方法。

        不变的地方封装起来

        Controller类优化:去掉@RequestMapping里面的value=

        方法标签@RequestMapping第一个参数value去掉

        方法标签:

        @RequestMapping(value=”/addUser”),改为@RequestMapping(“/addUser”);

        @ RequestMapping(value=”/delUser”),改为@RequestMapping(“/delUser”);

        @ RequestMapping(value=”/toUser”),改为@RequestMapping(“/toUser”);

        同样达到相同的效果

        Controller类优化:返回值简单

        return new ModelAndView(“/annotation”,”result”,result);

        改为

        return “/annotation”;

        重启tomcat,访问页面,同样达到相同的效果

        Controller类优化:参数传递用传统的request.setAttribute

        addUser()、delUser()、toUser()增加参数HttpServletRequest request

        增加

        request.setAttribute(“result”,result);

        同样达到参数传递的效果。

        总结:

        Springmvc的优化主要从两大方面来进行的:

        <!--[if !supportLists]-->1.       <!--[endif]-->配置文件优化

        增加了mvc标签----注解驱动<mvc:annotation-driven/>来代替两个bean:

        org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

        org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

        让springmvc得配置变得简单。

        <!--[if !supportLists]-->2.       <!--[endif]-->Controller类优化

        <!--[if !supportLists]-->1)  <!--[endif]-->controller类优化,增加根目录

        <!--[if !supportLists]-->2)  <!--[endif]-->去掉没有必要的参数,即去掉了http请求的方法post、get

        <!--[if !supportLists]-->3)  <!--[endif]-->去掉方法标签@RequestMapping里面的”value=”,只传递value的值

        <!--[if !supportLists]-->4)  <!--[endif]-->返回值简单,之前的返回值为ModelAndView,简化后,可以把ModelAndView当成字符串来对待,返回值为字符串,相应的return信息变得简单,只返回简单的字符串信息。

        <!--[if !supportLists]-->5)  <!--[endif]-->参数传递,可以把之前的ModelAndView参数传递,用字符串属性设置(request.setAttribute)来代替,同时别忘记了在方法里加入request的定义,即HttpServletRequest request。

        Control类优化,增加根目录;去掉没必要的参数;返回值简单;参数传递用传统的request.setAttribute来取得。

        参数传递和返回值可以用字符串代替。

        Springmvc基础注解配置最简洁的配置

        Controller之间没有依赖

        配置特别少

  • 相关阅读:
    Qt 3d
    yolov5 检测图片里面的对象
    QTreeWidget双击事件
    Qt QPainter QBrush 填充区域
    Qt QWidget保存为图片
    [原][减肥][名词解释]什么是GI
    [原][减肥]生酮减肥,喝防弹咖啡减肥的食谱
    [转][减肥]外源性酮症与内源性生酮
    fastadmin 单独设置导入权限【转载】
    [MySQL]多表关联查询技巧
  • 原文地址:https://www.cnblogs.com/exe19/p/5391666.html
Copyright © 2011-2022 走看看