Do you know the most popular SpringMVC framework? How to build it?

Do you know the most popular SpringMVC framework? How to build it?
Spring MVC is a web member of the Spring family. It is a Java-based, request-driven lightweight web framework that implements the design ideas of Web MVC. That is, it uses the idea of MVC architecture pattern to solve the responsibilities of the web layer. Coupling, request-driven refers to the use of request-response model, the purpose of the framework is to help us simplify development, Spring MVC is also to simplify our daily Web development.

Spring MVC is the realization of the idea of ​​serving to workers. The front controller is DispatcherServlet; the application controller is disassembled into Handler Mapping for processor management and View Resolver for view management; supports localization/internationalization (Locale) resolution and file upload, etc.; Provides a very flexible data verification, formatting and data binding mechanism; provides a powerful contract programming support that is greater than configuration (convention first principle).

How SpringMVC is built

  1. Development environment setup
  2. New Maven webApp
  3. Springmvc environment jar package dependency
  4. Configure web.xml (front controller configuration)
  5. servlet-context.xml configuration
  6. Writing the page controller
  7. Add view page
  8. Start jetty server

Case practice

Development environment setup

Eclipse + jdk1.7 + maven + Jetty

New Maven webApp

Establish the springmvc01 project and adjust the web environment.

Springmvc environment jar package dependency

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xxx</groupId>
    <artifactId>springmvc01</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>springmvc01 Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- spring web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <!-- spring mvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <!-- web servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
        </dependency>
    </dependencies>
    <!-- jetty 插件 -->
    <build>
    <finalName>springmvc01</finalName>
    <resources>
    <resource>
    <directory>src/main/resources</directory>
    </resource>
    </resources>
    <plugins>
        <!-- 编译环境插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
            <source>1.7</source>
            <target>1.7</target>
            <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>6.1.25</version>
            <configuration>
            <scanIntervalSeconds>10</scanIntervalSeconds>
            <contextPath>/springmvc01</contextPath>
            </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Configure web.xml (front controller configuration)

<?xml version="1.0" encoding="UTF-8"?> 

<web-app id="WebApp_ID" version="3.0" 

    xmlns="http://java.sun.com/xml/ns/javaee"  

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  

    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  

    <!-- 表示容器启动时 加载上下文配置 这里指定 spring 相关配置 --> 

    <context-param> 

        <param-name>contextConfigLocation</param-name> 

        <param-value>classpath:*.xml</param-value> 

    </context-param> 


    <!-- 启用 spring 容器环境上下文监听 --> 

    <listener> 

    	<listener class>org.springframework.web.context.ContextLoaderListener</listener-class> 

    </listener> 

    <!-- 编码过滤 utf-8 --> 

    <filter> 

        <description>char encoding filter</description> 

        <filter-name>encodingFilter</filter-name> 

        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 

        <init-param> 

            <param-name>encoding</param-name> 

            <param-value>UTF-8</param-value> 

        </init-param> 

    </filter> 

    <filter-mapping> 

        <filter-name>encodingFilter</filter-name> 

        <url-pattern>/*</url-pattern> 

    </filter-mapping> 

    <!-- servlet 请求分发器 -->  

    <servlet> 

        <servlet-name>springMvc</servlet-name> 

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 

        <init-param> 

            <param-name>contextConfigLocation</param-name> 

            <param-value>classpath:servlet-context.xml</param-value> 

        </init-param> 

        <!-- 表示启动容器时初始化该 Servlet --> 

        <load-on-startup>1</load-on-startup> 

    </servlet> 

    <servlet-mapping> 

        <servlet-name>springMvc</servlet-name> 

        <!-- 这是拦截请求, /代表拦截所有请求,拦截所有.do 请求 --> 

        <url-pattern>/</url-pattern> 

    </servlet-mapping> 

</web-app>  

In order to start our springMvc environment, the configuration of the mvc framework has not yet been carried out. The servlet-context.xml file is referenced in web.xml above.

servlet-context.xml configuration

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="  
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc.xsd  
        http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context.xsd 
        http://www.springframework.org/schema/aop  
        http://www.springframework.org/schema/aop/spring-aop.xsd 
        http://www.springframework.org/schema/tx  
        http://www.springframework.org/schema/tx/spring-tx.xsd">  

    <!-- 扫描 com.xxx.controller 下包 --> 

    <context:component-scan base-package="com.xxx.controller" />  

    <!-- mvc 请求映射 处理器与适配器配置--> 

    <mvc:annotation-driven/>  

    <!--配置视图解析器 默认的视图解析器- --> 

    <bean id="defaultViewResolver" 

        class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 

        <property name="viewClass"  

        value="org.springframework.web.servlet.view.JstlView" /> 

        <property name="contentType" value="text/html" /> 

        <property name="prefix" value="/WEB-INF/jsp/" /> 

        <property name="suffix" value=".jsp" /> 

    </bean> 

</beans> 

If it returns garbled characters: configure the message converter

<!-- 消息转换器 --> 

<mvc:message-converters register-defaults="true"> 

    <bean class="org.springframework.http.converter.StringHttpMessageConverter"> 

   	 	<property name="supportedMediaTypes" value="text/html;charset=UTF-8"/> 

    </bean> 

</mvc:message-converters> 

Writing the page controller

/** 

* 采用注解扫描形式 

*/ 

@Controller 

public class HelloController {
    
     

    /** 

    * 请求映射地址 /hello

    * @return 

    */ 

    @RequestMapping("/hello") 

    public ModelAndView hello(){
    
     

        ModelAndView mv=new ModelAndView();  

        mv.addObject("hello", "hello spring mvc"); 

        mv.setViewName("hello"); 

        return mv;  

    } 

} 

Add view page

Create a new jsp folder under WEB-INF, and create a new hello.jsp under File Plus

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 

<% 
String path = request.getContextPath(); 
String basePath =  
request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 

<html> 

    <head> 

        <base href="<%=basePath%>"> 

        <title>My JSP 'hello.jsp' starting page</title> 

        <meta http-equiv="pragma" content="no-cache">

        <meta http-equiv="cache-control" content="no-cache">

        <meta http-equiv="expires" content="0">

        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

        <meta http-equiv="description" content="This is my page">

    </head>  

    <body> 

    <!-- el 表达式接收参数值 --> 

    	${hello} 

    </body> 

</html> 

Start jetty server

Right-click the project → run as → maven build → goals and enter jetty:run in the input box to start the server.

If there is a successful start, the browser ( preferably with strong chrome or Firefox browser, the programmer's favorite !!! ) access address http: // localhost: 8080 / springmvc01 / hello

Expand

What can Spring MVC do for us

  1. Let us design a clean web layer very simply;

  2. Develop a more concise Web layer;

  3. Naturally integrated with Spring framework (such as IoC container, AOP, etc.);

  4. Provide powerful contract programming support that is greater than configuration;

  5. Able to easily perform unit testing of the Web layer;

  6. Support flexible URL to page controller mapping;

  7. Very easy to integrate with other view technologies, such as Velocity, FreeMarker, etc., because the model data is not placed in a specific API, but in a Model (Map data structure is implemented, so it is easy to be used by other frameworks);

  8. Very flexible data verification, formatting and data binding mechanism, can use any object for data binding, without having to implement specific framework API;

  9. Support flexible localization and other analysis;

  10. Simpler exception handling;

  11. Support for static resources;

  12. Support Restful style.

Guess you like

Origin blog.csdn.net/dirft_lez/article/details/109001019