This article brings you the detailed process of building the SpringMVC environment. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Spring MVC provides an excellent Web framework based on the MVC design concept for the presentation layer. It is currently one of the most mainstream MVC frameworks.
After Spring 3.0, it completely surpassed Struts2 and is called the best MVC framework. After learning SpringMVC, you will feel the cruelty that Struts2 brings to you in an instant.
Spring MVC uses a set of MVC annotations to make POJO a controller for processing requests. There is no need to implement any interface and the coupling degree is low.
And Spring MVC has good support for rest style. .
Utilizes a loosely coupled pluggable component structure, which is more scalable and flexible than other MVC frameworks.
Build a Spring MVC environment
1) Build an MVC environment based on the interface method. Implement the Controller interface to implement MVC
2) Based on the annotation method, in Spring 3.0 and later versions, the use of annotations greatly simplifies the traditional MVC configuration, and the flexibility and maintainability are greatly improved. .
To implement SpringMVC, the first step must be to enter the corresponding jar package
Then it is with Struts2 Also configure a core controller in Web.xml. Used to intercept requests.
<!-- 配置SpringMVC的请求的Servlet --> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
Do you feel familiar when you see this? It’s just a few more lines than Struts2. init-param is the spring file to be loaded during initialization. If there are multiple files, you can use commas to separate them.
load-on-startup will be loaded immediately at startup.
Then we write a Controller
package com.miya.spring.mvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/Miya") public class MiyaHelloController { @RequestMapping("/hello") public String hello(){ System.out.println("hello Miya"); return "/hello"; } }
##@Controller This annotation does not need to be too many Say, declare a controller.
@RequestMapping is defined on the class to declare a space. Above the method, a request path is declared
Returns a string of the path you want to access. Where is this path?<context:component-scan base-package="com.miya.spring.mvc"/> <!-- 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/views"/> <!-- 后缀 --> <property name="suffix" value=".jsp"/> </bean>In our Spring XML configuration, we first scan all annotations and then configure a view parser. When we return that hello, it is prefix result(hello) suffix to access our view. Now let’s try running it directly in index.jsp. request.getContextPath() gets the root path of your web project, which is webContent (webRoot in MyEclipse). Then you can now see our namespace Miya followed by the method request path hello defined in it. The address we finally visited http://localhost:8080/SpringMvcDemo1/Miya/hello accessed the file WEB-INF/views/hello.jsp. And this request is still a rest style request. When you get here, you will find that it is much more convenient than Struts2. Every configuration request of Struts2 has to go to Struts2 to configure a lot of actions and so on, and sometimes there are thousands of lines in the sturts file. I am too old to find one. The request took half an hour.
Another way we can implement it is to implement the Controller interface provided by Spring and rewrite the methods in the interface.
package com.miya.spring.mvc.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class MiyaWordController implements Controller{ @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("/hello"); return modelAndView; } }
The view can be set in ModelAndView. What I set is hello. Then we need to configure a bean in spring XML, name is the request path, class is the specified controller class
<bean></bean>Note: We intercept all requests in the project, which will cause pictures, styles, and JS to all report 404. We Can be done in spring External resource files are introduced into XML, and this way of implementing interfaces results in too high coupling, and each function needs to be written in a class, which makes our code bloated. Therefore, it is recommended to use annotations. Annotations are currently very popular. And many frameworks support annotation methods, and the syntax is simple, which makes the code concise.
<!-- 引入外部样式 --> <resources></resources>
The above is the detailed content of Detailed process of building SpringMVC environment. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor