微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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";
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