SpringMVC表单提交路径问题

    xiaoxiao2025-04-16  3

    最近写springMVC的一个表单的提交,jsp如下:

    <form action="servlet/GetProductId/test" method="post" >      出发城市:<input type="text" name="departCity" value="BJS"><br><br>     到达城市:<input type="text" name="arriveCity" value="WUH"><br><br>     往&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;返:     <input type="radio" name="sex" value="OW" checked>单程     <input type="radio" name="sex" value="RT" >往返<br><br>     乘客类型:     <input type="radio" name="passengerType" value="ADULT" checked>成人     <input type="radio" name="passengerType" value="CHILD" >儿童<br><br>     <!-- disabled="true -->     <input type="submit" value="提交">     <input type="reset" value="重置"> </form>

    运行时就会抛出 404 ,路径也变成了 http://localhost:8080/springMVC/views/servlet/GetProductId/test 

    这个路径显然是不对的,其中springMVC是项目名,views是jsp的文件夹。原来springMVC在action跳转时,会自动加上前面的根目录,结果就导致多了/springMVC/views。

    解决方法:

       1. 将jsp放到根目录下,这样再请求跳转时就不会再多前面的路径。

       2.在jsp前面加上下面两句话就可以获得绝对路径。

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

      在action上也加上<%=basePath%>,即:

     <form action="servlet/GetProductId/test" method="post" >

     

    转载请注明原文地址: https://ju.6miu.com/read-1298116.html
    最新回复(0)