在Spring boot中使用jsp,按如下步骤进行:
1、在pom.xml文件中配置依赖项
<!--引入Spring Boot内嵌的Tomcat对JSP的解析包--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- servlet依赖的jar包start --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <!-- servlet依赖的jar包start --> <!-- jsp依赖jar包start --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> </dependency> <!-- jsp依赖jar包end --> <!--jstl标签依赖的jar包start --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!--jstl标签依赖的jar包end -->
2、在application.properties文件配置spring mvc的视图展示为jsp:
#springboot 集成jsp #如果jsp页面放在webapps根目录下面 spring.mvc.view.prefix=/ #如果jsp页面放在WEB-INF目录下面 #spring.mvc.view.prefix=/WEB-INF/pages/ spring.mvc.view.suffix=.jsp
3、在src/main 下创建一个webapp目录,然后在该目录下新建index.jsp页面,pom.xml文件的build中要配置备注中的配置信息
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> </resource> <resource> <directory>src/main/webapp</directory> <targetPath>META-INF/resources</targetPath> <includes> <include>**/*.*</include> </includes> </resource> </resources>
控制器中的方法返回jsp页面
@Controller public class HelloController { @Autowired private Myconfig myconfig; @RequestMapping("index.do") public Object list(Model model){ model.addAttribute("userName","joinlabs"); return "index"; } }
访问index.do就会跳转到index.jsp页面了,而且可以通过EL表达式获取到保存的参数的值