springmvc整合jsp
1.webapp 目录下创建 WEB-INF/jsp 存放jsp
2.控制类
package com.painter.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Author: Painter
* @project_name: SpringMVC
* @system_login: sunshine
* @time: 2022/10/1117:00
*/
@Controller
@RequestMapping("/jsp")
public class JspController {
/**
* 没有添加@ResponseBody 注解时
*/
@RequestMapping("/jsp")
public String painterJsp(){
return "test"; // 自动到配置的目录下指定的文件名test.jsp
}
}
3.配置类
package com.painter.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.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
/**
* @Author: Painter
* @project_name: SpringMVC
* @system_login: sunshine
* @time: 2022/10/916:00
*/
/**
* @Configuration 等同于创建以个xml配置文件
* @ComponentScan("com.painter.controller") 将com.painter.controller包下的所有类注入到ioc容器中
*
* 在springmvc原理 所有请求过来先达到我们的 DispatcherServlet 分发具体控制类 方法执行
*/
@Configuration
@ComponentScan("com.painter.controller")
@EnableWebMvc
public class SpringMVCConfig implements WebMvcConfigurer { // 配置类
/**
* 1.@Configuration 定义SpringMVCConfig.xml配置文件
* 2.需要将我们的控制类注入到ioc容器 @ComponentScan("com.mayikt.controller")
* *@ComponentScan("com.mayikt.controller")将该包下所有的类 注入到IOC容器种
* 3.在springmvc原理 所有请求过来先达到我们的 DispatcherServlet 分发具体控制类 方法执行
*
*
* @Configuration
* SpringMVCConfig.java @Configuration
* springmvc.xml=== SpringMVCConfig.java
* -->
*/
//WebMvcConfigurer
@Bean
public InternalResourceViewResolver resourceViewResolver() {
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
//请求视图文件的前缀地址
internalResourceViewResolver.setPrefix("/WEB-INF/jsp/");
//请求视图文件的后缀
internalResourceViewResolver.setSuffix(".jsp");
internalResourceViewResolver.setExposeContextBeansAsAttributes(true);
return internalResourceViewResolver;
}
/**
* 视图配置
*
* @param registry
*/
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.viewResolver(resourceViewResolver());
}
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
4.注册配置配置类
package com.painter.config;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
/**
* @Author: Painter
* @project_name: SpringMVC
* @system_login: sunshine
* @time: 2022/10/916:05
*/
public class ServletInitializer extends AbstractDispatcherServletInitializer { // 注册配置类
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext = new AnnotationConfigWebApplicationContext();
// 注册我们的 springmvc config 配置类
annotationConfigWebApplicationContext.register(SpringMVCConfig.class);
return annotationConfigWebApplicationContext;
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
}
访问 http://127.0.0.1:8080/jsp/jsp
会自动在 WEB-INF/jsp找指定的test.jsp文件