预备


按照 ​​javascript:void(0)​​ 搭建简单 webapp

预备做完,再进行以下步骤


步骤


(一)添加Spring MVC 支持

项目名 -> 右键 -> Add Framework Support

【SpringMVC】之 IDEA 搭建SpringMVC(无web.xml)_spring


【SpringMVC】之 IDEA 搭建SpringMVC(无web.xml)_spring mvc_02


(二) 删除web.xml

目录结构

【SpringMVC】之 IDEA 搭建SpringMVC(无web.xml)_mvc_03


(三)配置pom.xml

下面是凡凡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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.donaldy</groupId>
<artifactId>SpringMVC4</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringMVC4 Maven Webapp</name>
<url>http://maven.apache.org</url>

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

<!-- Other Web dependencies -->
<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>

<!-- Logging with 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>
</dependencies>


<build>
<finalName>SpringMVC4</finalName>

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

配置完,注意要用maven update下哦。


(四)配置logback.xml (可选)

配置logback.xml(在控制台打印日志),开发Spring MVC经常出现和参数类型相关的4XX错误,设置此项会看到更详细的错误信息。

【SpringMVC】之 IDEA 搭建SpringMVC(无web.xml)_web.xml_04

<?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>

<jmxConfiguratior

<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>logback: %d{HH:mm:ss.SSS} %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.springframework.web" level="DEBUG"

<root level="info">
<appender-ref ref="console"
</root>

</configuration>


(五)Web配置

取代web.xml

【SpringMVC】之 IDEA 搭建SpringMVC(无web.xml)_spring mvc_05

/**
* Created by DonaldY on 2017/8/3.
*/
public class WebInitializer implements WebApplicationInitializer

public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(MyMvcConfig.class);
ctx.setServletContext(servletContext);

Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}


(六)Spring MVC 配置

普通的Spring 配置类

【SpringMVC】之 IDEA 搭建SpringMVC(无web.xml)_spring mvc_06

/**
* Created by DonaldY on 2017/8/3.
*/
@Configuration
@EnableWebMvc
@ComponentScan("cn.donaldy.highlight")
public class MyMvcConfig
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/classes/views/");
viewResolver.setSuffix(".jsp");
viewResolver.setViewClass(JstlView.class);
return


(七)配置Controller

简单控制类

【SpringMVC】之 IDEA 搭建SpringMVC(无web.xml)_spring_07

/**
* Created by DonaldY on 2017/8/3.
*/
@Controller
public class HelloController

@RequestMapping("/hello")
public String hello() {

return "hello";
}

}


(八)配置视图

简单视图

【SpringMVC】之 IDEA 搭建SpringMVC(无web.xml)_mvc_08

<%@ page contentType="text/html;charset=UTF-8" language="java"
<html>
<head>
<title>Title</title>
</head>
<body>

<h2>Welcome to Spring MVC world. </h2>
<h3>Hello, DonaldY.</h3>

</body>
</html>


(九)检验

【SpringMVC】之 IDEA 搭建SpringMVC(无web.xml)_idea_09


疑问

日志中还有不能理解的信息,再此记录下

【SpringMVC】之 IDEA 搭建SpringMVC(无web.xml)_mvc_10