0依赖

<!-- jsp -->
<dependency>
	<groupId>org.apache.tomcat.embed</groupId>
	<artifactId>tomcat-embed-jasper</artifactId>
</dependency> 

 

1、添加代码式注解,配置springmvc

@Configuration
public class SpringMvcConfigure extends WebMvcConfigurerAdapter {

	@Bean
	public InternalResourceViewResolver viewResolver() {
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setPrefix("/WEB-INF/jsp/");
		viewResolver.setSuffix(".jsp");
		// viewResolver.setViewClass(JstlView.class); // 这个属性通常并不需要手动配置,高版本的Spring会自动检测
		return viewResolver;
	}

}

  

2、创建mvc的controller

@Controller
public class HelloWorldController {
	@RequestMapping("/hello")
	public ModelAndView hello() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("msg", "this a msg from HelloWorldController");
		mv.setViewName("helloworld");
		return mv;
	}

}

 

3、根据代码式注解创建jsp所在目录以及文件

src/main/webapp/WEB-INF/jsp/helloworld.jsp(创建webapp目录可以参见另一随笔)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<title>Insert title here</title>
	</head>
	<body>
		1
	</body>
</html>

  

4、访问http://localhost:8080/hello