如何在SpringBoot中使用JSP
2017-01-22 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
1. 在pom.xm中加入支持JSP的依赖
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
2. 在src/main/resources/application.properties文件中配置JSP和传统Spring MVC中和view的关联
# MVC
spring.view.prefix=/WEB-INF/views/
spring.view.suffix=.jsp
3. 创建src/main/webapp/WEB-INF/views目录,JSP文件就放这里
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
Hello ${name}
</body>
</html>
4. 编写Controller
package com.chry.study;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/hello")
public ModelAndView getListaUtentiView(){
ModelMap model = new ModelMap();
model.addAttribute("name", "Spring Boot");
return new ModelAndView("hello", model);
}
}
5. 编写Application类
package com.chry.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(WebApplication.class, args);
}
}
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
2. 在src/main/resources/application.properties文件中配置JSP和传统Spring MVC中和view的关联
# MVC
spring.view.prefix=/WEB-INF/views/
spring.view.suffix=.jsp
3. 创建src/main/webapp/WEB-INF/views目录,JSP文件就放这里
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
Hello ${name}
</body>
</html>
4. 编写Controller
package com.chry.study;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/hello")
public ModelAndView getListaUtentiView(){
ModelMap model = new ModelMap();
model.addAttribute("name", "Spring Boot");
return new ModelAndView("hello", model);
}
}
5. 编写Application类
package com.chry.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(WebApplication.class, args);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询