【发布时间】:2017-05-27 19:01:39
【问题描述】:
你好有问题有错误
WARNING [http-nio-8080-exec-8] org.springframework.web.servlet.PageNotFound.handleHttpRequestMethodNotSupported Request method 'POST' not supported
当我试图在控制器上发帖时。我认为问题出在 Spring Security 中,我已将其添加到我的项目中。在它运作良好之前。
<form id="estimationForm" method="post" class="form-horizontal">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4><i class="fa fa-address-card-o" aria-hidden="true"></i> 평가</h4>
</div>
<!-- Estimate modal -->
<div class="modal-body">
<div class="form-group">
<div class="col-lg-12 text-left">
<input type="text" name="name" class="form-control form-block" placeholder="이름" required />
</div>
</div>
<div class="form-group">
<div class="col-lg-12 text-left">
<input type="email" name="email" class="form-control form-block" placeholder="이메일" required />
</div>
</div>
<div class="form-group">
<div class="col-lg-12 text-left">
<textarea class="form-control form-block" name="message" rows="4" placeholder="아이디" required></textarea>
</div>
</div>
</div>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<!-- Estimate modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">닫기</button>
<button type="button" onclick="estimate()" class="btn btn-primary">보내기</button>
</div>
</form>
这是我的 js `function estimate() {
var loginForm = document.forms["estimationForm"];
var name = loginForm.name.value;
var email = loginForm.email.value;
var message = loginForm.message.value;
alert(name +" " + email + " " + message);
$.ajax({
type: "POST",
data: {name: name , email: email , message: message},
url: "estimate",
dataType: "json",
success: function(data) {
if(data.url == "ok") {
alert("message have been sent")
} else if (data.url == ""){
alert("fail")
}
},
});
}`
这是我的控制器
@Controller
@SessionAttributes("roles")
public class IndexPageController {
@Autowired
AuthenticationTrustResolver authenticationTrustResolver;
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String loginPage(Locale locale, Model model) {
/* // add parametrized message from controller
String welcome = messageSource.getMessage("welcome.message", new Object[]{"John Doe"}, locale);
model.addAttribute("message", welcome);*/
// obtain locale from LocaleContextHolder
Locale currentLocale = LocaleContextHolder.getLocale();
model.addAttribute("locale", currentLocale);
if (isCurrentAuthenticationAnonymous()) {
return "index";
} else {
return "redirect:/list";
}
}
@ResponseBody
@RequestMapping(value = "/estimate" , method = RequestMethod.POST)
public String estimateWindow(@RequestParam("name") String recipientName, @RequestParam("email") String recipientMail, @RequestParam("message") String recipientRequestText, Model model) {
System.out.print("name " + recipientName + " email " + recipientMail + " text " + recipientRequestText);
JSONObject myJsonObj = new JSONObject();
myJsonObj.append("response", "ok");
return myJsonObj.toString();
}
/**
* This method returns true if users is already authenticated [logged-in], else false.
*/
private boolean isCurrentAuthenticationAnonymous() {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return authenticationTrustResolver.isAnonymous(authentication);
}
}
【问题讨论】:
标签: ajax spring security spring-mvc controller