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!

Java is platform-independent because of its "write once, run everywhere" design philosophy, which relies on Java virtual machines (JVMs) and bytecode. 1) Java code is compiled into bytecode, interpreted by the JVM or compiled on the fly locally. 2) Pay attention to library dependencies, performance differences and environment configuration. 3) Using standard libraries, cross-platform testing and version management is the best practice to ensure platform independence.

Java'splatformindependenceisnotsimple;itinvolvescomplexities.1)JVMcompatibilitymustbeensuredacrossplatforms.2)Nativelibrariesandsystemcallsneedcarefulhandling.3)Dependenciesandlibrariesrequirecross-platformcompatibility.4)Performanceoptimizationacros

Java'splatformindependencebenefitswebapplicationsbyallowingcodetorunonanysystemwithaJVM,simplifyingdeploymentandscaling.Itenables:1)easydeploymentacrossdifferentservers,2)seamlessscalingacrosscloudplatforms,and3)consistentdevelopmenttodeploymentproce

TheJVMistheruntimeenvironmentforexecutingJavabytecode,crucialforJava's"writeonce,runanywhere"capability.Itmanagesmemory,executesthreads,andensuressecurity,makingitessentialforJavadeveloperstounderstandforefficientandrobustapplicationdevelop

Javaremainsatopchoicefordevelopersduetoitsplatformindependence,object-orienteddesign,strongtyping,automaticmemorymanagement,andcomprehensivestandardlibrary.ThesefeaturesmakeJavaversatileandpowerful,suitableforawiderangeofapplications,despitesomechall

Java'splatformindependencemeansdeveloperscanwritecodeonceandrunitonanydevicewithoutrecompiling.ThisisachievedthroughtheJavaVirtualMachine(JVM),whichtranslatesbytecodeintomachine-specificinstructions,allowinguniversalcompatibilityacrossplatforms.Howev

To set up the JVM, you need to follow the following steps: 1) Download and install the JDK, 2) Set environment variables, 3) Verify the installation, 4) Set the IDE, 5) Test the runner program. Setting up a JVM is not just about making it work, it also involves optimizing memory allocation, garbage collection, performance tuning, and error handling to ensure optimal operation.

ToensureJavaplatformindependence,followthesesteps:1)CompileandrunyourapplicationonmultipleplatformsusingdifferentOSandJVMversions.2)UtilizeCI/CDpipelineslikeJenkinsorGitHubActionsforautomatedcross-platformtesting.3)Usecross-platformtestingframeworkss


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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.

SublimeText3 English version
Recommended: Win version, supports code prompts!
