How to use the annotations of spring MVC framework?

Spring MVC annotation method

  • @Controller
  • @RequestMapping
  • @SessionAttributes

Case practice

@Controller definition

In spring 3.0, class can be defined as a controller class through the @ controller annotation. In order for spring MVC to find the bean defined as controller, you need to add in the servlet context configuration file

As defined below:

<context:component-scan base-package="com.xxx.controller"/> 

Note: in fact, using @ Component can also play the same role as @ Controller.

@RequestMapping

If defined in front of the class, the url is bound to the class.

If defined in front of the method, the url is bound to the method of the class

//url: http://localhost:8080/springmvc01/hello2/test01.do 

@RequestMapping("test01") 

public ModelAndView test01(){ 

    ModelAndView mv=new ModelAndView(); 

    mv.setViewName("hello"); 

    mv.addObject("hello", "hello test01"); 

    return mv; 

} 

//url: http://localhost:8080/springmvc01/hello2.do?test01 

@RequestMapping(params="test02") 

public ModelAndView test02(){ 

    ModelAndView mv=new ModelAndView(); 

    mv.setViewName("hello"); 

    mv.addObject("hello", "hello test02"); 

    return mv; 

} 

//url: http://localhost:8080/springmvc01/hello2/test03.do 

@RequestMapping("test03") 

    public String test03(Model model){ 

    model.addAttribute("hello", "hello test03"); 

    return "hello"; 

} 

//url: http://localhost:8080/springmvc01/hello2/test04.do 

@RequestMapping("test04") 

public String test04(ModelMap modelMap){ 

    modelMap.addAttribute("hello", "hello test04"); 

    return "hello"; 

} 

//url: http://localhost:8080/springmvc01/hello2/test05.do 

@SuppressWarnings("unchecked") 

@RequestMapping("test05") 

public String test05(Map model){ 

    model.put("hello", "hello test05 "); 

    return "hello"; 

} 

@SessionAttributes

Used to declare the attribute of session level storage, which is placed on the processor class (understand)

@Controller 

@SessionAttributes({"userName"})// Put userName into session 

public class UserController { 

    @RequestMapping("/queryUser") 

    public ModelAndView queryUser(String userName){ 

        ModelAndView mv=new ModelAndView(); 

        mv.addObject("userName", userName); 

        mv.setViewName("user"); 

        return mv; 

    } 

} 

Page value

<body> 

    ${sessionScope.a}|||${sessionScope.b} 

</body> 

Comparison of extended ~ common MVC frameworks

Operation performance:

Jsp + Servlet > struts 1 > spring MVC > struts 2 + freemaker > struts 2, ognl, value stack.

In terms of development efficiency, it is basically the opposite. It is worth emphasizing that the development efficiency of spring mvc is equal to that of struts 2, but at present, the popularity of spring mvc has far exceeded that of struts 2.

The reason for the low performance of struts2 is that OGNL (an expression language, through its simple and consistent expression syntax, you can access any attribute of the object, call the method of the object, and use it in combination with the struts 2 Framework) and the value stack (simply understood as the stack for storing struts 2 actions). Therefore, if the system concurrency is high, freemaker can be used for display instead of OGNL and value stack. In this way, the performance will be greatly improved.

Help documentation

Shortcut key

catalogue

title

Text style

list

link

Code slice

form

Footnotes

notes

Custom list

LaTeX mathematical formula

Insert Gantt chart

Insert UML diagram

Insert Mermaid flowchart

Insert Flowchart

Insert class diagram

Shortcut key

Markdown

Icon

Shortcut key

revoke

Ctrl /⌘+Z

redo

Ctrl /⌘+Y

Bold

Ctrl /⌘+B

Italics

Ctrl /⌘+I

title

Ctrl /⌘+Shift +H

Ordered list

Ctrl /⌘+Shift +O

Unordered list

Ctrl /⌘+Shift +U

To do list

Ctrl /⌘+Shift +C

Insert code

Ctrl /⌘+Shift +K

Insert link

Ctrl /⌘+Shift +L

Insert picture

Ctrl /⌘+Shift +G

lookup

Ctrl /⌘+F

replace

Ctrl /⌘+G

Spring MVC annotation method

Case practice

@Controller definition

@RequestMapping

@SessionAttributes

Comparison of extended ~ common MVC frameworks

Tags: Java

Posted by thientanchuong on Sat, 07 May 2022 10:37:18 +0300