jsp
1. 引入jsp相关依赖
<!-- 引入jsp的依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- 引入tomcat的部分依赖:scope作用域,provided在进行部署项目时,不添加此jar -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
2. 在resources下创建和配置全局配置文件:application.properties
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
3. 创建web目录和视图页面:src/main/webapp/WEB-INF
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>jstl测试页面</title>
</head>
<body>
<table cellspacing="0px" border="1px">
<tr>
<th>编号</th>
<th>姓名</th>
<th>地址</th>
</tr>
<c:forEach var="test" items="${testList}">
<tr>
<td>${test.id}</td>
<td>${test.name}</td>
<td>${test.addr}</td>
<tr>
</c:forEach>
</table>
</body>
</html>
4. 创建controller
package com.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/*
* 测试jsp的jstl标签
*/
@Controller
public class JSTLController {
@RequestMapping("/index")
public String test(Model model){
// 创建集合容器
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
// 创建与前台交互内容
Map<String,String> map = new HashMap<String, String>();
map.put("id", "001");
map.put("name", "Micro");
map.put("addr", "NewYork");
Map<String,String> map2 = new HashMap<String, String>();
map2.put("id", "002");
map2.put("name", "Myth");
map2.put("addr", "Dali");
Map<String,String> map3 = new HashMap<String, String>();
map3.put("id", "003");
map3.put("name", "墨渐生微");
map3.put("addr", "Shanghai");
// 将交互内容存入容器
list.add(map);
list.add(map2);
list.add(map3);
// 将容器数据放入前台作用域
model.addAttribute("testList", list);
// 跳转页面:自动添加全局文件中的配置
return "index";
}
}
thymeleaf
1. 引入thymeleaf的启动器
<!-- 引入thymeleaf模块依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2. 创建存放视图的目录和视图: resources/templates
<!DOCTYPE html>
<html>
<head>
<title>测试thymeleaf</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1 style="color:red;">标签测试</h1>
<h2>th:text 输出数据表达式</h2>
<p th:text="hello"></p>
<p th:text="${test}"></p>
<h1>th:value 赋值给input标签 的value</h1>
<input type="text" value="000" th:value="thymeleaf赋值" />
<h2>th:if 条件判断标签</h2>
<p th:if="${value} gt 18">大于18</p>
<p th:if="${value} lt 66">小于66</p>
<h2>th:each 循环标签</h2>
<p th:each="e : ${list}">
<p th:text="${e}"></p>
</p>
<hr /><br /><br />
<h1 style="color:red;">内置对象测试格式化日期</h1>
<h2>以浏览器默认日期格式进行格式化</h2>
<p th:text="${#dates.format(date)}"></p>
<h2>自定义日期格式化</h2>
<p th:text="${#dates.format(date,'yyyy-MM-dd HH:mm:ss')}"></p>
<hr /><br /><br />
<h1 style="color:red;">内置对象测试字符串</h1>
<h2>判断字符串是否为空</h2>
<p th:text="${#strings.isEmpty(str)}"></p>
<h2>判断字符串是否包含指定的子串</h2>
<p th:text="${#strings.contains(str,'f')}"></p>
<h2>判断当前字符串是否以子串开头</h2>
<p th:text="${#strings.startsWith(str,'a')}"></p>
<h2>判断当前字符串是否以子串结尾</h2>
<p th:text="${#strings.endsWith(str,'a')}"></p>
<h2>返回字符串的长度</h2>
<p th:text="${#strings.length(str)}"></p>
<h2>查找子串的位置</h2>
<p th:text="${#strings.indexOf(str,'h')}"></p>
<h2>截取子串</h2>
<p th:text="${#strings.substring(str,3)}"></p>
<p th:text="${#strings.substring(str,3,5)}"></p>
<h2>字符串转大小写</h2>
<p th:text="${#strings.toUpperCase(str)}"></p>
<p th:text="${#strings.toLowerCase(str)}"></p>
</body>
</html>
3. 创建controller
package com.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/*
* 测试thymeleaf模块引擎
*/
@Controller
public class ThController {
@RequestMapping("/index")
public String test(Model model){
// 为前台页面测试标签进行准备
model.addAttribute("test", "测试thymeleaf模块引擎");
model.addAttribute("value",30);
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
model.addAttribute("list", list);
// 为前台测试内置对象进行准备
model.addAttribute("str", "abcdefg");
model.addAttribute("date", new Date());
/*
* thymeleaf设置前缀,默认路径就是当前main/resources/templates
* prefix: classpath:/templates/
* suffix: .html
*/
return "index";
}
}
JSON数据
<!--引入fastjson依赖库 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
package com.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
/*
* 测试阿里巴巴json字符串转换
*/
@Controller
public class JSONController {
@ResponseBody
@RequestMapping("/json")
public String json() {
// 创建集合容器
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
// 创建与前台交互内容
Map<String, String> map = new HashMap<String, String>();
map.put("id", "001");
map.put("name", "Micro");
map.put("addr", "NewYork");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("id", "002");
map2.put("name", "Myth");
map2.put("addr", "Dali");
Map<String, String> map3 = new HashMap<String, String>();
map3.put("id", "003");
map3.put("name", "墨渐生微");
map3.put("addr", "Shanghai");
// 将交互内容存入容器
list.add(map);
list.add(map2);
list.add(map3);
// 处理json格式数据
String json = JSONObject.toJSONString(list);
return json;
}
}
静态资源
默认资源目录classpath:下面的static目录:如resources/static
静态资源可以放置在 src/main/webapp目录下, 该目录名称必须是webapp
自定义异常处理页面
src/main/resources/templates 目录下创建 error.html 页面。注意:名称必须叫 error,必须有thymeleaf模块支持
<!DOCTYPE html>
<html>
<head>
<title>error.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>出现未知错误,请与管理员联系</h1>
</body>
</html>
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/*
* 测试异常跳转界面
*/
@Controller
@RequestMapping("/test")
public class ErrorController {
@RequestMapping("/testError")
public String test(){
/*
* 程序会发生算数异常,会自动跳转到templates下的error.html页面
* 错误页面必须是html5版本的页面,否则会发生500服务器内部错误
*/
System.out.println(1/0);
return "index";
}
}
局部异常
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/*
* 局部处理异常:
*/
@Controller
public class Test {
@RequestMapping("/test")
public String test(){
System.out.println(1/0);
return "index";
}
// 声明该方法为异常处理类,并指明处理的异常类型
@ExceptionHandler(value=java.lang.ArithmeticException.class)
public ModelAndView handleException(Exception e){
/* 异常方法返回类型必须为ModelAndView
* 使用形参接收处理的异常
*/
ModelAndView mv = new ModelAndView();
// 设置异常信息
mv.addObject("erroe",e.getMessage());
// 设置跳转页面
mv.setViewName("error");
return mv;
}
}
全局异常
package com.controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
/*
* 全局异常处理:
* 可以声明多个异常处理方法,用来处理所有controller层的异常
*/
// 注解声明为全局异常处理类
@ControllerAdvice
public class GlobaException {
// 声明该方法为异常处理类,并指明处理的异常类型
@ExceptionHandler(value = {java.lang.ArithmeticException.class})
public ModelAndView handleException(Exception e) {
/*
* 异常方法返回类型必须为ModelAndView 使用形参接收处理的异常
*/
ModelAndView mv = new ModelAndView();
// 设置异常信息
mv.addObject("erroe", e.getMessage());
// 设置跳转页面
mv.setViewName("error");
return mv;
}
}