美文网首页
分布式商城项目总结二之整合静态页面

分布式商城项目总结二之整合静态页面

作者: MisterDo | 来源:发表于2019-10-19 15:44 被阅读0次

2.7.3 整合静态页面

  • 将准备好的静态页面放入WEB-IF下


    静态资源.PNG
  • 由于在web.xml中定义的url拦截形式为“/”表示拦截所有的url请求,包括静态资源例如css、js等。所以需要在springmvc.xml中添加资源映射标签

    <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>
    <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>

2.8 修改taotao-manager-mapper的pom文件

在pom文件加入如下内容:

<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉,mapper文件由mapperGenerator工具生成 -->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

2.9 整合测试

2.10 页面初始化

如果不使用tomcat插件,可以利用idea对tomcat进行配置


欢迎页.PNG

注意:
这里之所以访问localhost:8080/index会自动返回一个index.jsp页面,是因为在web.xml中配置了欢迎页,

<welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
</welcome-file-list>

欢迎页的功能类似于一个如下的controller:

@Controller
public class indexController {
     @RequestMapping("/index")
     public String showIndex(){
        return "index";
     }

注意:
web.xml中关于DispatcherServlet的url路径配置<url-pattern>/</url-pattern>,其实有很多讲究,如果url-pattern设置为/,则所有的静态资源都会被拦截,但不会影响jsp,对静态资源的访问都会除去后缀名,处理为对controller的调用。

2.11页面的切换

@Controller
public class PageController {
    //打开首页
    @RequestMapping("/")
    public String showIndex(){
        return "index";
    }
    //展示其他页,儒新增商品,查询商品等等
    @RequestMapping("/{page}")
    public String showPage(@PathVariable String page){
        return page;
    }
}

3. 商品列表查询

3.1 商品列表页面

查询商品列表.PNG
红色画圈部分表示请求对应的jsp为item-list.jsp,对应于2.11 PageControlller中的@RequestMapping("/{page}")

在加载出item-list.jsp,页面发起一个url为/item/list的get请求,请求参数为page和rows,为EasyUI自带的两个参数在item-list.jsp中table的标签上可以看到,如下,

<table class="easyui-datagrid" id="itemList" title="商品列表" 
       data-options="singleSelect:false,collapsible:true,pagination:true,url:'/item/list',method:'get',pageSize:30,toolbar:toolbar">

客户端发起get请求:


商品列表请求.PNG

服务端响应回json数据:


商品列表返回结果.PNG

请求返回的数据必须为以下类型的实体转为的json字符串,EasyUI会根据这种格式的json字符串,自动解析json数据,渲染到页面,渲染结果即为上图查询商品列表所示

public class EUDataGridResult {
    private long total;
    private List<?> rows;
    public long getTotal() {
        return total;
    }
    public void setTotal(long total) {
        this.total = total;
    }
    public List<?> getRows() {
        return rows;
    }
    public void setRows(List<?> rows) {
        this.rows = rows;
    }
}

其中rows列表中的实体类属性要与item-list.jsp的table标签中filed属性名一一对应,如下:
TbItem实体:

public class TbItem {
    private Long id;
    private String title;
    private String sellPoint;
    private Long price;
    private Integer num;
    private String barcode;
    private String image;
    private Long cid;
    private Byte status;
    private Date created;
    private Date updated;
//set和get方法省略。。。
}

item-list.jsp的table标签中filed属性:

<th data-options="field:'ck',checkbox:true"></th>
            <th data-options="field:'id',width:60">商品ID</th>
            <th data-options="field:'title',width:200">商品标题</th>
            <th data-options="field:'cid',width:100">叶子类目</th>
            <th data-options="field:'sellPoint',width:100">卖点</th>
            <th data-options="field:'price',width:70,align:'right',formatter:TAOTAO.formatPrice">价格</th>
            <th data-options="field:'num',width:70,align:'right'">库存数量</th>
            <th data-options="field:'barcode',width:100">条形码</th>
            <th data-options="field:'status',width:60,align:'center',formatter:TAOTAO.formatItemStatus">状态</th>
            <th data-options="field:'created',width:130,align:'center',formatter:TAOTAO.formatDateTime">创建日期</th>
            <th data-options="field:'updated',width:130,align:'center',formatter:TAOTAO.formatDateTime">更新日期</th>

当Controller返回一个EUDataGridResult对象时,@ResponseBoby会自动将其转为json格式的字符串,


@Controller
public class ItemController {

    @Autowired
    ItemService itemService;

    @RequestMapping("/item/list")
    @ResponseBody
    public EUDataGridResult getItemList(Integer page,Integer rows){
        EUDataGridResult result = itemService.getItemList(page,rows);
        return result;
    }
}

service的实现类中的getItemList方法,分页利用MyBatis的一个插件PageHelper,但是注意该插件对联合查询不友好

@Override
    public EUDataGridResult getItemList(int page, int rows) {
        //查询商品列表
        TbItemExample example = new TbItemExample();
        //分页处理
        PageHelper.startPage(page, rows);
        List<TbItem> list = itemMapper.selectByExample(example);
        //创建一个返回值对象
        EUDataGridResult result = new EUDataGridResult();
        result.setRows(list);
        //取记录总条数
        PageInfo<TbItem> pageInfo = new PageInfo<>(list);
        result.setTotal(pageInfo.getTotal());
        return result;
    }

3.3 分页处理

3.3.1 Mybatis分页插件 - PageHelper说明

如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件。该插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库分页。

3.3.2 使用方法

第一步:在Mybatis配置xml中配置拦截器插件:

<plugins>
    <!-- com.github.pagehelper为PageHelper类所在包名 -->
    <plugin interceptor="com.github.pagehelper.PageHelper">
        <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->        
        <property name="dialect" value="mysql"/>
    </plugin>
</plugins>

第二步:在代码中使用

1、设置分页信息:
    //获取第1页,10条内容,默认查询总数count
    PageHelper.startPage(1, 10);
    //紧跟着的第一个select方法会被分页
    List<Country> list = countryMapper.selectIf(1);
2、取分页信息
//分页后,实际返回的结果list类型是Page<E>,如果想取出分页信息,需要强制转换为Page<E>,
Page<Country> listCountry = (Page<Country>)list;
listCountry.getTotal();
3、取分页信息的第二种方法
//获取第1页,10条内容,默认查询总数count
PageHelper.startPage(1, 10);
List<Country> list = countryMapper.selectAll();
//用PageInfo对结果进行包装
PageInfo page = new PageInfo(list);
//测试PageInfo全部属性
//PageInfo包含了非常全面的分页属性
assertEquals(1, page.getPageNum());
assertEquals(10, page.getPageSize());
assertEquals(1, page.getStartRow());
assertEquals(10, page.getEndRow());
assertEquals(183, page.getTotal());
assertEquals(19, page.getPages());
assertEquals(1, page.getFirstPage());
assertEquals(8, page.getLastPage());
assertEquals(true, page.isFirstPage());
assertEquals(false, page.isLastPage());
assertEquals(false, page.isHasPreviousPage());
assertEquals(true, page.isHasNextPage());

相关文章

网友评论

      本文标题:分布式商城项目总结二之整合静态页面

      本文链接:https://www.haomeiwen.com/subject/gsegmctx.html