SPRINGMVC annotation Optimization

Source: Internet
Author: User

    • Http://www.blogbus.com/wanping-logs/235898656.html

      Review:

      As mentioned in the previous section, enabling spring annotations is mainly two parts: one is to enable the scan package, the other is to open the annotation, inject two beans, no ID, no name, and spring automatically calls the two packages after opening the annotations. These two beans were replaced after 2 and 3.

      Optimized configuration Note Driver Alternative annotations Two packages initiated by configuration

      Since the two beans are used each time, MVC adds a note driver without adding the two packages, as long as the annotation driver is added.

      <mvc:annotation-driver/>

      In the SPRINGMVC configuration file

      Add the MVC note-driven configuration to remove the two-pack configuration of annotations starting as follows:

      <?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 ">

      <!--annotation Scan Package--

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

      <!--opening annotations--

      <mvc:annotation-driven/>

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

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

      <!--static resource access--

      <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 Code Identification

      To differentiate, create a new Java class named: User2controller.java. The code content does not change, just change the module identity from user to User2, otherwise after Tomcat starts, Access user identity, the service does not know to return that result page.

      The User2controller.java code is as follows:

      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----optimized version";

      Request.setattribute ("result", result);

      return "/annotation";

      }

      @RequestMapping ("/deluser")

      Public String Deluser (HttpServletRequest request) {

      String result = "This is deluser------optimized version";

      Request.setattribute ("result", result);

      return "/annotation";

      }

      @RequestMapping ("/touser")

      Public String Touser (HttpServletRequest request) {

      return "/annotation";

      }

      }

      Deploy Tomcat to launch access to the following page

      Localhost:8080/springmvc5/user2/adduser

      The system normally provides services

      Controller class optimization, add root directory

      Optimized controller, class configuration @requestmapping to differentiate labels

      The @requestmapping class can also be configured to differentiate the label User2.

      Controller class Optimization: Remove unnecessary parameters (remove request type post, get)

      Remove method Label @requestmapping inside method

      Login to distinguish between get and post, other places do not need to distinguish so thin, but also increase the workload. The configuration of Get can also, post also can, so efficiency greatly improved.

      @RequestMapping Remove the method

      Public @RequestMapping (value= "/adduser", Method=requestmethod.get)

      Switch

      Public @RequestMapping (value= "/adduser")

      Public @RequestMapping (value= "/deluser", Method=requestmethod.get)

      Switch

      Public @RequestMapping (value= "/deluser")

      Public @RequestMapping (value= "/touser", Method=requestmethod.get)

      Switch

      Public @RequestMapping (value= "/touser")

      Restart Tomcat to achieve the same effect

      Summarize:

      The Requestmapping method can also implement the post and get methods after removing the method of sending the message.

      The same place to wrap it up

      Controller class Optimization: Remove the value= inside the @requestmapping

      Method label @requestmapping The first parameter value is removed

      Method Tags:

      @RequestMapping (value= "/adduser"), changed to @requestmapping ("/adduser");

      @ requestmapping (value= "/deluser"), changed to @requestmapping ("/deluser");

      @ requestmapping (value= "/touser"), changed to @requestmapping ("/touser");

      Same effect.

      Controller class Optimization: Simple return value

      return new Modelandview ("/annotation", "result", result);

      Switch

      return "/annotation";

      Restart Tomcat, access the page, same effect

      Controller class Optimization: Parameter passing with traditional Request.setattribute

      AddUser (), Deluser (), Touser () add parameter HttpServletRequest request

      Increase

      Request.setattribute ("result", result);

      Also achieves the effect of parameter passing.

      Summarize:

      The optimization of SPRINGMVC is mainly carried out in two major ways:

      <!--[if!supportlists]-->1. <!--[endif]--> configuration file optimization

      Added MVC tag----Note Driver <mvc:annotation-driven/> instead of two beans:

      Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

      Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

      Make SPRINGMVC easy to configure.

      <!--[if!supportlists]-->2. <!--[Endif]-->controller class optimization

      <!--[if!supportlists]-->1) <!--[Endif]-->controller class optimization, adding root directory

      <!--[if!supportlists]-->2) <!--[endif]--> Remove unnecessary parameters, that is, remove the HTTP request method post, get

      <!--[if!supportlists]-->3) <!--[endif]--> Remove the "value=" in the method label @requestmapping, passing value only

      <!--[if!supportlists]-->4) <!--The return value of the [endif]--> is simple, the previous return value is Modelandview, simplified, you can treat Modelandview as a string, The return value is a string, and the corresponding return information becomes simple, returning only simple string information.

      <!--[if!supportlists]-->5) <!--[endif]--> parameter pass, you can pass the previous Modelandview parameter, Use the String property setting (Request.setattribute) instead, and don't forget to include the request definition in the method, HttpServletRequest request.

      The control class optimizes, increases the root directory, removes the unnecessary parameter, the return value is simple, the parameter passes with the traditional request.setattribute to obtain.

      Parameter passing and return values can be replaced by strings.

      SPRINGMVC Basic Annotation configuration for the most concise configuration

      There is no dependency between controllers

      Very few configurations

SPRINGMVC annotation Optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.