Home >Java >javaTutorial >Detailed process of building SpringMVC environment

Detailed process of building SpringMVC environment

不言
不言Original
2018-09-26 14:45:023052browse

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. 66e1775cbd9d5002635ae3285442ba88/3ec4a5583206d351b61ed79c1a0f9c66Intercept all requests. (At the same time, css and js will also be intercepted);

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.

0c02004446d7f3f85c79ae2f5dedf06e

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

32c97008f0b4c7cbcba6489f3ba8eac34bb0e59fd50cbfe6f6ce2215b9d94243
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.

ff199eef702ba3861ff15c24816f4ceb
2cecf299d6f56fb7e0ba4470456908f305ca0f956480d4ad236391e89e886bd6

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn