【发布时间】:2015-07-16 08:21:10
【问题描述】:
我在spring-mvc 中遇到了请求映射问题:
我可以通过两个网址看到uploadPage.jsp 的回复
http://localhost:8080/dms/files/?module=abchttp://localhost:8080/dms/files?module=abc
uploadPage.jsp中的表单成功提交了url 1,浏览器中的url显示为http://localhost:8080/dms/files/upload。
但是对于 url 2,浏览器 url 错误为http://localhost:8080/dms/upload。
这个url映射有什么问题?
控制器:
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>
【问题讨论】:
-
这是 2 个不同的资源
/files不是/files/。问题是您在action属性中使用了相对 URL,而不是absoluteURL。请改用绝对 URL。您应该发帖到/dms/files/upload而不是upload。使用 URL 标签(spring 或 plain)创建正确的 URL。<sf:url value="/files/upload" var="actionUrl" />然后在您的表单元素中使用action="${actionUrl}"。
标签: spring-mvc java spring jsp spring-mvc request-mapping