想起之前提起来springmvc
就是xml
配置文件,但是最近看了一些书籍发现spring
已经支持类的方式了,便一边学习,一边记录
其中使用的开发工具是idea
1 SpringMVC无XML项目
1.1 SpringMVC项目预览
不是使用springboot
的,因此搭建完毕后需要使用tomcat
来启动项目
1.2 pom.xml
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.9.RELEASE</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>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 使用slf4j和logback作为日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</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>1.7.25</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>1.2.3</version>
</dependency>
1.3 配置SpringMVC
之前配置SpringMVC
都是使用的springmvc.xml
文件,然后在webxml
中引用即可
使用配置类的方式来配置类似于springmvc.xml
中的文件
package cn.jzh.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("cn.jzh")
public class MyMvcConfig {
/**
* 此处相当于web项目中 springmvc.xml文件
*/
@Bean
public InternalResourceViewResolver viewResolver (){
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
viewResolver.setViewClass(JstlView.class);
return viewResolver;
}
}
1.4 配置Web
之前配置web
文件都是在webapp\WEB-INF\web.xml
中的,现在不用xml
文件那么使用配置类如下:
package cn.jzh.config;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
public class WebInitializer implements WebApplicationInitializer {
/**
* 此处相当于 web项目中 web.xml文件
*/
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(MyMvcConfig.class);
context.setServletContext(servletContext);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
dispatcher.addMapping("/");
dispatcher.setLoadOnStartup(1);
}
}
1.5 简单控制层
package cn.jzh.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class JspController {
private final static Logger log = LoggerFactory.getLogger(JspController.class);
@RequestMapping(value="/index")
public String hello(){
System.out.println("=======================");
log.info("jsp的demo日志。。。。。。。。。。。");
System.out.println("=======================");
return "index";
}
}
注意:
不要添加@ResponseBody
注解,否则页面不会跳转而是在浏览器上显示一个index
的字符串
1.6 配置日志
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="5 seconds">
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<restJUL>true</restJUL>
</contextListener>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-4relative %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="console" />
</root>
</configuration>
此处的日志配置文件可能不太明白,推荐日志参考文件:Logback文件详解
1.7 运行测试
其中两个index.xml
文件就不在此放了,就是为了验证能否跳转到WEB-INF
中的文件,没有具体逻辑和代码
直接放在tomcat
中就可以运行测试
网友评论