Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of getting started with SpringMVC

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points of SpringMVC entry case analysis, the content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Spring MVC is a follow-up product of SpringFrameWork and has been integrated into Spring Web Flow. The Spring framework provides a full-featured MVC module for building Web applications. Use Spring pluggable MVC architecture, so when using Spring for WEB development, you can choose to use Spring's Spring MVC framework or integrate other MVC development frameworks, such as Struts1 (generally not used now), Struts 2 (generally used by old projects), and so on.

Spring is an excellent Web framework based on MVC design concept for the presentation layer, and it is one of the most mainstream MVC frameworks at present. Spring3.0 has since surpassed Struts2 to become the best MVC framework. Through a set of MVC annotations, SpringMVC makes POJO the controller for processing requests without implementing any interfaces.

Then someone is going to question, since SpringMVC is better and better than Struts2 framework, why should we learn Struts2? First of all, it doesn't hurt to learn more, and secondly, some old projects in companies should still use Struts2, so it doesn't make sense if you don't know how to Struts2. Finally, if you have studied Struts2, when you learn SpringMVC, you will be more comfortable with it.

There is no doubt that the first entry project is HelloWorld.

Implementation steps:

Add jar package

Configure DispatcherServlet in web.xml

Join the profile of SpringMVC

Write the processor that processes the request and identify it as the processor

Authoring views

1. Add jar package

two。 Configure DispatcherServlet in web.xml

SpringMVC

SpringDispatcherServlet

Org.springframework.web.servlet.DispatcherServlet

ContextConfigLocation

Classpath:springmvc.xml

one

SpringDispatcherServlet

/

This is a fixed configuration, and it's easy to type this code if your development tool has a Spring plug-in.

Click on the blank space to use the shortcut key, then select dispatcherservlet, and enter.

The init-param tag is configured with initialization parameters, and the parameters that need to be configured are the location and name of the SpringMVC configuration file. After learning Struts2, you will know that load-on-startup is set to 1, that is, the Servlet is created when the web container is loaded, and other parameters are similar to Struts2.

3. Join the profile of SpringMVC

Create a configuration file for SpringMVC under the src directory:

These configurations have already been mentioned in Spring, so I won't repeat them. If you really don't understand, you can read my previous article.

This is the view parser provided by SpringMVC, which parses the return value of the control layer method into the actual physical view. I will elaborate on the view parser in a later article, which will not be dwelt on here.

4. Write the processor that processes the request and identify it as the processor

Package com.itcast.spring.handler

Import org.springframework.stereotype.Controller

Import org.springframework.web.bind.annotation.RequestMapping

@ Controller

Public class HelloWorld {

@ RequestMapping ("/ helloworld")

Public String hello () {

System.out.println ("hello world")

Return "success"

}

}

The class is marked as a controller through @ Controller and is managed by the SpringIOC container.

5. Authoring views

Insert title here

Hello World

At this point, a basic case is completed, and let's analyze the implementation process in detail.

First, we visit index.jsp, which has a hyperlink. By clicking on the hyperlink, we will issue a / helloworld request. At this time, the springDispatcherServlet configured in web.xml will intercept the request, and then the SpringIOC container will match the relevant controller. Because the mapping request path @ RequestMapping ("/ helloworld") is annotated on the hello () method in the HelloWorld class, the request issued by the hyperlink matches this method successfully. The method is executed and the "success" string is returned. The return value is parsed to the actual physical view through the view parser, while the parsing rule of the InternalResourceViewResolver parser is: prefix + returnVal + suffix. That is, the / WEB-INF/views/ we configured in the parser plus the return value "success" + the suffix ".jsp" will point to the physical view (/ WEB-INF/views/success.jsp).

So we create a new views folder under the WEB-INF directory, and then create a new success.jsp file:

Insert title here

Success Page

After this series of processes, when we click on the hyperlink, the page will jump to the success.jsp page. Is that the case or not? The operation will be clear:

For the configuration in the web.xml file, you can also use the default configuration file instead of configuring the init-param initialization parameters. But the default configuration file has some tricks:

Must be placed in the WEB-INF directory

File name must be +-servlet.xml

What do you mean?

The name of the servlet configured here plus-servlet.xml is the name of the configuration file, so in this project, the name of the configuration file should be springDispatcherServlet-servlet.xml, because servlet-name can be defined at will, so the name of the configuration file can also be followed at will.

The above is all the content of this article "getting started with SpringMVC example Analysis". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report