Spring MVC项目快速搭建

一 点睛

Spring MVC提供了一个DispatcherServlet来开发Web项目。

在Servlet 2.5及以下的时候,只要在web.xml下配置元素就可以了。

但在Servlet 3.0下,可以实现无web.xml的配置。

在Spring MVC里实现WebApplicationInitializeer接口便实现等同web.xml的配置。

二 项目说明

基于Maven搭建零配置Spring MVC原型项目。

三 实战

1 构建Maven项目,pom.xml如下:


       4.0.0
       com.wisely
       highlight_springmvc4
       0.0.1-SNAPSHOT
       war
       
              
              1.7
              UTF-8
              UTF-8
              
              2.2
              1.2
              3.1.0
              
              4.1.5.RELEASE
              
              1.0.13
              1.7.5
       
       
              
                     javax
                     javaee-web-api
                     7.0
                     provided
              
              
              
                     org.springframework
                     spring-webmvc
                     ${spring-framework.version}
              
              
              
                     javax.servlet
                     jstl
                     ${jstl.version}
              
              
              
                     javax.servlet
                     javax.servlet-api
                     ${servlet.version}
                     provided
              
              
              
                     javax.servlet.jsp
                     jsp-api
                     ${jsp.version}
                     provided
              
              
              
                     org.springframework
                     spring-tx
                     ${spring-framework.version}
              
              
              
                     org.slf4j
                     slf4j-api
                     ${slf4j.version}
              
              
                     log4j
                     log4j
                     1.2.16
              
              
                     org.slf4j
                     jcl-over-slf4j
                     ${slf4j.version}
              
              
                     ch.qos.logback
                     logback-classic
                     ${logback.version}
              
              
                     ch.qos.logback
                     logback-core
                     ${logback.version}
              
              
                     ch.qos.logback
                     logback-access
                     ${logback.version}
              
       
       
              
                     
                           org.apache.maven.plugins
                           maven-compiler-plugin
                           2.3.2
                           
                                  ${java.version}
                                  ${java.version}
                           
                     
                     
                        org.apache.maven.plugins
                        maven-war-plugin
                        2.3
                        
                            false
                        
                    
              
       

2 日志配置

在src/main/resources目录下,新建logback.xml用来配置日志,内容如下:



       
              true
       
       
       
              
                     logbak: %d{HH:mm:ss.SSS} %logger{36} - %msg%n
              
       
       
       
       
              
       

3 演示页面编写

在src/main/resources下建立view目录,并在此目录下新建index.jsp,内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
       pageEncoding="UTF-8"%>




Insert title here


       
              Welcome to Spring MVC world
       

4  Spring MVC配置

package com.wisely.highlight_springmvc4;

import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import com.wisely.highlight_springmvc4.interceptor.DemoInterceptor;
import com.wisely.highlight_springmvc4.messageconverter.MyMessageConverter;

@Configuration
@EnableWebMvc// 开启一些MVC的默认配置
@ComponentScan("com.wisely.highlight_springmvc4")
//Spring的配置类,这里配置了一个JSP的ViewResolver,用来映射路径和实际页面的位置
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/classes/views/");
        viewResolver.setSuffix(".jsp");
        viewResolver.setViewClass(JstlView.class);
        return viewResolver;
    }
}

5 Web配置

package com.wisely.highlight_springmvc4;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
//WebApplicationInitializer是Spring提供用来配置Servlet3.0+配置的接口,从而实现
//替代web.xml的位置。实现此接口将会自动被SpringServletContainerInitializer(用来启动Servlet3.0容器)
//获取到。
public class WebInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext)
            throws ServletException {
        //新建WebApplicationContex
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        //注册配置类
        ctx.register(MyMvcConfig.class);
        //将其和当前servletContext关联。
        ctx.setServletContext(servletContext); 
        //注册Spring MVC的DispatcherServlet
        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); 
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
    }

}

6 编写简单控制器

package com.wisely.highlight_springmvc4.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller//声明是一个控制器
public class HelloController {
    
    @RequestMapping("/index")//配置URL和方法之间的映射
    public  String hello(){
        //通过ViewResolver的Bean的配置,返回值为index,说明页面放置的位置
        //为WEB-INF/classes/views/index.jsp
        return "index";
    }

}

Spring MVC项目快速搭建_第1张图片

7 运行

将程序部署到Tomcat,启动Tomcat,并访问http://localhost:8080/highlight_springmvc4/index

Spring MVC项目快速搭建_第2张图片

你可能感兴趣的:(Spring,MVC)