【SpringMVC问题】springmvc jsp页面跳转到controller丢失项目名导致404

3 篇文章 0 订阅

问题

在写前台测试文件上传时,jsp跳转controller时出现404错误,查看跳转的链接里丢失了项目名
在这里插入图片描述
在这里插入图片描述

具体代码如下

jsp代码

<%@page pageEncoding="UTF-8" language="java" contentType="text/html;UTF-8" %>

<form action="/fileupload" method="post" enctype="multipart/form-data">
    <%--文件上传表单的name属性值一定要与controller处理器中方法的参数对应,否则无法实现文件上传--%>
    上传LOGO:<input type="file" name="file"/><br/>
    上传照片:<input type="file" name="file1"/><br/>
    上传任意文件:<input type="file" name="file2"/><br/>
    <input type="submit" value="上传"/>
</form>

controller代码

package com.cw.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;

@Controller
public class FileUploadController {

    @RequestMapping("/fileupload")
    public String fileupload() throws IOException {
        System.out.println("file upload is running ...");
        return "page.jsp";
    }
}

tomcat配置

在这里插入图片描述
在这里插入图片描述

解决方案(如想简单粗暴直接看方法四)

跳转链接前加"/"会返回到上一级,即会丢失项目名称,导致跳转出现的页面空白。

如果你的页面上的路径不以/开头,则认为是相对路径,默认会自动加上上个页面请求的路径

方法一:将Application context设置为/

如果jsp中请求参数要保留"/“的情况下(),可以配置tomcat,Deployment里的Application context只填写”/",此时,跳转链接加不加上"/"都可以
tomcat配置
在这里插入图片描述
jsp代码

<%@page pageEncoding="UTF-8" language="java" contentType="text/html;UTF-8" %>
<!-- 如果Application context配置为/,则这里的action中/fileupload,/加不加都可以-->
<form action="/fileupload" method="post" enctype="multipart/form-data">
    <%--文件上传表单的name属性值一定要与controller处理器中方法的参数对应,否则无法实现文件上传--%>
    上传LOGO:<input type="file" name="file"/><br/>
    上传照片:<input type="file" name="file1"/><br/>
    上传任意文件:<input type="file" name="file2"/><br/>
    <input type="submit" value="上传"/>
</form>

方法二:去掉请求参数中的/,Application context设置为项目名

如果jsp中请求参数不加"/",Deployment里的Application context要填写"/自定义名称"。
在这里插入图片描述
jsp代码

<%@page pageEncoding="UTF-8" language="java" contentType="text/html;UTF-8" %>
<!-- 如果Application context配置为项目路径,则这里的action中fileupload,/一定不能加,因为跳转链接前加"/"会返回到上一级,即会丢失项目名称,导致跳转出现的页面空白。-->
<form action="fileupload" method="post" enctype="multipart/form-data">
    <%--文件上传表单的name属性值一定要与controller处理器中方法的参数对应,否则无法实现文件上传--%>
    上传LOGO:<input type="file" name="file"/><br/>
    上传照片:<input type="file" name="file1"/><br/>
    上传任意文件:<input type="file" name="file2"/><br/>
    <input type="submit" value="上传"/>
</form>

方法三:前面添上完整路径

如果不想对现有tomcat配置进行修改,则可以在jsp页面上添加以下语句

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

<!--该语句需要放到head标签里-->
<base href="<%=basePath%>">

这个语句是用来拼接当前网页的相对路径的。

  1. request.getSchema();可以返回当前页面所使用的协议,就是"http"
  2. request.getServerName();返回当前页面所在服务器的名字,就是上面例子中的"localhost"
  3. request.getServerPort();返回当前页面所在服务器的端口号,就是上面例子中的"8080"
  4. request.getContextPath();返回当前页面所在的应用的名字,就是上面例子中的"spring09"

basePath全路径显示则为:http://localhost:8080/spring09/
path为/spring09

注意action这个加了“/ ,servlet 会根据这个,将fileupload认为是项目名,去找fileupload项目下的这个页面,当然无法跳转显示,只要去掉“/”,写成 action=“fileupload”即可

<%@page pageEncoding="UTF-8" language="java" contentType="text/html;UTF-8" %>
<%
    String path = request.getContextPath();
    System.out.println("path = " + path);
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    System.out.println("basePath = " + basePath);
%>
<html>
<head>
    <base href="<%=basePath%>">
</head>
<body>
<form action="fileupload" method="post" enctype="multipart/form-data">
    <%--文件上传表单的name属性值一定要与controller处理器中方法的参数对应,否则无法实现文件上传--%>
    上传LOGO:<input type="file" name="file"/><br/>
    上传照片:<input type="file" name="file1"/><br/>
    上传任意文件:<input type="file" name="file2"/><br/>
    <input type="submit" value="上传"/>
</form>
</body>
</html>



这样设置后页面能够成功跳转
在这里插入图片描述

方式四:补充上项目名

前端如果用的是jsp,直接在请求参数前加上${pageContext.request.contextPath},如${pageContext.request.contextPath}/setting/style.css,补上项目名,这种方式简单粗暴

<%@page pageEncoding="UTF-8" language="java" contentType="text/html;UTF-8" %>
<form action="${pageContext.request.contextPath}/fileupload" method="post" enctype="multipart/form-data">
    <%--文件上传表单的name属性值一定要与controller处理器中方法的参数对应,否则无法实现文件上传--%>
    上传LOGO:<input type="file" name="file"/><br/>
    上传照片:<input type="file" name="file1"/><br/>
    上传任意文件:<input type="file" name="file2"/><br/>
    <input type="submit" value="上传"/>
</form>
  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值