Search code examples
javaspringjspspring-mvcrequest-mapping

request mapping issue - spring mvc


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>

Solution

  • 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">