首先描述一下出现错误的情景:

我刚学springmvc,想做一个登录界面的东西。然后试着写了一个controller如下:

@RequestMapping(value = "/login", method = RequestMethod.POST)

public String login( String name,

String password, Model model) {

User u = userService.login(name, password);

if (u == null)

throw new UserException("no user exist!");

if (!u.getPassword().equals(password)) {

throw new UserException("password is not right!");

}

model.addAttribute("loginUser", u);

return "redirect:/user/users";

}


然后就出现了 400 badrequest这个错误了。网上找资料,说是属性不匹配吧,于是找jsp页面的错误,没有不匹配的地方,于是添加

log4j.logger.org.springframework.web=debug到log4j日志里面,果然出现错误的地方了。究其原因是

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String show(@PathVariable int id, Model model) {
model.addAttribute("user", userService.load(id));
return "user/show";
}

因为这个mapping地址localhost:8080/xxx/user/id(int类型)的,

login的mapping地址localhost:8080/xxx/user/login(String类型)的,

所以在dispatch的时候,spring误认为是要去访问show这个方法,就造成了数据类型不匹配,也就是String型无法转变为int类型的错误,也就造成了400 bad request错误了,如此,解决方法就是在show方法的

@RequestMapping(value = "/{id}", method = RequestMethod.GET)改为

@RequestMapping(value = "show/{id}", method = RequestMethod.GET)这样就区别开了。

到此,第一个问题解决。方法是

  1. 先检查一下数据类型是否匹配。
  2. 自己检查不到,就用控制台输出一个debug信息,然后查看,Springmvc中log4j.logger.org.springframework.web=debug到log4j日志里面
  3. 查看信息,修改即可。

405 Request method ‘GET’ not supported

这个错误,纯属自己的问题了,我也查看了jsp页面提交方法是post,然后controller的方法也是method = RequestMethod.POST,怎么会出现GET请求呢?原来是我直接访问login页面。

我在controller中写了一个方法,将视图转到login页面就解决了

如下:

@RequestMapping(value="",method=RequestMethod.GET)

public String index(){

return "user/login";

}

另,附上我在网上查找资料的时候,别人遇到的一些错误原因:

@Controller
@RequestMapping("/newPost.htm")
public class NewPostController{ @Autowired
PostsBusinessDelegate postsBusinessDelegate = null; @Autowired
AddNewPostFormBean addNewPostFormBean = null; @RequestMapping(method=RequestMethod.GET)
public String getCreateForm(Model model) {
model.addAttribute(addNewPostFormBean);
return "/newPost.htm";
} @RequestMapping(method=RequestMethod.POST)
public String create(AddNewPostFormBean article, BindingResult result, HttpServletRequest request) {
if (result.hasErrors()) {
return "/newPost.htm";
}
postsBusinessDelegate.addNewPost((User) request.getSession().getAttribute("CURRENT_USER"), article);
return "redirect:/index.htm";
} @RequestMapping(***method=RequestMethod.GET***)
public String getView(Model model) {
AddNewPostFormBean anpfb = this.addNewPostFormBean;
if (anpfb == null) {
return "/newPost.htm";
}
model.addAttribute(anpfb);
return "/newPost.htm";
} @ExceptionHandler(Exception.class)
public String exception(Throwable t)
{
return "/index.htm";
}
<form:form method="post" >
<form:errors path="*"/>
<p>Title: <form:input path="articleTitle" size="45" maxlength="60"/></p>
<form:select path="categoryId">
<form:option value="Select Category..."></form:option>
<c:forEach items="${sessionScope.CATEGORIES}" var="category" varStatus="index">
<form:option value="${category.categoryId}" label="${category.categoryName}"></form:option>
</c:forEach>
</form:select>
<form:textarea path="articleText" id="editor1"/>
<div class="buttons">
<input type="submit" value="Add Post" />
</div>
</form:form>

这个问题主要出现在有两个GET,不知道映射哪个。controller中create跟getView方法

如有错误,请指出,刚学习springmvc不久。谢谢!

Spring MVC出现POST 400 Bad Request &405 Request method 'GET' not supported的更多相关文章

  1. Spring Mvc 在非controller层 实现获取request对象

    一般我们在Controller层,会编写类似这样的方法 @Controller @RequestMapping(value="/detail") public class GetU ...

