文章目录
一、学习目标 二、基于java配置方式使用Spring MVC (一)创建Maven项目 (二)添加相关依赖 (三)创建日志属性文件 (四)创建首页文件 (五)创建Spring MVC配置类 (六)创建Web应用初始化配置类 (七)创建演示控制器 (八)配置Tomcat服务器 (九)启动服务器,查看效果
一、学习目标
二、基于java配置方式使用Spring MVC
(一)创建Maven项目
(二)添加相关依赖
<?xml version="1.0" encoding="UTF-8"?>
< 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> net.huawei.spring</ groupId>
< artifactId> SpringMvcDemo02</ artifactId>
< version> 1.0-SNAPSHOT</ version>
< properties>
< spring.version> 5.3.25</ spring.version>
</ properties>
< dependencies>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-core</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-beans</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-context</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-test</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-jdbc</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 5.1.49</ version>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> druid</ artifactId>
< version> 1.1.24</ version>
</ dependency>
< dependency>
< groupId> log4j</ groupId>
< artifactId> log4j</ artifactId>
< version> 1.2.17</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-aop</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> aspectj</ groupId>
< artifactId> aspectjrt</ artifactId>
< version> 1.5.4</ version>
</ dependency>
< dependency>
< groupId> org.aspectj</ groupId>
< artifactId> aspectjweaver</ artifactId>
< version> 1.9.6</ version>
< scope> runtime</ scope>
</ dependency>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.13</ version>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-web</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-webmvc</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> javax.servlet</ groupId>
< artifactId> jstl</ artifactId>
< version> 1.2</ version>
</ dependency>
< dependency>
< groupId> javax.servlet</ groupId>
< artifactId> javax.servlet-api</ artifactId>
< version> 3.1.0</ version>
< scope> provided</ scope>
</ dependency>
< dependency>
< groupId> com.fasterxml.jackson.core</ groupId>
< artifactId> jackson-core</ artifactId>
< version> 2.9.7</ version>
</ dependency>
< dependency>
< groupId> com.fasterxml.jackson.core</ groupId>
< artifactId> jackson-databind</ artifactId>
< version> 2.9.7</ version>
</ dependency>
< dependency>
< groupId> com.fasterxml.jackson.core</ groupId>
< artifactId> jackson-annotations</ artifactId>
< version> 2.9.7</ version>
</ dependency>
< dependency>
< groupId> com.fasterxml.jackson.dataformat</ groupId>
< artifactId> jackson-dataformat-xml</ artifactId>
< version> 2.5.3</ version>
</ dependency>
</ dependencies>
</ project>
(三)创建日志属性文件
log4j.rootLogger=WARN, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/springmvc.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
(四)创建首页文件
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
< html>
< head>
< title> 首页</ title>
</ head>
< body>
< h1 style = " text-align : center" > Welcome to Spring MVC World~</ h1>
< h3 style = " text-align : center" > <%= new Date() %></ h3>
</ body>
</ html>
(五)创建Spring MVC配置类
package net. huawei. spring. config ;
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. view. InternalResourceViewResolver ;
import org. springframework. web. servlet. view. JstlView ;
@Configuration
@EnableWebMvc
@ComponentScan ( "net.huawei.spring" )
public class SpringMvcConfig {
@Bean
public InternalResourceViewResolver viewResolver ( ) {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver ( ) ;
viewResolver. setViewClass ( JstlView . class ) ;
viewResolver. setPrefix ( "/WEB-INF/classes/templates/" ) ;
viewResolver. setSuffix ( ".jsp" ) ;
return viewResolver;
}
}
(六)创建Web应用初始化配置类
package net. huawei. spring. config ;
import org. springframework. web. WebApplicationInitializer ;
import org. springframework. web. context. support. AnnotationConfigWebApplicationContext ;
import org. springframework. web. servlet. DispatcherServlet ;
import javax. servlet. ServletContext ;
import javax. servlet. ServletException ;
import javax. servlet. ServletRegistration ;
public class MyWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup ( ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext ( ) ;
context. register ( SpringMvcConfig . class ) ;
context. setServletContext ( servletContext) ;
ServletRegistration. Dynamic servlet = servletContext. addServlet ( "dispatcher" , new DispatcherServlet ( context) ) ;
servlet. addMapping ( "/" ) ;
servlet. setLoadOnStartup ( 1 ) ;
}
}
(七)创建演示控制器
package net. huawei. spring. controller ;
import org. springframework. stereotype. Controller ;
import org. springframework. web. bind. annotation. GetMapping ;
@Controller
public class DemoController {
@GetMapping ( "/index" )
public String index ( ) {
return "index" ;
}
}
(八)配置Tomcat服务器
(九)启动服务器,查看效果