<span th:text="${request1}">测试数据测试数据</span>
<hr>
[[${request1}]]
<hr>
<span th:text="${session.session1}">测试数据测试数据</span>
<hr>
[[${session.session1}]]
<hr>
<span th:text="${#servletContext.getAttribute('context1')}">测试数据</span>
<hr>
[[${#servletContext.getAttribute('context1')}]]
<hr>
<input type="text" value="aaaa" th:value="${request1}">
<script src="/js/jquery-3.2.1.js" th:src="@{/js/jquery-3.2.1.js}"></script>
<div th:fragment="topFragment">
公共的top页面
</div>
<div th:fragment="bottomFragment">
公共的bottom页面
</div>
<div th:insert="~{common :: topFragment}"></div>
<div th:replace="~{common :: bottomFragment}"></div>
common是要引入模块的文件名。
属性:
<table>
<tr>
<th>seq</th>
<th>id</th>
<th>name</th>
<th>phone</th>
<th>操作</th>
</tr>
<tr th:each="user,i:${ul}" th:if="${i.count%2!=0}">
<td>[[${i.count}]]</td>
<td>[[${user.uid}]]</td>
<td th:text="${user.uname}"></td>
<td>[[${user.uphone}]]</td>
<td>
<a th:href="@{/user/del(uid=${user.uid},uname=${user.uname})}">删除</a>
</td>
</tr>
</table>
<a th:href="@{/user/del(uid=${user.uid},uname=${user.uname})}">删除</a>
var uid=12;
var url = "[[@{/user/del}]]?uid="+uid;
针对于同步开发使用。不能达到返回的前端不同的错误信息(比如token过期,密码有误等等自定义的错误信息).
使用浏览器测试请求:返回的是错误页面。
使用postman测试:返回的是json数据。
异常处理机制:
ErrorMvcAutoConfiguration配置类中有三个核心的异常处理的bean:
1.BasicErrorController:映射路径/error,出现异常tomcat会把请求转到/error的处理器中。
errorHtml:浏览器请求的处理器方法,查找错误视图,如果查找不到,则使用viewName为error的错误视图。
error:异步请求的处理器方法,直接返回json数据。
2.DefaultErrorAttributes:该对象中getErrorAttributes方法,生成了一个Map,该集合中包含了固定的错误信息。
3.StaticView默认错误视图,该视图的名字叫:error
4.在四个静态资源路径下定义error/错误码.html|error/4xx.html,5xx.html
5.怎么扩展错误信息,从DefaultErrorAttributes派生子类,注入容器,替换掉默认的bean对象。
日常开发中,仍然使用全局统一异常处理:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
@MapperScan("com.javasm.mapper")
@EnableTransactionManagement
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.14.241:3306/crm?characterEncoding=UTF8&useSSL=true&serverTimezone=Asia/Shanghai
username: root
password: root
mybatis:
type-aliases-package: com.javasm
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath:com/javasm/mapper/*.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
@SpringBootApplication
@MapperScan("com.javasm.mapper")
@EnableTransactionManagement
@EnableCaching // redis的注解识别
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<String, Object> template = new RedisTemplate();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
redis:
password: root
@SpringBootTest
class ApplicationTests {
@Resource
private RedisTemplate<String, Object> rt;
@Test
public void testRedis(){
ValueOperations<String, Object> value = rt.opsForValue();
value.set("12",new ApplySimple(1,"啦啦啦",1001));
HashOperations<String, Object, Object> opsForHash = rt.opsForHash();
opsForHash.put("hashTest","123",new ApplySimple(12,"测试",123));
}
@Test
// 手动取注解存进的值,取不出来,为null
public void testRedisGet(){
rt.setKeySerializer(new JdkSerializationRedisSerializer());
rt.setValueSerializer(new JdkSerializationRedisSerializer());
ValueOperations<String, Object> value = rt.opsForValue();
Object o = value.get("apply::10");
HashOperations<String, Object, Object> opsForHash = rt.opsForHash();
Object hashTest = opsForHash.get("hashTest", "123");
System.out.println(o);
System.out.println(hashTest);
}
}
//unless:对返回结果做判断,unless的条件为false时,加缓存,为true时不加
//condition:对查询条件做判断,条件为true时,查缓存,为false不查缓存
@Cacheable(cacheNames = "blogs",key = "#bid",unless = "#result==null",condition = "#bid>0")
@Override
public Blog selectById(Integer bid) {
Blog blog = bm.selectByPrimaryKey(bid);
return blog;
}
@CacheEvict(cacheNames = "blogs",key = "#b.bid")
@Override
public boolean updateBlogById(Blog b) {
return bm.updateByPrimaryKeySelective(b)==1?true:false;
}
因篇幅问题不能全部显示,请点此查看更多更全内容