猿问

配置java spring boot读取JSP文件

有人知道如何配置 java spring boot 来读取 JSP 文件吗?互联网上的方法对我不起作用。我已经在 porm.xml 中添加了 tomcat jasper 依赖项。


我的 JSP 文件:home.jsp,


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

I love java

</body>

</html>

在中添加依赖项PORM.XML


<!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->

<dependency>

    <groupId>org.apache.tomcat.embed</groupId>

    <artifactId>tomcat-embed-jasper</artifactId>

    <scope>provided</scope>

</dependency>


<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat -->

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-tomcat</artifactId>

    <version>2.1.3.RELEASE</version>

</dependency>            

我的控制器:HomeController.java


package com.example.demo;


import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;


@Controller

public class HomeController {

    @RequestMapping("home")

    @ResponseBody

    public String home() {

        System.out.println("hi");

        return "home.jsp";

    }

}

请解释这里有什么问题?我正在使用 STS IDE。添加 tomcat jasper 对我不起作用。


泛舟湖上清波郎朗
浏览 142回答 4
4回答

慕尼黑5688855

@ResponseBody从您的控制器方法中删除@Controllerpublic class HomeController {&nbsp; &nbsp; @RequestMapping("home")&nbsp; &nbsp; public String home() {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("hi");&nbsp; &nbsp; &nbsp; &nbsp; return "home.jsp";&nbsp; &nbsp; }&nbsp;}

四季花海

更新 application.propertiesspring.mvc.view.suffix=.jspspring.mvc.view.prefix=/WEB-INF/jsp/并返回不带扩展名的 jsp 名称,例如“home”而不是“home.jsp”

潇潇雨雨

使用模型和视图返回 jsp 页面。在下面的代码中,List 是 jsp 页面的名称。@RequestMapping( value="/dataCreate", method = {RequestMethod.POST })&nbsp; &nbsp; &nbsp; &nbsp; public ModelAndView createTemplate(HttpServletRequest request,HttpServletResponse response)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MultipartFile cuscsvfile = multipartRequest.getFile("cuscsvfile");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ModelAndView modelView=new ModelAndView("redirect:list");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String templateName = request.getParameter("templateName");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String msg = messageSource.getMessage("new.template.creation.success", new Object[]{templateName}, Locale.ENGLISH);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modelView.addObject("msg", msg);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //return "redirect:/list";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return modelView;&nbsp; &nbsp; &nbsp; &nbsp; }

尚方宝剑之说

试试看,不用.jsp也可以返回。@RequestMapping("home")&nbsp; &nbsp; public String home() {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("hi");&nbsp; &nbsp; &nbsp; &nbsp; return "home";&nbsp; &nbsp; }为此,我们必须在application.properties文件中进行一些配置,spring.mvc.view.suffix=.jsp(但这不是下面问题的确切答案。这只是您如何修改代码的一个想法。)
随时随地看视频慕课网APP

相关分类

Java
我要回答