NutzCN Logo
问答 ${base}应该重新调整才更实用
发布于 3047天前 作者 qq_a1b6f073 2586 次浏览 复制 上一个帖子 下一个帖子
标签: mvc
    您好!
    在nutz 1.b.53版本中${base}的取值为

    ```
    req.setAttribute("base", req.getContextPath());
    ```
    这样的取值结果我觉得不实用,一般项目中引用js如下:

    ```
    <link href="${base}/styles/plugins/hplus/css/bootstrap.min.css" rel="stylesheet">
    ```
    所以,防止出错,我建议修改为如下:

    ```
    String base = request.getServerName()
     + (request.getServerPort() == 80 ? "" : request.getServerPort())
     + request.getContextPath();
    ```
7 回复

这写法能预防啥?

举个现有写法会出错,你的写法才能正常的场景?

@wendal 在jsp页面中,普通的用法,如加载css、js应该没有问题,假如是在js中作为变量的值(资源路径前缀),不会有问题吗?

@wendal 好吧,还没试错成功~~~~(>_<)~~~~
以前在spring框架中的做法是

<c:set var="base" value="${pageContext.request.scheme}://${pageContext.request.serverName }:${pageContext.request.serverPort}${pageContext.request.contextPath}"/>

根本就不会有问题,绝对路径VS相对路径

@kerbores 都是绝对路径,他的写法是加上域名和端口,多此一举, cdn的时候死翘翘

添加回复
请先登陆
回到顶部