新建一个项目,添加web支持 ,然后修改pom.xml。
<!--添加jsp页面的解析依赖--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!--jsp文件的存放以及编译目标文件夹--> <resources> <!--手动指springboot的jsp编译的位置,比如META-INF/resources--> <resource> <!--源文件夹--> <directory>src/main/webapp</directory> <!--指定编译文件夹--> <targetPath>META-INF/resources</targetPath> <!--指定哪些源文件要进行编译--> <includes> <include>*.*</include> </includes> </resource> </resources>
然后,在项目结构里(project structure)里的module里添加当前项目的web支持。
然后,创建一个controller类
package com.example.control; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; @Controller public class MyController { @RequestMapping("/say")//此处say指定了浏览器访问地址,比如:http://127.0.0.1:8080/say public ModelAndView say(){ ModelAndView mv=new ModelAndView(); mv.addObject("message","hehe ,got it!");//此处message等下会在页面中自动配置,并显示后面的内容 mv.setViewName("say");//此处say,与webapp中的页面文件同名,这样就能印射到相应的jsp文件上 return mv; } }
然后,创建一个jsp文件得到ModelAndView对象,并显其中内容。
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>${message}</h1> </body> </html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!