[英]No mapping found even though it exists in the Spring MVC Controller
I have controller EnvironmentController
as follows 我有如下的控制器
EnvironmentController
@Controller
@RequestMapping(value = "/dashboard/environments")
public class EnvironmentController {
/*
*Other methods
*/
@RequestMapping(value = "/${envId}/addKey/${region}", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody String addKey(@PathVariable("envId") Long envId,
@PathVariable("region") String region, HttpSession session) {
if (session.getAttribute("loggedin") != null
&& (Boolean) session.getAttribute("loggedin") == true) {
DUser user = (DUser) session.getAttribute("user");
List<Role> roles = roleDao.getRoles(userDao.getUserByEmail(user
.getEmail()));
// other codes
} else {
session.invalidate();
return "{unauthorized}";
}
return "{}";
}
}
In my JSP page I am posting the data using jQuery, the code being. 在我的JSP页面中,我正在使用jQuery发布数据,代码为。
$("#create").on('click',function() {
alert($("#region").val());
$.ajax({
type : "POST",
url : "${pageContext.request.contextPath}/dashboard/environments/${envId}/addKey/"+$("#region").val(),
data : $("#region").val(),
success : success
});
});
RootContext of the web app being myapp
Web应用程序的RootContext是
myapp
I can see in the web browser console that the data is POST
ed to http://localhost:8080/myapp/dashboard/environments/2/addKey/us-east-1
. 我可以在Web浏览器控制台中看到将数据
POST
到http://localhost:8080/myapp/dashboard/environments/2/addKey/us-east-1
。
Also in the logs I can see the following INFO: Mapped "{[/dashboard/environments/${envId}/addKey/${region}],methods=[POST],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}" onto public java.lang.String com.myapp.controller.EnvironmentController.addKey(java.lang.Long,java.lang.String,javax.servlet.http.HttpSession)
同样在日志中,我可以看到以下
INFO: Mapped "{[/dashboard/environments/${envId}/addKey/${region}],methods=[POST],params=[],headers=[],consumes=[],produces=[application/json],custom=[]}" onto public java.lang.String com.myapp.controller.EnvironmentController.addKey(java.lang.Long,java.lang.String,javax.servlet.http.HttpSession)
When I do POST
to the url, it get warning 当我执行
POST
到URL时,它得到警告
WARNING: No mapping found for HTTP request with URI [/myapp/dashboard/environments/2/addKey/us-east-1] in DispatcherServlet with name 'dispatcher'
I cannot figure out why is addKey
method is not called when I post data to it and that the mapping is incorrect. 我无法弄清楚为什么在
addKey
发布数据时未调用addKey
方法,并且映射不正确。
Thanks. 谢谢。
Your request mapping is wrong. 您的请求映射是错误的。 The path variable placeholders should not contain
$
. 路径变量占位符不应包含
$
。 So the correct form is @RequestMapping(value = "/{envId}/addKey/{region}", ...)
. 因此正确的格式为
@RequestMapping(value = "/{envId}/addKey/{region}", ...)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.