猿问
下载APP

请问该如何在SpringBoot中使用JSP?

如何在SpringBoot中使用JSP


若吾皇
浏览 63回答 4
4回答

慕雪6442864

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的关联# MVCspring.view.prefix=/WEB-INF/views/spring.view.suffix=.jsp3. 创建src/main/webapp/WEB-INF/views目录,JSP文件就放这里<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Hello</title></head><body>Hello ${name}</body></html>4. 编写Controllerpackage 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@EnableAutoConfigurationpublic 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;@SpringBootApplicationpublic class WebApplication extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(WebApplication.class);}public static void main(String[] args) throws Exception {SpringApplication.run(WebApplication.class, args);}

拉莫斯之舞

创建PageController,编码如下: package org.springboot.sample.controller; import java.util.Date; import java.util.Map; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; ...

繁星点点滴滴

这个跟springmvc一样的啊,首先你看你的spring-mvc.xml有没有配置defaultViewResolver,然后你在action的方法中如果1.标注了@ResponseBody,返回字符串的话是通过write输出到页面。2.没有标注这个,springmvc会到配置的目录下找相应的jsp。比如返回"hello",它就在webpage/目录下找hello.jsp。返回"user/login",它就会找webpage/user/login.jsp

慕的地2183247

这个跟spring mvc一样的啊,首先你看你的spring-mvc.xml 有没有配置defaultViewResolver,<property name="prefix" value="/webpage/" /><property name="suffix" value=".jsp" />然后你在action的方法中如果1.标注了@ResponseBody,返回字符串的话是通过write输出到页面。2.没有标注这个,spring mvc会到配置的目录下 找相应的jsp。比如返回 "hello",它就在 webpage/目录下找hello.jsp。 返回 "user/login",它就会找 webpage/user/login.jsp
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答