手把手教你:SpringBoot开发JSP

本文详细介绍了如何在SpringBoot项目中使用JSP作为视图技术。通过四步,包括创建war包项目、配置application.properties、编写Controller以及打包和部署,展示了如何实现JSP页面的访问和控制器交互。实验结果显示,成功调用了不同的方法并呈现了对应JSP页面的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文专注于采用SpringBoot来开发JSP。

默认啊这个SB框架支持四个视图引擎,我们也都不会用,就会点JSP。所以采用JSP来做视图技术。

步骤:

第一步:创建一个应用,打包方式采用war。这个具体的步骤参考我的上一篇博文:https://blog.youkuaiyun.com/superfreak/article/details/115082381

第二步:外部配置修改application.properties文件,添加如下内容:

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

该文件位置如图:

这么做有利于jsp文件的保密,外部不能访问,而控制器却能访问。

第三步:修改你的Controller ,代码如下:

package com.example.springbootwartest;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import org.springframework.web.servlet.ModelAndView;

@RestController
public class HelloController {

	@RequestMapping("/")
	public String index() {
		return "Greetings from Spring Boot,The APP will be packaged to a war!";
	}
	@RequestMapping("/test")
	public String test() {
		return "test";
	}
	@RequestMapping("/showDefaultViewResolver")
	public ModelAndView doshowDefaultViewResolver()  throws Exception {
		 ModelAndView showDefaultViewResolver = new ModelAndView();
		 showDefaultViewResolver.setViewName("test2"); 
		 System.out.println("hello springMVC in the showDefaultViewResolver url");
		return showDefaultViewResolver;
		
	}
	
	@RequestMapping("/testMethod")
    public ModelAndView testMethod() throws Exception{
        ModelAndView mv = new ModelAndView();
        mv.setViewName("test"); 
        System.out.println("hello springMVC in the testMethod url");
        return mv;
	}

	

}

第四步:打包及部署。

程序打包

打包是在STS中:

对话框中Goals处输入:clean package 

点击Run,就会给你打包了。

war包得到后,放入tomcat的webapp目录下,启动你的tomcat,

然后打开浏览器,地址栏输入以下地址来看看结果

1、地址栏输入:http://localhost:8080/spring-boot-war-test-0.0.1-SNAPSHOT/  

这样根据我们的controller中的代码,将调用这个方法:

@RequestMapping("/")
	public String index() {
		return "Greetings from Spring Boot,The APP will be packaged to a war!";
	}

实际结果也是这样的。 

2、地址栏输入:http://localhost:8080/spring-boot-war-test-0.0.1-SNAPSHOT/test

根据代码,调用这个方法:

@RequestMapping("/test")
	public String test() {
		return "test";
	}

实际结果如下:

3、地址栏输入:http://localhost:8080/spring-boot-war-test-0.0.1-SNAPSHOT/testMethod

调用方法:

@RequestMapping("/testMethod")
    public ModelAndView testMethod() throws Exception{
        ModelAndView mv = new ModelAndView();
        mv.setViewName("test"); 
        System.out.println("hello springMVC in the testMethod url");
        return mv;
	}

这里用到了类ModelAndView ,返回的是一个 ModelAndView,最终它找到了位于WEB-INF中的test.jsp文件

 

结果如下:

4 地址栏输入:http://localhost:8080/spring-boot-war-test-0.0.1-SNAPSHOT/showDefaultViewResolver

根据代码会调用:

@RequestMapping("/showDefaultViewResolver")
	public ModelAndView doshowDefaultViewResolver()  throws Exception {
		 ModelAndView showDefaultViewResolver = new ModelAndView();
		 showDefaultViewResolver.setViewName("test2"); 
		 System.out.println("hello springMVC in the showDefaultViewResolver url");
		return showDefaultViewResolver;
		
	}

最终映射为了test2.jsp

结果如下:

我想在test2.jsp中显示一些SB框架给我们的创建的哪些Bean,但是目前没有成功。后续想试一试usebean。

项目中的两个jsp文件的位置和代码如下:

test.jsp内容:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
I am the test jsp page.

</body>
</html>

test2.jsp内容:

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import ="org.springframework.context.ApplicationContext" %>
<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
<title>Show the DefaultViewResolver</title>
</head>
<body>
<p>
I am the test2 jsp 
I want to show something of spring Boot Framework ,
Maybe ,I should Use usebean .
<p>







</body>
</html>

最后,对于想用SpringBoot 开发JSP的同志们来说,按照我的博客做以及可以了。

再次奥!

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值