新建SpringConfig类
package org.hx.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
/*
* 这个配置类的作用相当于applicationContext.xml*/
@Configuration
@ComponentScan(basePackages = "org.hx",useDefaultFilters = true,excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class)})
public class SpringConfig {
}
新建SpringMVCConfig类
package org.hx.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonInputMessage;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.text.SimpleDateFormat;
import java.util.List;
/*
* 这个配置类相当于spring-servlet.xml*/
@Configuration
@ComponentScan(basePackages = "org.hx")
public class SpringMVCConfig extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/");
}
@Override
protected void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/jsp/",".jsp");
}
@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
ObjectMapper om = new ObjectMapper();
om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(om);
converters.add(converter);
}
}
Spring容器和SpringMVC容器的关系是父子容器的关系。Spring容器是父容器,SpringMVC容器是子容器。在子容器里可以访问父容器里的对象,但是在父容器里不可以访问子容器的对象,说的通俗点就是,在controller里可以访问service对象,但是在service里不可以访问controller对象。所以在SpringConfig类中配置了不扫描controller。
新建一个WebInit类,这个类相当于web.xml文件,加载springmvc的配置文件,添加DispatcherServlet
package org.hx.config;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
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;
/*
* 这个类的作用相当于web.xml配置文件*/
public class WebInit implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
//加载SpringMVC的配置文件
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMVCConfig.class);
//添加DispatcherServlet
ServletRegistration.Dynamic springmvc =servletContext.addServlet("springmvc",new DispatcherServlet(ctx));
//给DispatcherServlet添加路径映射
springmvc.addMapping("/");
//给DispatcherServlet添加启动时机
springmvc.setLoadOnStartup(1);
}
}
新建User类
package org.hx.model;
import java.util.Date;
public class User {
private String name;
private Date birthday;
public User() {
}
public User(String name, Date birthday) {
this.name = name;
this.birthday = birthday;
}
public void setName(String name) {
this.name = name;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public Date getBirthday() {
return birthday;
}
}
新建HelloController类
package org.hx;
import org.hx.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Controller
public class HelloController {
@GetMapping("/hello")
@ResponseBody
public String hello(){
return "hello javassm";
}
@GetMapping("/hello2")
public String hello2()
{
return "hello";
}
@GetMapping("/hello4")
@ResponseBody
public List<User> getName()
{
List<User> names = new ArrayList<User>();
for(int i=0;i<10;i++)
{
names.add(new User("hx:"+i,new Date()));
}
return names;
}
}
在webapp文件目录下新建jsp文件夹,创建hello.jsp文件
<%--
Created by IntelliJ IDEA.
User: 64557
Date: 2021/2/22
Time: 11:35
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>hello.jsp</h1>
</body>
</html>
把例子部署到tomcat运行即可。