-
也许有人会问为什么,上次发了一个SpringMVC教程案例,但现在怎么又发一个注解版的?
当然出这个教程当然是有意义的,大家可以想想一想你在写Springmvc的时候是不是会写着写着去找命名空间,但是你用现在的注解开发你会发现简单易懂,而且开发效率会快一点。不多说下面直接上手教程。
-
我们来回顾什么是SpringMVC?
SpringMVC就是一个Spring内置的MVC框架。MVC框架,它解决WEB开发中常见的问题(参数接收、文件上传、表单验证、国际化等等),而且使用简单,与Spring无缝集成。还支持 RESTful风格的URL请求。采用了松散耦合可插拔组件结构,比其他 MVC 框架更具扩展性和灵活性。
1.导入需要的Jar
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.19</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
2.创建一个ApplicationContext的config文件
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Import;
@Configurable
@Import({SpringmvcConfig.class})
public class ApplicationContextConfig {
}
3.创建一个Springmvc的config文件
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configurable
@Controller
@ComponentScan("com.suncode.controller")
@EnableWebMvc
public class SpringmvcConfig {
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
4.创建一个Servlet的config文件,这个就类似于web你们配置的一些设置
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import javax.servlet.Filter;
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringmvcConfig.class};
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
return new Filter[]{characterEncodingFilter};
}
}
5.我们为了测试能不能跳转页面在WEB-INF下面建一个Hello.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>再见</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap-theme.min.css" integrity="sha384-6pzBo3FDv/PJ8r2KRkGHifhEocL+1X2rVCTTkUfGk7/0pbek5mMa1upzvWbrUbOZ" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
</head>
<body>
<div class="media" style="padding-top: 20px;padding-left: 20px;">
<div class="media-left media-middle">
<a href="#">
<img class="media-object" src="https://5fou.com/i/2022/06/01/x2qz6p.jpg"
style="width: 80px;height: 80px;">
</a>
</div>
<div class="media-body">
<h4 class="media-heading"><font style="font-weight:bold;">SunnyBoy</font></h4>
${likeMe}
</div>
</div>
</body>
</html>
6.启动服务器,看看测试效果