千峰商城-springboot项目实战03-SpringBoot整合JSP

1.添加依赖

在pom.xml中的<dependencies> * </dependencies>标签中添加jsp、jstl依赖。

 

复制代码
        <!--整合jsp-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <!--jstl-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
复制代码

 

2.在pom.xml中将项目打包方式改为war。

    <packaging>war</packaging>

3.在main下面新建一个目录webapp,在webapp下新建一个目录WEB-INF。在WEB-INF下边新建一个jsp文件:index.jsp。

 

 

index.jsp:

复制代码
  Created by IntelliJ IDEA.
  User: 临易
  Date: 2022/4/15
  Time: 20:10
  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>
index..
</body>
</html>
复制代码

 

4.启动springboot项目运行。

在浏览器网址栏输入: http://localhost:8080/demo1/index.jsp

页面显示 index..  ,jsp整合成功。

 

 

 

 5.如果不想异步,则修改UserController

(删去@ResponseBody,将方法rigist的类型改为String,在rigist方法中返回  “/index.jsp”)

 

UserController.java:

复制代码
package com.qfedu.springboot.demo.controller;

import com.qfedu.springboot.demo.entity.User;
import com.qfedu.springboot.demo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

@Controller
@RequestMapping("/user")
public class UserController {

    //通过Resoure注入UserService
    @Resource
    private UserService userService;

    //前后端分离。前端所有消息都是异步请求。
    @RequestMapping("/regist")
    //@ResponseBody
    public String regist(User user){
        userService.userRegist(user);
        return "/index.jsp";
    }
}
复制代码

 

 

6.执行

http://localhost:8081/demo1/user/regist?userName=Lucy&userPwd=99999&userRealname=Lulu&userImg=05.jpg

 

 

 

执行成功!

 

7.在application.yml文件中,增加mvc前缀与后缀属性。

 

 application.yml的spring部分:

复制代码
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/qfedu?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
  mvc:
    view:
      prefix: /
      suffix: .jsp
复制代码

此时,UserController中的跳转就可以直接写index。

 

 UserController.java:

复制代码
package com.qfedu.springboot.demo.controller;

import com.qfedu.springboot.demo.entity.User;
import com.qfedu.springboot.demo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

@Controller
@RequestMapping("/user")
public class UserController {

    //通过Resoure注入UserService
    @Resource
    private UserService userService;

    //前后端分离。前端所有消息都是异步请求。
    @RequestMapping("/regist")
    //@ResponseBody
    public String regist(User user){
        userService.userRegist(user);
        return "index";
    }
}
复制代码

 

 

8.提高页面的安全性:将index.jsp放到WEB-INF文件夹下后,index.jap文件将只能通过controller访问。

 

 

然后将application.yml配置文件中的前缀改为  "/WEB-INF/" ,则可访问。

 application.yml的spring部分:

复制代码
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/qfedu?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
  mvc:
    view:
      prefix: /WEB-INF/
      suffix: .jsp
复制代码

在包controller中重新创建一个类PageController,只做页面的请求转发。

 

 PageController.java:

复制代码
package com.qfedu.springboot.demo.controller;

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

@Controller
public class PageController {
    
    @RequestMapping("/index.html")      //障眼法
    public String index(){
        return "index";
    }
}
复制代码

 

 

9.测试

输入  “http://localhost:8081/demo1/index.jsp

 

 结果不能访问到页面了。

 

 而输入  “http://localhost:8081/demo1/index.html”

 

 则可以访问 index.jsp 页面。

 

 

实际过程:这个请求被controller拦截,然后controller转发到了index,再通过application.yml的mvc增加前缀,后缀配置,最后成功访问!

 

 

 

 

 

 
 
 
posted @   临易  阅读(55)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
点击右上角即可分享
微信分享提示