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

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

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的配置文件中配置
视图解析器。

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

(3)jsp:forward page;

<jsp:forward page="{路径|<%=表达式%>}"/>
 
  <jsp:param name="参数名称" value="参数内容"/>
 
</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包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.3.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.mr</groupId>
   <artifactId>springboot_jsp</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>springboot_jsp</name>
   <description>Demo project for Spring Boot</description>

   <properties>
      <java.version>1.8</java.version>
   </properties>
   <packaging>war</packaging>
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>

      <!--jsp支持-->
      <!-- servlet 依赖. -->
      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>javax.servlet-api</artifactId>
         <scope>provided</scope>
      </dependency>
      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>jstl</artifactId>
      </dependency>
      <!-- tomcat 的支持.-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-tomcat</artifactId>
      </dependency>
      <!--引入Spring boot内嵌的Tomcat对jsp的解析包-->
      <dependency>
         <groupId>org.apache.tomcat.embed</groupId>
         <artifactId>tomcat-embed-jasper</artifactId>
         <scope>provided</scope>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
      <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>
   </build>

</project>

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

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

添加web

选择已经添加的webapp

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

5.application.properties

server.port=8082

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

简单Demo

6.JspController.java

@Controller
public class JspController {
    @GetMapping("/boot/index")
    public String index(Model model){
        model.addAttribute("msg","Spring boot集成jsp");
        return "index";
    }
    @GetMapping("/boot/aaa")    
    public String aaa(Model model){
        model.addAttribute("sss","boot 整合jsp");
        return "aaa";
    }
}

7.index.jsp

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

8.aaa.jsp

<%--
  Created by IntelliJ IDEA.
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${sss}
</body>
</html>

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

浏览器效果

相关文章