  2. Spring Mvc Http 400 Bad Request问题排查

    如果遇到了Spring MVC报错400,而且没有返回任何信息的情况下该如何排查问题? 问题描述 一直都没毛病的接口,今天测试的时候突然报错400 Bad Request,而且Response没有返回 ...

  3. 戏说 Spring MVC 框架

    Spring MVC 是 Spring 框架的一部分,和 Struts 一样都是属于 web 层框架,根据项目分层的思想,一个项目基本可以分为持久层,业务层和 web 层.而 Spring MVC 主 ...

  4. spring + spring mvc + tomcat 面试题(史上最全)

    文章很长,而且持续更新,建议收藏起来,慢慢读! 高并发 发烧友社群:疯狂创客圈(总入口) 奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : 极致经典 + 社群大片好评 < Java 高并发 三 ...

  5. Spring Data MongoDB example with Spring MVC 3.2

    Spring Data MongoDB example with Spring MVC 3.2 Here is another example web application built with S ...

  6. spring mvc controller中获取request head内容

    spring mvc controller中获取request head内容: @RequestMapping("/{mlid}/{ptn}/{name}") public Str ...

  7. An overview of the Spring MVC request flow

    The Spring MVC request flow in short: When we enter a URL in the browser, the request comes to the d ...

  8. spring mvc DispatcherServlet详解之三---request通过ModelAndView中获取View实例的过程

    整个spring mvc的架构如下图所示: 上篇文件讲解了DispatcherServlet第二步:通过request从Controller获取ModelAndView.现在来讲解第三步:reques ...

  9. spring mvc DispatcherServlet详解之二---request通过Controller获取ModelAndView过程

    整个spring mvc的架构如下图所示: 上篇文件讲解了DispatcherServlet通过request获取控制器Controller的过程,现在来讲解DispatcherServletDisp ...

随机推荐

  1. dot 语法全介绍

    0. 保存 保存为 pdf:dot -Tpdf iris.dot -o iris.pdf 1. 基本 (1)无向图.有向图.子图 graph G {} // 无向图 digraph G {} // 有 ...

  2. GO语言学习(十六)Go 语言结构体

    Go 语言结构体 Go 语言中数组可以存储同一类型的数据,但在结构体中我们可以为不同项定义不同的数据类型. 结构体是由一系列具有相同类型或不同类型的数据构成的数据集合. 结构体表示一项记录,比如保存图 ...

  3. VC error link

    错误1:LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main在project-setting-link里找到pro ...

  4. Jenkins学习总结(1)——Jenkins详细安装与构建部署使用教程

    Jenkins是一个开源软件项目,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能.Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括: 1.持续的软件版本发 ...

  5. C# SocketAsyncEventArgs类

    Namespace:System.Net.Sockets Assemblies:System.Net.Sockets.dll, System.dll, netstandard.dll (Represe ...

  6. vue给对象新添加属性,一定要使用Vue.set( target, key, value )这个API来添加

    this.tagList = [{ id:1, tagName:'90后' }, { id:2, tagName:'土豪' }, { id:3, tagName:'美女' }, { id:4, tag ...

  7. JS错误记录 - fgm练习 - 函数传参

    <script> window.onload = function() { var oBtn = document.getElementsByTagName('button')[0]; v ...

  8. sublime找到成对标签(Ctrl+Shift+")

    sublime找到成对标签(Ctrl+Shift+") windows版本默认快捷键是Ctrl+Shift+" sublime text怎么突出显示成对标签 使用BracketHi ...

  9. iOS开发UI篇--一个侧滑菜单SlidingMenu

    一.简介 侧滑菜单已经成为app一个极常用的设计,不管是事务类,效率类还是生活类app.侧滑菜单因Path 2.0和Facebook为开发者熟知,国内目前也有很多流行app用到了侧滑菜单,比如QQ.网 ...

  10. 【Codeforces Round #435 (Div. 2) B】Mahmoud and Ehab and the bipartiteness

    [链接]h在这里写链接 [题意] 让你在一棵树上,加入尽可能多的边. 使得这棵树依然是一张二分图. [题解] 让每个节点的度数,都变成二分图的对方集合中的点的个数就好. [错的次数] 0 [反思] 在 ...