IDEA spring-boot jpa jsp 框架搭建(三)
来源:互联网 发布:java面试基础知识总结 编辑:程序博客网 时间:2024/05/22 10:34
IDEA spring-boot jpa jsp 框架搭建(三)
上一节我们详细的说了一下 JPA 的相关操作,今天我们来进行JSP的整合。spring-boot 默认支持的模板引擎是 Thymeleaf模板引擎,我看了很多文章,都说Thymeleaf如何好用,但我用了一下,感觉极其难玩,比如HTML页面上差了一个/,那就报错,我被它搞得真是疯掉了。所以我还是选择使用JSP。
添加依赖
使用jsp时必须要添加它相关的依赖,我们打开pom.xml,添加以下依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- servlet 依赖包 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <!-- JSTL (JSP standard Tag Library) JSP 标准标签库 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- Tomcat的支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency>
下一步是配置application.properties
只需要添加这2个就可以了
#页面默认前缀目录spring.mvc.view.prefix=/WEB-INF/views/#页面默认后缀目录spring.mvc.view.suffix=.jsp
新建文件
接下来需要新建一个文件,在src\mian下新建一个webapp文件夹,再在webapp文件夹下面新建WEB-INF/views这两个文件夹。
演示一下如何创建文件夹:
则最终的目录结构为:
配置IDEA
左边选项卡选到 Modules
点击“加号”添加
然后就是这个样子
下面就要更改相关信息:
更改这2处地方,默认的是web,要更改为src\main\webapp,配置好之后,上文提到的web.xml就自动出现了,不需要去更改。然后去重启项目,就可以正常访问jsp页面了。
测试一下
在 views 下新建一个jsp页面,命名为index.jsp
<%-- Created by IntelliJ IDEA. User: LD Date: 2017/12/12/0012 Time: 9:12--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>hello world</title></head><body><h1>欢迎来到此页面</h1></body></html>
然后重新写一下controller,我们之前建立了一个controller,名字是IndexController,现在我们需要修改一下;
原来的controller 使用的是 @RestController 这个注解,这个注解我之前也解释过了,它意味着数据以json形式传递,现在我们想 直接返回到界面,所以需要使用@Controller 这个注解;
原来的controller
package cn.itlaobing.web.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class IndexController { @RequestMapping("index") public String index(){ return "hello world"; }}
现在的controller
package cn.itlaobing.web.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@Controllerpublic class IndexController { @RequestMapping("index") public String index(){ //返回到index.jsp界面 return "index"; }}
好了,现在启动服务器,访问测试一下!!!
测试成功,页面可以访问了,今天的知识就先到这里了,有不懂的朋友欢迎打扰,后面我会继续更新。。。
未完待续。。。
阅读全文
0 0
- IDEA spring-boot jpa jsp 框架搭建(三)
- IDEA spring-boot jpa jsp 框架搭建(一)
- IDEA spring-boot jpa jsp 框架搭建(二)
- idea 搭建maven+spring mvc+jpa框架
- IntellJ IDEA+Spring Boot+JSP
- IDEA Spring-Boot没有JSP
- IDEA spring boot+jsp 404
- Spring Boot框架搭建
- spring boot jsp mvc jpa mysql
- [三]Spring Boot 整合jsp
- 【框架搭建】spring boot入门
- spring boot maven 框架搭建
- 在IDEA中使用spring-boot,mySql,JPA
- IDEA Spring boot工程的搭建
- 搭建Spring boot基本环境-idea
- Spring Boot的快速搭建(IDEA)
- 在IntelliJ IDEA下搭建基于Maven的spring boot框架
- idea Spring-boot三种启动方式
- 统一编址和独立编址
- 上升沿检测代码实现
- Java中的ReentrantLock和synchronized两种锁定机制的对比
- 对于随机森林的通俗理解
- Linux第一课1.1-1.5
- IDEA spring-boot jpa jsp 框架搭建(三)
- CSS3实现文字流光渐变特效
- 我们为什么要使用Spring Cloud?
- java--面向对象之继承2
- 【AI架构】第四范式发布“先知”3.0,助力企业打造AI核心
- 【数字经济】概念、技术、实践:中国 500强泰康集团背后的数据驱动
- Centos7下安装11gR2(Error in invoking target 'agent nmhs' of makefile解决方式)
- 九九乘法表
- html5之sse服务器发送事件EventSource介绍