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

我刚学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不久。谢谢!

最新文章

  1. CentOS 7 配置静态IP
  2. http://kb.cnblogs.com/zt/ef/
  3. Linux查找最近修改的文件
  4. PHP太怪了,in_array() ,strpos,
  5. .net如何判断网页是否由搜索引擎蜘蛛访问?
  6. LineNumberReader类
  7. QT+QT creator+OpenCV图像灰度化
  8. 『openframeworks』shader制作三角形马赛克效果
  9. PCB Design_经验之谈
  10. python__基础 : 类的__init__,__str__,__del__方法
  11. struts2中的拦截器
  12. Hadoop问题:DataNode进程不见了
  13. jsp基础语言-jsp指令
  14. DeeplabV3+ 在自己环境下跑出现的错误
  15. Linux 命令梳理
  16. LeetCode--122、167、169、189、217 Array(Easy)
  17. Word在转PDF的过程中如何创建标签快速方便阅读(图文详解)
  18. markdown简单常用语法
  19. Ogre 编辑器二(用Ogre的地形组件加载天龙八部地形)
  20. LA 3027 合作网络

热门文章

  1. 需求:在浏览器加载完毕后,自动播放视频:出现play() failed because the user didn&#39;t interact with the document first.错误
  2. 15、python学习手册之:元组、文件及其他
  3. 最新GitHub新手使用教程(Linux/Ubuntu Git从安装到使用)——详细图解
  4. 驱动学习3-make
  5. springboot 使用FreeMarker模板(转)
  6. mysql常见故障诊断
  7. client产生CLOSE_WAIT状态的解决方式
  8. C++ BYTE数组转字符串
  9. spark提交应用的方法(spark-submit)
  10. linux下U盘状态检测