0

I am facing an issue with the request mapping in as:
There are two url by which I am able to see the response of uploadPage.jsp

  1. http://localhost:8080/dms/files/?module=abc
  2. http://localhost:8080/dms/files?module=abc

The form in uploadPage.jsp is successfully submitted for the url 1 and the url in browser displayed as http://localhost:8080/dms/files/upload.
But for the url 2 there is an error with the browser url as http://localhost:8080/dms/upload.

What is the problem with this url mapping?

Controller:

package dms.spring.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import dms.pojo.CrmDms;

@Controller
@RequestMapping(value = "/files")
public class FileUploadController
{
    @RequestMapping(method = RequestMethod.GET)
    public String index( HttpServletRequest webRequest, ModelMap map )
    {
        String module = webRequest.getParameter( "module" );
        CrmDms crmDms = new CrmDms();
        crmDms.setModule( module );
        map.put( CrmDms.class.getSimpleName(), crmDms );
        return "uploadPage";
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String uploadFile( @ModelAttribute(value = "CrmDms") CrmDms crmDms,
                              @RequestParam(value = "document") MultipartFile file,
                              ModelMap modelMap )
    {
        System.out.println( crmDms.getModule() );
        return "successPage";
    }
}

JSP (uploadPage.jsp):

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="sf"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
  <div id="main-wrapper">
    <sf:form action="upload" method="post" commandName="CrmDms">
      <sf:input path="module" />
      <input type="submit" value="Submit">
    </sf:form>
  </div>
</body>
</html>
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
  • Those are 2 different resource `/files` isn't `/files/`. The problem is that you are using relative URLs in your `action` attribute instead of `absolute` URLs. Use absolute URLs instead. You should post to `/dms/files/upload` and not `upload`. Use the URL tag (either spring or plain) to create the correct URL. `` then in your form element use `action="${actionUrl}"`. – M. Deinum Jul 16 '15 at 08:44

2 Answers2

0

You have a common problem with relative URLs in JSP. That's the reason why the rule is generally to always use absolute URLs.

When you first use http://localhost:8080/dms/files/?module=abc, the <sf:form action="upload" method="post" ... posts as expected to http://localhost:8080/dms/files/upload and is correctly processed by your controller.

But when you use http://localhost:8080/dms/files?module=abc, the <sf:form action="upload" method="post" ... posts to http://localhost:8080/dms/upload and gives you the error.

If you want to get rid of that problem, the simplest way is to use an absolute URL in <sf:form tag, either prepending context path by hand :

<sf:url var="upload" value="/files/upload"/>
<sf:form action="${upload}" method="post" commandName="CrmDms">

or by using (Spring > 3.2.3) the servletRelativeAction attribute :

<sf:form servletRelativeAction="/files/upload" method="post" commandName="CrmDms">
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
-1

yes your url mapping is wrong here in your case Remove below line from your controller to get output from url 2

@RequestMapping(value = "/files")

because you have give a url pattern at controller level so spring container first check url pattern at controller level if it find correct url pattern at controller then after it will search for any further url pattern

Here in your case

url 2. http://localhost:8080/dms/upload

will directly search for resource having url pattern starts with upload but spring container cant found the proper mapping that why it wont give you desirable output

Nirav Prajapati
  • 2,987
  • 27
  • 32