zoukankan      html  css  js  c++  java
  • 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";
    }
    
  • 相关阅读:
    wcf连接数据库用sqlhelper,连接数一直没有释放反而增加
    Assembly.GetManifestResourceStream为null
    webapi <Message>已拒绝为此请求授权。</Message>
    未找到与名为“xxx”的控制器匹配的类型。
    Eclipse Ctrl+鼠标左键不能查看源代码
    WEB API 用MemoryStream流做下载功能
    mysql繁字体报错,Incorrect string value: 'xE9_' for column 'UserName' at row 1
    学信网模拟登录2
    选择排序
    mysql学习笔记
  • 原文地址:https://www.cnblogs.com/zxfei/p/11477836.html
Copyright © 2011-2022 走看看