springmvc简单构建一个项目(一)

阅读更多
  • Springmvc提供了一个DispatcherServlet来开发web应用
  • 这里采用Sevlet3.0+无web.xml的配置方式,实现WebApplicationInitializer接口替代web.xml
  • maven构建零配置的springmvc原型项目

1.构建maven项目

   pom.xml:


  4.0.0
  com.zgw
  springmvc_withoutwebxml
  0.0.1-SNAPSHOT
  war

  
		
		1.8
		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}
		

		
		
			com.fasterxml.jackson.dataformat
			jackson-dataformat-xml
			2.5.3
		

		
		
			commons-fileupload
			commons-fileupload
			1.3.1
		
		
		
			commons-io
			commons-io
			2.3
		

		
			org.springframework
			spring-test
			${spring-framework.version}
			test
		
		
	
		
			junit
			junit
			4.11
			test
		

	

	
		
			
				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下建立views目录,新建index.jsp

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




Insert title here


	
		这是一个演示页面
	

 注意:页面放置在src/main/resources下,这是springboot的页面放置方式

 

4.spring mvc的配置

package com.zgw.springmvc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;



@Configuration
@EnableWebMvc  //开启springmvc的支持
@ComponentScan("com.zgw.springmvc")
public class MySpringmvcConfig extends WebMvcConfigurerAdapter {
	
	@Bean
	public InternalResourceViewResolver viewResolver() {
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		//开发时页面的放置方式和运行时不同,主要是因为我们看到的页面效果是运行时的,
		//它将会自动编译到/WEB-INF/classes/views/下
		viewResolver.setPrefix("/WEB-INF/classes/views/");
		viewResolver.setSuffix(".jsp");
		viewResolver.setViewClass(JstlView.class);
		return viewResolver;
	}

}

 

5.web配置,替代web.xml

package com.zgw.springmvc;

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接口,将会自动被SpringServletContainerInitializer
 *(用来启动servlet3.0容器)获取到
 * @author zan
 *
 */
public class WebXmlConfig implements WebApplicationInitializer {

	@Override
	public void onStartup(ServletContext servletContext)
			throws ServletException {
		//新建WebApplicationContext,注册配置类MySpringmvcConfig,并和当前的servletContext关联
		AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(MySpringmvcConfig.class);
        ctx.setServletContext(servletContext); 
        
        //注册springmvc的DispatcherServlet
        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); //3
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);

	}

}

 

6.控制器

 

package com.zgw.springmvc;

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

@Controller
public class HelloController {
	
	@RequestMapping("/index")
	public  String hello(){
		
		return "index";
		//通过ViewResolver的Bean配置,返回index.说明页面放置路径为/WEB-INF/classes/views/index.jsp
	}

}

 

运行结果:

 




 

 
 

 

  • springmvc_withoutwebxml.rar (14 KB)
  • 下载次数: 0

你可能感兴趣的:(springmvc,java)