springboot jsp引入和配置

如何配置JSP页面

  1. 在pom.xml文件中添加如下配置
<!-- 允许JSP页面的功能 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-tomcat</artifactId>
	<scope>provided</scope>
</dependency>
<dependency>
	<groupId>org.apache.tomcat.embed</groupId>
	<artifactId>tomcat-embed-jasper</artifactId>
	<scope>provided</scope>
</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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<!-- <link rel="stylesheet" href="/static/js/antd/antd.min.css" /> -->
<!-- 使用绝对路径引入资源文件,资源文件在webapp/static目录下面 -->
<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值