SpringMVC访问WEB-INF下的jsp解决方案&Spring Boot集成使用jsp

x33g5p2x  于2022-05-06 转载在 Spring  
字(5.8k)|赞(0)|评价(0)|浏览(556)

SpringMVC访问WEB-INF下的jsp解决方案

一. 问题

​将项目中用到的jsp等文件放在WEB-INF目录下。实际开发过程中,需要在框架页面通过iframe嵌入对应的具体页面,此处如果直接调用对应页面所在的url地址,则会提示404错误。

​ WEB-INF目录下的文件不能直接访问,需要映射,jsp放在那里也不能访问,应该是servlet或javabean才行。

二.解决思路

1. 为什么要将对应jsp放在WEB-INF下?

​ 传统的java web项目考虑一定的安全性都会选择把jsp放在web-inf下,毕竟jsp存在部分java代码是要经过编译运行的,通过后台servlet action或者controller进行跳转,这样也便于统一管理维护。再做一些过滤器拦截器之类的安全措施可以说是双重保险。

2.如何实现正常访问

(1)直接把JSP页面放到WEB-INF外的webapp目录下;

(2)Controller进行处理;

​ 通过ModelAndView来进行跳转,跳转到compare.jsp页面。当然要用ModelAndView的话需要在Spring的配置文件中配置
视图解析器。

  1. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  2. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  3. <property name="prefix" value="/WEB-INF/vm/" />
  4. <property name="suffix" value=".jsp" />
  5. </bean>

(3)jsp:forward page;

  1. <jsp:forward page="{路径|<%=表达式%>}"/>
  2.   <jsp:param name="参数名称" value="参数内容"/>
  3. </jsp:forward>

(4)基于视图解析器,添加mvc:view-controller

两种用法

1、重定向
<mvc:view-controller path=“/” view-name=“redirect:/admin/index”/>
即如果当前路径是/ 则重定向到/admin/index

2、view name
<mvc:view-controller path=“/” view-name=admin/index"/>
如果当前路径是/ 则交给相应的视图解析器直接解析为视图

mvc:view-controller相当于@RequestMapping

注意:

1. 使用这个标签后,必须配置,否则会造成所有的@Controller注解无法解析

2.如果请求存在处理器,则这个标签对应的请求处理不起作用。因为请求先去找处理器,如果找不到,才使用这个标签

Spring Boot集成使用jsp

简介:

官方不推荐使用jsp作为页面,我们可以使用其他的模板引擎,比如 Thymeleaf和 freemarker,官方主推的是Thymeleaf。但是目前来说,很多项目的页面还是用的jsp.而且很多现成的项目用的jsp页面,扒过来就能用,当然如果时间允许的情况,还是不建议大家使用jsp,而是采用官方推荐的模板。

搭建项目

1.新建一个基础的 springboot WEB项目

2.pom.xml文件jar包

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.3.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.mr</groupId>
  12. <artifactId>springboot_jsp</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springboot_jsp</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <packaging>war</packaging>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-test</artifactId>
  28. <scope>test</scope>
  29. </dependency>
  30. <!--jsp支持-->
  31. <!-- servlet 依赖. -->
  32. <dependency>
  33. <groupId>javax.servlet</groupId>
  34. <artifactId>javax.servlet-api</artifactId>
  35. <scope>provided</scope>
  36. </dependency>
  37. <dependency>
  38. <groupId>javax.servlet</groupId>
  39. <artifactId>jstl</artifactId>
  40. </dependency>
  41. <!-- tomcat 的支持.-->
  42. <dependency>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-starter-tomcat</artifactId>
  45. </dependency>
  46. <!--引入Spring boot内嵌的Tomcat对jsp的解析包-->
  47. <dependency>
  48. <groupId>org.apache.tomcat.embed</groupId>
  49. <artifactId>tomcat-embed-jasper</artifactId>
  50. <scope>provided</scope>
  51. </dependency>
  52. </dependencies>
  53. <build>
  54. <plugins>
  55. <plugin>
  56. <groupId>org.springframework.boot</groupId>
  57. <artifactId>spring-boot-maven-plugin</artifactId>
  58. </plugin>
  59. </plugins>
  60. <resources>
  61. <resource>
  62. <directory>src/main/java</directory>
  63. <includes>
  64. <include>**/*.xml</include>
  65. </includes>
  66. </resource>
  67. <resource>
  68. <directory>src/main/resources</directory>
  69. <includes>
  70. <include>**/*.*</include>
  71. </includes>
  72. </resource>
  73. <resource>
  74. <directory>src/main/webapp</directory>
  75. <targetPath>META-INF/resources</targetPath>
  76. <includes>
  77. <include>**/*.*</include>
  78. </includes>
  79. </resource>
  80. </resources>
  81. </build>
  82. </project>

3. 新建webapp文件夹,与resources同级

4. 新建JSP页面,此时发现New里面没有JSP页面。需要设置一下才会出现

添加web

选择已经添加的webapp

此时webapp上有蓝色圆点,表示设置成功

5.application.properties

  1. server.port=8082
  2. #页面默认前缀目录 默认在webapp下有别的文件夹可以,以文件夹/往下加
  3. spring.mvc.view.prefix=/
  4. #页面默认后缀目录
  5. spring.mvc.view.suffix=.jsp``

简单Demo

6.JspController.java

  1. @Controller
  2. public class JspController {
  3. @GetMapping("/boot/index")
  4. public String index(Model model){
  5. model.addAttribute("msg","Spring boot集成jsp");
  6. return "index";
  7. }
  8. @GetMapping("/boot/aaa")
  9. public String aaa(Model model){
  10. model.addAttribute("sss","boot 整合jsp");
  11. return "aaa";
  12. }
  13. }

7.index.jsp

  1. <%@
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <h1>Hello word</h1>
  11. ${msg}
  12. </body>
  13. </html>

8.aaa.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. To change this template use File | Settings | File Templates.
  4. --%>
  5. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  6. <html>
  7. <head>
  8. <title>Title</title>
  9. </head>
  10. <body>
  11. ${sss}
  12. </body>
  13. </html>

右键选择spring-boot:run启动项目即可。

浏览器效果

相关文章