- Springmvc提供了一个DispatcherServlet来开发web应用
- 这里采用Sevlet3.0+无web.xml的配置方式,实现WebApplicationInitializer接口替代web.xml
- maven构建零配置的springmvc原型项目
1.构建maven项目
pom.xml:
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zgw</groupId> <artifactId>springmvc_withoutwebxml</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <!-- Generic properties --> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- Web --> <jsp.version>2.2</jsp.version> <jstl.version>1.2</jstl.version> <servlet.version>3.1.0</servlet.version> <!-- Spring --> <spring-framework.version>4.1.5.RELEASE</spring-framework.version> <!-- Logging --> <logback.version>1.0.13</logback.version> <slf4j.version>1.7.5</slf4j.version> </properties> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-framework.version}</version> </dependency> <!-- 其他web依赖 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>${jsp.version}</version> <scope>provided</scope> </dependency> <!-- Spring and Transactions --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring-framework.version}</version> </dependency> <!-- 使用SLF4J和LogBack作为日志 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>${logback.version}</version> </dependency> <!--对json和xml格式的支持 --> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.5.3</version> </dependency> <!-- file upload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!-- 非必需,可简化IO操作 --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring-framework.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>
2.日志的配置,在src/main/resources下新建logback.xml:
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="1 seconds"> <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"> <resetJUL>true</resetJUL> </contextListener> <jmxConfigurator/> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>logbak: %d{HH:mm:ss.SSS} %logger{36} - %msg%n</pattern> </encoder> </appender> <logger name="org.springframework.web" level="DEBUG"/> <!-- 设置为bug,便于开发中 调试4XX错误--> <root level="info"> <appender-ref ref="console"/> </root> </configuration>
3.演示页面,在src/main/resources下建立views目录,新建index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <pre> 这是一个演示页面 </pre> </body> </html>
注意:页面放置在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 } }
运行结果:
相关推荐
在工作当中,我们有时需要创建一个全新的工程,而基于spring-mvc web的工程较为常见,这篇文章主要介绍了IntelliJ IDEA maven 构建简单springmvc项目(图文教程),感兴趣的小伙伴们可以参考一下
SpringMVC精品资源--一个简单的论坛项目,开发IDE为IntelliJ IDEA,通过Maven构建,后台采
一个初学者练手项目,开发IDE为IntelliJ IDEA,通过Maven构建,后台采用Spring、SpringMVC、MyBatis,数据库采用MySQL、Redis,前台用到jQuery,图片被上传到七牛云。
这个项目是一个简单的教务查询系统,该练手小项目希望能帮助到大家,熟悉SSM的整合开发 使用技术 IOC容器:Spring Web框架:SpringMVC ORM框架:Mybatis 安全框架:Shiro 数据源:C3P0 日志:log4j 前端框架...
本人博客里面“使用Maven构建Spring MVC项目的简单示例”的源代码
本项目是一个整合 **SpringMVC+Spring+MyBatis(SSM)** 框架的 **Demo**。 拥有高效率便捷开发模式,使开发人员更专注于业务,达到面向业务开发。 项目使用 **Maven** 构建,便于项目管理,支持 **Oracle、...
刚好最近要做一个项目,所以把这个集成环境继续完善了一下。采用Maven构建的Spring+SpringMVC+MyBatis+Shiro的集成开发环境。加入了Mybatis PageHelper分页组件,并抽象出了BaseService层。另外加入了Shrio完成登录...
eclipse开发工具,运用maven构建的springmvc简单入门级demo,完整的代码。有需要的朋友可以看一看哈。搭建过程中还是碰到很多问题,如果项目不能运行,请发私信给我,一定详细为你解答哈。
SpringMVC+Hibernate框架整合,构建一个简单项目-附件资源
使用SpringMVC构建的一个简单的网站,采用了JavaScript+SpringMVC+MySql的基本架构,可以使你在一个项目中了解SpringMVC基础、JavaScript基本编程、和基本的MySql操作。
通过Meavn构建项目,创建简单的SpringMVC项目。实现helloworld功能