我有控制器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 "{}";
}
}
在我的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为myapp
我可以在网络浏览器控制台中看到数据POST
已编辑为http://localhost:8080/myapp/dashboard/environments/2/addKey/us-east-1
。
同样在日志中我可以看到以下内容
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)
当我对网址POST
时,会收到警告
WARNING: No mapping found for HTTP request with URI [/myapp/dashboard/environments/2/addKey/us-east-1] in DispatcherServlet with name 'dispatcher'
我无法弄清楚当我向其发布数据并且映射不正确时,为什么没有调用addKey
方法。
感谢。
答案 0 :(得分:1)
您的请求映射错误。路径变量占位符不应包含$
。所以正确的形式是@RequestMapping(value = "/{envId}/addKey/{region}", ...)
。