为啥老师没有<%=request.getContextPath()%>就不报错?

来源:4-6 FileUpload--单文件上传

键盘兔

2016-04-05 15:21

404 /courses/doUpload 资源不存在,表单action属性值前面加上<%=request.getcontextpath()%>就正常了,而教程里是没加<%=request.getcontextpath()%>的真实奇怪,教程里添加课程有<%=request.getcontextpath()%>,上传文件没有<%=request.getcontextpath()%>结果都对。自己的代码确必须有<%=request.getcontextpath()%>。真不知道什么原因?

写回答 关注

2回答

  • YuriLee
    2016-04-15 01:51:19
    已采纳

    1、建议使用base标签

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

    <%

    String path = request.getContextPath();

    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

    %>

    <html>

      <head>

    <base href="<%=basePath%>">

    </head>

    2、之后,你在页面上访问的所有资源,都会默认加上basePath的前缀。避免再次出现404的错误

    键盘兔

    非常感谢!

    2017-08-31 11:50:25

    共 1 条回复 >

  • 莨菽菽
    2017-08-14 11:40:54

    我也很奇怪。。你知道原因了么?而且为什么不加的话就会报错呢?

Spring MVC起步

Java中Spring MVC框架入门教程,快来看最易用的MVC框架

195980 学习 · 572 问题

查看课程

相似问题