我经常要翻资料的操作(持续更新)
jsp常用头信息
<!--c标签-->
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!--处理日期格式-->
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!--处理日期格式使用案例-->
<fmt:formatDate value="${bills.billtime}" pattern="yyyy-MM-dd"></fmt:formatDate>
JSP下拉列表
<select name="typeid">
<option value="-1">不限</option>
<!--从数据库获得数据放入下拉菜单-->
<c:forEach items="${billType}" var="type">
<!--如果返回的id等于刚刚发送的,则选择,这个判断是怕没有选内容-->
<option value="${type.id}" ${tid==type.id?'selected':''}>${type.name}</option>
</c:forEach>
</select>
JSP中if的使用
<c:if test="${bills.size()>0}">
<c:forEach items="${bills}" var="bills">
<tr>
<td>${bills.title}</td>
<td><fmt:formatDate value="${bills.billtime}" pattern="yyyy-MM-dd"></fmt:formatDate></td>
<td>${bills.billType.name}</td>
<td>
<c:choose>
<c:when test="${bills.billType.name=='支出'||bills.billType.name=='借出'||bills.billType.name=='还出'}">
-${bills.price}
</c:when>
<c:when test="${bills.billType.name=='收入'||bills.billType.name=='借入'||bills.billType.name=='还入'}">
+${bills.price}
</c:when>
<c:otherwise>
${bills.price}
</c:otherwise>
</c:choose>
</td>
<td>${bills.explains}</td>
<td>删除 修改</td>
</tr>
</c:forEach>
</c:if>
<c:if test="${bills.size()==0}">
<tr>
<td colspan="6"><h3>没有找到任何数据</h3></td>
</tr>
</c:if>
mapper.XML动态扩容SQL
select * from bills b,bill_type t where b.typeid=t.id
< if test = " tid>0" >
and t.id=#{tid}
</ if>
< if test = " begin!=null and begin!=' ' " >
and b.billtime>=#{begin}
</ if>
< if test = " end!=null and end!=' ' " >
and b.billtime<![CDATA[<=]]> #{end}
</ if>
表单合并显示
< tr>
< td colspan = " 6" > < h3> 没有找到任何数据</ h3> </ td>
</ tr>
添加默认值的操作
@RequestMapping ( "/getAllBills" )
public String getBills ( @RequestParam ( defaultValue = "1" ) int index, Integer typeid, String begin, String end, ModelMap map) {
List< Bills> bills = billsService. getBills ( typeid, begin, end) ;
List< BillType> billType = billTypeService. getBillType ( ) ;
map. addAttribute ( "bills" , bills) ;
map. addAttribute ( "billType" , billType) ;
map. addAttribute ( "tid" , typeid) ;
map. addAttribute ( "begin" , begin) ;
map. addAttribute ( "end" , end) ;
return "show" ;
}
分页插件的使用
jsp页面
<tr>
<td colspan="6">
<a href="/getAllBills?typeid=${tid}&begin=${begin}&end=${end}">首页</a>
<a href="/getAllBills?index=${info.prePage==0?1:info.prePage}&typeid=${tid}&begin=${begin}&end=${end}">上一页</a>
<a href="/getAllBills?index=${info.nextPage==0?info.pages:info.nextPage}&typeid=${tid}&begin=${begin}&end=${end}">下一页</a>
<a href="/getAllBills?index=${info.pages}&typeid=${tid}&begin=${begin}&end=${end}">尾页</a>
总页数:${info.pages}
总条数:${info.total}
</td>
</tr>
Service层
public PageInfo< Bills> getBills ( int typeid, String begin, String end, int index, int size) {
Map map = new HashMap ( ) ;
map. put ( "tid" , typeid) ;
map. put ( "begin" , begin) ;
map. put ( "end" , end) ;
PageHelper. startPage ( index, size) ;
List< Bills> bills = billsMapper. getBills ( map) ;
PageInfo< Bills> info = new PageInfo < > ( bills) ;
return info;
}
Controller层
public String getBills ( @RequestParam ( defaultValue = "1" ) int index, @RequestParam ( defaultValue = "-1" ) Integer typeid, String begin, String end, ModelMap map) {
PageInfo< Bills> info = billsService. getBills ( typeid, begin, end, index, PageUtil. PAGESIZE) ;
List< BillType> billType = billTypeService. getBillType ( ) ;
map. addAttribute ( "info" , info) ;
map. addAttribute ( "billType" , billType) ;
map. addAttribute ( "tid" , typeid) ;
map. addAttribute ( "begin" , begin) ;
map. addAttribute ( "end" , end) ;
return "show" ;
}
常用标签
< form method = " post" action = " " >
< input type = " hidden" name = " id" value = " ${bills.id}" >
< p>
类型:
< c: forEach items = " ${billType}" var = " type" >
< input type = " radio" value = " ${type.id}" name = " tid" > ${type.name}
</ c: forEach>
</ p>
< p> 标题:< input type = " text" style =" width : 500px" name = " " > </ p>
< p> 日期:< input type = " text" > 金额:< input type = " text" > </ p>
< p> 说明:< textarea cols = " 50" rows = " 4" name = " " > </ textarea> </ p>
< input type = " reset" value = " 重置" >
< input type = " submit" value = " 保存" >
</ form>
redirect重定向
return "redirect:/gettypes" ;
事务注解
@Override
@Transactional
public int insert ( Bills record) {
return billsMapper. insert ( record) ;
}
修改时选中对应单选列表
<c:forEach items="${billType}" var="type">
<input type="radio" value="${type.id}" ${type.id==bills.id?'checked':''} name="typeid">${type.name}
</c:forEach>
处理乱码及弹窗
resp. setContentType ( "text/html;charset=utf-8" ) ;
数据库配置文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
Thymeleaf回显
< div class = " form-group" >
< label for = " date2" class = " control-label" > 结束时间</ label>
< input type = " text" th: value= " ${param.date2}" class = " form-control" name = " date2" id = " date2" placeholder = " 结束时间" onclick = " WdatePicker()" />
</ div>
日志
logging :
level :
com.hsp : debug