赞
踩
使用idea创建一个基于gradle的springboot项目,在build.gradle
文件中加入:
//加入后自动生成`webapp`目录
apply plugin: 'war'
加入依赖:
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
//Required dependency for JSP
//providedRuntime 不能用,因为spring5.x不认了
compile 'org.apache.tomcat.embed:tomcat-embed-jasper'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
项目结构:
WebMvcConfig.java
配置,实现WebMvcConfigurer
:
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
registry.viewResolver(resolver);
}
}
WebMvcConfigurerAdapter
在java8中被遗弃,因为java8中接口可以存在默认方法的原因:
@Deprecated
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
}
HomeController.java
@Controller
public class HomeController {
@RequestMapping("welcome")
public String welcome(){
return "welcome";
}
}
并在/WEB-INF/views/
下创建welcome.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>welcome</title>
</head>
<body>
welcome
</body>
</html>
启动Demo2Application
,然后访问http://localhost:8080/welcome
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。