springboot jsp引入和配置

文章目录

    • 如何配置JSP页面
    • 如何给JSP页面加载资源(CSS、js、image)

如何配置JSP页面

  1. 在pom.xml文件中添加如下配置

<dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-tomcatartifactId><scope>providedscope>
dependency>
<dependency><groupId>org.apache.tomcat.embedgroupId><artifactId>tomcat-embed-jasperartifactId><scope>providedscope>
dependency>
  1. 添加配置文件
# 配置JSP页面,
spring.mvc.view.prefix=/WEB-INF/jsp/ 
spring.mvc.view.suffix=.jsp 
  1. 创建/src/main/webapp/WEB-INF/jsp目录,用来存放jsp文件
  2. 创建/src/main/webapp/WEB-INF/web.xml 文件
  3. 创建Controller类,实现URL定向到具体的JSP页面
package com.hb.page;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;/*** 使用@Controller 注解可以跳转到对应的jsp页面中去* @author 黄彪* 2018年9月6日*/
@Controller
@RequestMapping("/pcustomer")
public class CustomerPageController {@RequestMapping("/Add")public String index(){return "addCustomer";}
}

说明:在地址栏中输入 localhost:9090/pcustomer/Add;页面显示的是/src/main/webapp/WEB-INF/jsp/addCustomer.jsp的内容

@RestController用于描述REST服务,相当于@Controller 和 @ResponseBody的组合,@ResponseBody表示请求直接返回的是字符串,而不是跳转到页面,因此控制器智能使用@Controller注解,才能跳转到JSP页面

如何给JSP页面加载资源(CSS、js、image)

  1. 创建/spbootstudy/src/main/webapp/static文件夹,所有的静态资源(CSS、image、js)存放在该目录下面

  2. 引入静态资源需要使用绝对路径(指明根目录,从根目录查找对应的文件)

<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title heretitle>


<script type="text/javascript" src="/static/js/jquery/jquery.min.js">script>
<script type="text/javascript" src="/static/js/vue/vue.min.js">script>
head>
<body><div id="addCustomer"><div>用户名:<input v-model="username" placeholder="请输入用户名"/>div><div>年龄:<input v-model="age" placeholder="请输入年龄"/>div><div>生日:<input v-model="birthday" placeholder="请输入生日"/>div><div><button @click="commitData">提交button>div>div>
body>
<script>
new Vue({el: '#addCustomer',data: function(){return {username:"huangbiao1",age:30,birthday:"1988-11-30",createDate:""}  },mounted: function() {console.log("mounted");},methods:{commitData: function(){var that = this;var paramObj = {username: that.username,age: that.age,birthday: that.birthday}console.log("commitData");console.dir(that.data);debugger$.ajax({method:"GET",url:"/customer/save",async:true,data: paramObj})}}
})
script>
html>


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部