Detailed process of building SpringMVC environment
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>
<% response.sendRedirect(request.getContextPath() + "/Miya/hello"); %>
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 name="/Miya/word" class="com.miya.spring.mvc.controller.MiyaWordController"></bean>
<!-- 引入外部样式 --> <mvc:resources location="/css/" mapping="/css/**"></mvc: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!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



SpringBoot and SpringMVC are both commonly used frameworks in Java development, but there are some obvious differences between them. This article will explore the features and uses of these two frameworks and compare their differences. First, let's learn about SpringBoot. SpringBoot was developed by the Pivotal team to simplify the creation and deployment of applications based on the Spring framework. It provides a fast, lightweight way to build stand-alone, executable

What is the difference between SpringBoot and SpringMVC? SpringBoot and SpringMVC are two very popular Java development frameworks for building web applications. Although they are often used separately, the differences between them are obvious. First of all, SpringBoot can be regarded as an extension or enhanced version of the Spring framework. It is designed to simplify the initialization and configuration process of Spring applications to help developers

SpringBoot and SpringMVC are two frameworks commonly used in Java development. They are both provided by the Spring framework, but they have some differences in functions and usage methods. This article will introduce the characteristics and differences of SpringBoot and SpringMVC respectively. 1. Features of SpringBoot: Simplified configuration: SpringBoot greatly simplifies the project configuration process through the principle of convention over configuration. It can automatically configure the parameters required by the project, and developers

The difference between spring and springmvc: 1. Positioning and functions; 2. Core functions; 3. Application areas; 4. Extensibility. Detailed introduction: 1. Positioning and functions. Spring is a comprehensive application development framework that provides dependency injection, aspect-oriented programming, transaction management and other functions. It is designed to simplify the development of enterprise-level applications, and Spring MVC is the Spring framework. A module in it is used for the development of Web applications and implements the MVC pattern; 2. Core functions and so on.

The differences between springboot and springmvc are: 1. Different meanings; 2. Different configurations; 3. Different dependencies; 4. Different development times; 5. Different productivity; 6. Different ways to implement JAR packaging function; 7. Whether batch processing is provided Function; 8. Different functions; 9. Different community and documentation support; 10. Whether deployment descriptors are required.

With the development of the Internet, Web services are becoming more and more common. As an application programming interface, JavaAPI is constantly launching new versions to adapt to different application scenarios. As a popular open source framework, SpringMVC can help us easily build web applications. This article will explain in detail how to use SpringMVC for Web service processing in JavaAPI development, including configuring SpringMVC, writing controllers, and using

Analyzing the similarities and differences between SpringBoot and SpringMVC SpringBoot and SpringMVC are very important development frameworks in the Java field. Although they are both part of the Spring framework, there are some obvious differences in usage and functionality. This article will compare SpringBoot and SpringMVC and analyze the similarities and differences between them. First, let's learn about SpringBoot. SpringBo

The role of interceptor SpringMVC's interceptor is similar to the filter in Servlet development, which is used to pre-process and post-process the processor. Interceptors are connected into a chain in a certain order, and this chain is called an interceptor chain (InterceptorChain). When an intercepted method or field is accessed, the interceptors in the interceptor chain will be called in the order they were previously defined. Interceptors are also the specific implementation of AOP ideas. The difference between interceptors and filters: Filter (Filter) The scope of use of interceptor (Intercepter) is part of the servlet specification and can be used by any JavaWeb project. Spri
