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!