我正在做一些关于spring security的示例应用程序。
这是我的控制者:
@Controller
public class MainController {
......
......
@RequestMapping(value = "/edit", method = RequestMethod.GET)
public ModelAndView editPage() {
ModelAndView model = new ModelAndView();
model.addObject("title", "User edit Form - Database Interaction");
model.addObject("message", "This page is for ROLE_ADMIN only!");
model.setViewName("editpage");
System.out.println("getting edit page");
return model;
}
@RequestMapping(value = "/edit", method = RequestMethod.POST)
public ModelAndView updateCredentials(@RequestParam(value= "username", required= false) String username , @RequestParam(required=false)String password) {
System.out.println("Username= "+username+" password= "+password);
ModelAndView model = new ModelAndView();
model.addObject("title", "Credential Edit Operation");
model.addObject("message", "You are successfully updated your credentials");
model.addObject("edited", "TRUE");
model.setViewName("editpage");
System.out.println("executed updateCredentials POST method");
return model;
}
}
这是我的editpage.jsp,我提交表单;
<%@page session="true"%>
<html>
<body>
<h1>Title : ${title}</h1>
<h1>Message : ${message}</h1>
<br>
<c:if test="${ edited ne 'TRUE'}">
<form action="/edit" method="POST" id="editForm">
<input type="text" name="username" />
<input type="text" name="password" />
<a href="javascript:crSubmit()" >Edit</a>
</form>
</c:if>
<c:url value="/j_spring_security_logout" var="logoutUrl" />
<form action="${logoutUrl}" method="post" id="logoutForm">
<input type="hidden" name="${_csrf.parameterName}"
value="${_csrf.token}" />
</form>
<script>
function formSubmit() {
document.getElementById("logoutForm").submit();
}
function crSubmit() {
document.getElementById("editForm").submit();
}
</script>
<c:if test="${pageContext.request.userPrincipal.name != null}">
<h2>
Welcome : ${pageContext.request.userPrincipal.name} | <a
href="javascript:formSubmit()"> Logout</a>
</h2>
</c:if>
</body>
</html>
现在我面临的问题是,当我提交表单时,我得到的URL是错误的。如下;
我正在获得404响应代码。
但项目的实际网址是
http://localhost:8080/SpringSecurity/edit
它是如何发生的?
我错在哪里?
请建议..
答案 0 :(得分:1)
您需要将当前上下文路径添加到网址(SpringSecurity
是您的上下文)。试试这个:
<form action="${pageContext.request.contextPath}/edit" method="POST" id="editForm">
....