
第五步:再webapp下创建hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
hello jsp
abc
页面中使用了超链接,当点击超链接,会跳转到后台有注解"/abc"的位置进行数据处理。
第六步:src/main/com/pp/springbootjsp下创建一个controller包,然后新建一个HelloController类package com.pp.springbootjsp.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("/abc")
public String hello(Model model){
model.addAttribute("msg","你好");
return "success";
}
}
在类上面使用注解@Controller,然后再hello方法上使用注解@GetMapping("/abc"),从而让前端页面点击连接后识别到此处运行此方法,hello的方法中传入了Model 类型的变量model,再方法内使用model.addAttribute(“msg”,“你好”);将“你好”传送到“msg”,最后使用return “success”;返回到success.jsp。这里返回的应该是一个是路径的全地址(http://localhost:8080//WEB-INF/success.jsp),但是使用类配置文件,只需要写文件名,前缀和后缀都会自动添加。
第七步:写application.properties配置文件spring.mvc.view.prefix=/WEB-INF/ spring.mvc.view.suffix=.jsp第八步:配置tomcat服务器 1.点击idea的Add Configuration 2.点左上角的加号,然年点Tomcat Server,然后点击local 3.如果以前有配置Tomcat就点击图中标注的地方,如果没有就点击后面的Configure… 4.然后点击Deployment,再点击图中2处的加号,然后点击Artifact 5.连个都可以使用 6.将Application context清楚,然后点击Apply。 7.点击下面页面中运行按钮 第九步:运行结果 1.再页面输入http://localhost:8080/hello.jsp 2.点击abc链接
源码地址:https://gitee.com/yangforever/project-learning/tree/master/demo/SpringBoot/springboot-jsp