web编程jsp小tips

jsp文件

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

web资源路径问题

是不是感觉写${pageContext.request.contextpath}/很长,比较费劲,有没有什么简写的方法呢?

现提供两种解决办法

方法

  1. 存储的方式:
    在jsp文件开头,用一段Java代码将项目的路径存到pageContext域中,像下面那样
<%pageContext.setAttribute("appPath",request.getcontextpath()); %>
  1. 取出的方式为:${appPath}/
<link rel="stylesheet" type="text/css" href="${appPath }/static/H-ui/css/H-ui.min.css" />

方式二

  1. 存储的方式:还是用一段Java代码,用一个简单的字符串来接收项目路径
<%string appPath = request.getcontextpath()+"/"; %>
  1. 取出的方式为:<%=appPath%>
<link rel="stylesheet" type="text/css" href="<%=appPath %>static/H-ui/css/H-ui.min.css" />

注意:
HttpServletRequest request.getcontextpath()得到的web项目路径是不带/的,例如/ssm-crm

pageHelper分页插件使用

  1. 加入jar包:核心jar包pageHelper-5.1.2.jar和依赖包jsqlparser-1.0.jar
  2. 在mybatis核心配置文件增加注册插件的配置
<!-- pageHelper分页插件注册 -->
<plugins>
    <plugin interceptor="com.github.pageHelper.PageInterceptor">
        <!-- 分页参数合理化 -->
        <property name="resonable" value="true" />
    </plugin>
</plugins>
  1. Controller中使用方式
@RequestMapping(value = "/custs")
public String getCusts(@RequestParam(value = "pn",defaultValue = "1") Integer pn,Model model) {
    // 调用pageHelper,启用分页查询,10为每页显示的记录数,也可以从页面传入,此时需要增加方法的参数 @RequestParam(value = "limit",defaultValue = "10")
    // 这行代码一定要放在第一句,否则不能进行分页查询
    pageHelper.startPage(pn,10);

    List<Customer> list = customerService.getAll();

    // 5为每页连续显示的页数
    PageInfo<Customer> pageInfo = new PageInfo<>(list,5);

    model.addAttribute("pageInfo",pageInfo);

    return "customer-list";
}

相关文章

vue阻止冒泡事件 阻止点击事件的执行 &lt;div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些