简体   繁体   English

无法使用Spring Boot访问JSP页面

[英]Cannot acess a JSP page using Spring Boot

I'd would like to add mapping in Spring Boot (1.5.4) for a jsp page. 我想在Spring Boot (1.5.4)中为jsp页面添加映射。 First of all, I started by creating in project in IntelliJ . 首先,我首先在IntelliJ创建项目。 Then I added a few packages in pom.xml . 然后我在pom.xml添加了一些软件包。 Now I have in this file the following dependencies: 现在,此文件中具有以下依赖项:

  • spring-boot-starter-thymeleaf 弹簧引导起动thymeleaf
  • spring-boot-starter-web 弹簧引导启动的Web
  • tomcat-embed-jasper Tomcat的嵌入-碧玉
  • spring-boot-devtools 弹簧引导devtools
  • spring-boot-starter-test 弹簧引导启动测试
  • jstl JSTL

Next, I created a class to make a mapping for /login . 接下来,我创建了一个类来为/login进行映射。

@Controller
public class LoginController
{
    @RequestMapping("/login")
    public String login()
    {
        return "login";
    }
}

I created login.jsp file in the location of src/main/webapp/web-inf/jsp . 我在src/main/webapp/web-inf/jsp的位置创建了login.jsp文件。

在此处输入图片说明

Lastly, I added the following in application.properties : 最后,我在application.properties添加了以下内容:

spring.mvc.view.prefix = /WEB-INF/jsp/
spring.mvc.view.suffix = .jsp

Unfortunelty, when I try access localhost/login I get the Whitelabel Error Page page. 不幸的是,当我尝试访问localhost/login我得到了Whitelabel Error Page页面。 What have I missed with configuration? 我错过了哪些配置?

I have faced the same problem today and have found your question while googling the solution. 我今天也遇到了同样的问题,并且在搜索解决方案时发现了您的问题。

It is a common IntelliJ problem, so, I'll tell you, what I have done to make app work (hope it will help). 这是一个常见的IntelliJ问题,所以,我将告诉您,我为使应用程序正常工作所做的事情(希望它会有所帮助)。

First of all, try to go to http://localhost:8080/login (you mentioned localhost without port number). 首先,尝试转到http:// localhost:8080 / login (您提到的localhost没有端口号)。 If this doesn't help, try the following steps: 如果这样做没有帮助,请尝试以下步骤:

  1. Change your package name to the same case as in properties-file (WEB-INF instead of lowercase). 将包名称更改为与properties-file中的大小写相同(WEB-INF而不是小写)。
  2. Go to Project Structure (CTRL+ALT+SHIFT+S on Windows/Linux) -> Modules -> find the module with your project name -> expand it -> Web -> Web Resources Directories: "Web Resource Directory" - should be the path to your webapp folder (for example, on my Linux machine it was /home/user/Documents/Java/Projects/myproject/src/main/webapp ), "Path Relative to Deployment Root" - "/" (without quotes) -> Apply. 转到项目结构(在Windows / Linux上为CTRL + ALT + SHIFT + S)->模块->查找具有您项目名称的模块->展开它-> Web-> Web资源目录:“ Web资源目录”-应该是您的webapp文件夹的路径(例如,在我的Linux机器上是/ home / user / Documents / Java / Projects / myproject / src / main / webapp),“相对于部署根的路径”-“ /”(不带引号) )->应用。
  3. Return to module with the same name as your project and instead of Web choose Spring -> check that your main class should be mentioned there twice and your properties file once (if no, add them by "+" button). 返回与您的项目同名的模块,而不是Web,请选择Spring->检查您的主类应该在其中提及两次,并在属性文件中提及一次(如果否,请通过“ +”按钮添加它们)。
  4. Click on Select Run/Debug Configuration (a block with your main class name near Run button) -> Edit configurations... -> Code Coverage tab -> Packages and classes to record coverage data: Add package (green plus with small package icon button) -> type your classpath + ". " (without quotes) (for example, com.example. ) -> Is Active and Include/Exclude checkboxes should be checked -> OK. 单击选择运行/调试配置(主类名称位于运行按钮附近的块)->编辑配置...->代码覆盖率选项卡->记录覆盖率数据的包和类:添加包(绿色加小包图标)按钮)->输入类路径+“。 ”(不带引号)(例如com.example。 )->处于活动状态,应选中包含/排除复选框->确定。
  5. Open IntelliJ Terminal, type "mvn clean install" (without quotes) -> ENTER -> wait untill the process will finish -> you shoul see a message something like: 打开IntelliJ Terminal,输入“ mvn clean install”(不带引号)-> ENTER->等待该过程完成->您应该看到类似以下的消息:

    [INFO] BUILD SUCCESS [INFO] -------------------------------------------------------------- [INFO] Total time: 9.339 s [INFO] Finished at: 2018-03-03T00:46:58+02:00 [INFO] Final Memory: 36M/273M [INFO] [INFO]成功建立[INFO] ------------------------------------------ -------------------- [INFO]总时间:9.339 s [INFO]完成于:2018-03-03T00:46:58 + 02:00 [INFO]最终内存:36M / 273M [INFO]

  6. Go to browser -> http://localhost:8080/login -> your login.jsp page should appear (if no luck try CTRL+F5 on Windows/Linux to refresh webpage cash). 转到浏览器-> http:// localhost:8080 / login->您的login.jsp页面应该出现(如果没有运气,请尝试在Windows / Linux上按CTRL + F5刷新网页现金)。

Hope this gonna help you, as it works for me. 希望这会对您有所帮助,因为它对我有用。

Have fun! 玩得开心! =) =)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM