Developing spring MVC with annotations

Take a step forward every day 2021-02-23 17:22:45
developing spring mvc annotations


Spring2.5 in the future , Development with annotation SpringMVC It's very powerful , So are annotations SpringMVC The essence of . In actual development , Will use annotations to achieve .
This makes SpringMVC Minimize development effort , Developers only need to focus on the business logic and the implementation of the page .

1、web.xml file

To configure DispatcherServlet, And the corresponding servlet-mapping.
This file is in addition to servlet-name, and springmvc Profile name for , The rest is fixed , Reusable .

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- register DispatcherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Associated with a springmvc Configuration file for , The naming rule is :【servlet-name】-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--/ Match all requests : barring .jsp-->
<!--/* Match all requests : Include .jsp-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

2、Spring bean The configuration file

Follow the official nomenclature , This is used here springmvc-servlet.xml. This file is in addition to the package path to scan , The rest is fixed , It can be reused directly .

  • Open the annotation

    • newly added context constraint
    • Turn on auto scan package
  • Static resource filtering

    • Use default default-servlet-handler
    • Give Way Spring MVC Don't deal with static resources .css .js .html .mp3 .mp4
  • Support mvc Annotation driven

    • stay spring Generally used in @RequestMapping Annotation to complete the mapping relationship
      To make @RequestMapping Annotations to take effect
      You must register with the context DefaultAnnotationHandlerMapping
      And a AnnotationMethodHandlerAdapter example
      These two instances are handled at the class level and the method level, respectively .
      and annotation-driven Configuration helps us to automatically inject the above two instances .
  • view resolver

    • By default InternalResourceViewResolver
    • To configure .jsp The prefix of the full path file name 、 suffix .
<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Auto scan package , Make the annotation under the specified package effective , from IOC Container management -->
<context:component-scan base-package="controller"/>
<!-- Give Way Spring MVC Don't deal with static resources .css .js .html .mp3 .mp4-->
<mvc:default-servlet-handler/>
<!-- Support mvc Annotation driven
stay spring Generally used in @RequestMapping Annotation to complete the mapping relationship
To make @RequestMapping Annotations to take effect
You must register with the context DefaultAnnotationHandlerMapping
And a AnnotationMethodHandlerAdapter example
These two instances are handled at the class level and the method level, respectively .
and annotation-driven Configuration helps us to automatically inject the above two instances .-->
<mvc:annotation-driven/>
<!-- view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- Prefix -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- suffix -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>

3、Controller Class writing

Develop... Using annotations SpringMVC,99% The amount of work we do is controller Class writing .SpringMVC Annotation Minimize development .

Controller Responsible for resolving user requests , Conduct business processing , And return a model .

Controller Through the implementation of interface and annotation definition two methods to achieve .

Realized Controller The class of the interface is the controller , This is the older way .

A controller class can only have one method . Multiple methods need to write multiple controller.

It is recommended to define the implementation with annotations .

  • @Controller

    • decorator , Ensure that the class can be Spring Automatically scan to , Automatic assembly for Spring bean
  • @RequestMapping

    Request address , take controller Class or its specific methods map to the specified front-end view

    • decorator , Represents the response path of all methods of this class, with this address as the parent path .
    • Modification methods : Represents the response path of the method , Method to respond to the specified front end view .
  • return

    • Return value : View jsp file name

    • @Controller In annotated classes , If the method returns the value corresponding to jsp The file name exists , Will be parsed by the view parser

@Controller // decorator , Ensure that the class can be Spring Automatically scan to , Automatic assembly for Spring bean
@RequestMapping("controller") // decorator , Represents the response path of all methods of this class, with this address as the parent path
public class MyController {
// complete url:http://localhost:8080/( project name )/controller/hello
@RequestMapping("/hello") // Modification methods : Represents the response path of the method , Method to respond to the specified front end view .
public String sayHello(Model model){
model.addAttribute("msg", "Hello!SpringMVC Annotation!");
// Return value : View jsp file name
//@Controller In annotated classes , If the method returns the value corresponding to jsp The file name exists , Will be parsed by the view parser
return "hello";
}
}

4、 To write hello.jsp page

Put it in /WEB-INF/jsp/ Under the path

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Zuka</title>
</head>
<body>
${msg}
</body>
</html>

5、 test

url:http://localhost:8080/springmvc_annotation_war_exploded/controller/hello

版权声明
本文为[Take a step forward every day]所创,转载请带上原文链接,感谢
https://javamana.com/2021/02/20210223172137007y.html

  1. 忘记云服务器上MySQL数据库的root密码时如何重置密码?
  2. Detailed explanation of lettuce, an advanced client of redis
  3. Springboot learning integrated application (1)
  4. Linux File Recover
  5. Linux-Security
  6. How to reset the password when you forget the root password of MySQL database on the cloud server?
  7. Linux File Recover
  8. Linux-Security
  9. LiteOS:盘点那些重要的数据结构
  10. Linux Memory
  11. Liteos: inventory those important data structures
  12. Linux Memory
  13. 手把手教你使用IDEA2020创建SpringBoot项目
  14. Hand in hand to teach you how to create a springboot project with idea2020
  15. spring boot 整合swagger2生成API文档
  16. Spring boot integrates swagger2 to generate API documents
  17. linux操作系统重启后 解决nginx的pid消失问题
  18. Solve the problem of nginx PID disappearing after Linux operating system restart
  19. JAVA版本号含义
  20. The meaning of java version number
  21. 开源办公开发平台丨Mysql5.7两套四节点主从结构环境搭建教程(二)
  22. 开源办公开发平台丨Mysql5.7两套四节点主从结构环境搭建教程(一)
  23. Open source office development platform mysql5.7 two sets of four node master-slave structure environment building tutorial (2)
  24. HTTP的“无状态”和REST的“状态转换”
  25. Open source office development platform mysql5.7 two sets of four node master-slave structure environment building tutorial (1)
  26. 【大数据哔哔集20210128】使用Hive计算环比和同比
  27. 【大数据哔哔集20210125】Kafka将逐步弃用对zookeeper的依赖
  28. 【大数据哔哔集20210124】有人问我Kafka Leader选举?我真没慌
  29. 【大数据哔哔集20210123】别问,问就是Kafka高可靠
  30. Spring 事务、异步和循环依赖有什么关系?
  31. Spring 动态代理时是如何解决循环依赖的?为什么要使用三级缓存?
  32. "Stateless" of HTTP and "state transition" of rest
  33. [big data bibiji 20210128] use hive to calculate month on month and year on year
  34. [big data bibiji 20210125] Kafka will gradually abandon its dependence on zookeeper
  35. [big data beeps 20210124] someone asked me about Kafka leader election? I'm not in a panic
  36. [big data bibiji 20210123] don't ask, ask is Kafka highly reliable
  37. jQuery Gantt Package 在Visual Studio中创建一个新的ASP.NET项目
  38. What is the relationship between spring transactions, asynchrony, and circular dependencies?
  39. How to solve circular dependency in spring dynamic proxy? Why use level 3 caching?
  40. Unicode码的二进制转换(Java)
  41. JAVA的函数式接口
  42. JAVA里的元祖类
  43. JQuery Gantt package to create a new ASP.NET project
  44. Binary conversion of Unicode code (Java)
  45. The functional interface of Java
  46. Yuanzu class in Java
  47. Java中的CPU占用高和内存占用高的问题排查
  48. HashMap连环问你能答出几道?
  49. IntelliJ IDEA 还能画思维导图,果然最强 IDE!
  50. java中的反射和代理
  51. Troubleshooting of high CPU and memory usage in Java
  52. How many questions can you answer?
  53. IntelliJ idea can also draw mind maps. It's really the strongest ide!
  54. Reflection and proxy in Java
  55. Java中的CPU占用高和内存占用高的问题排查
  56. Linux OOM(out of memory)
  57. mysql 自定义函数因参数名称报错
  58. Troubleshooting of high CPU and memory usage in Java
  59. Linux OOM(out of memory)
  60. MySQL user defined function error due to parameter name