springmvvc05

springmvc注解式开发

@RequestMapping 定义请求规则


放在类上面使用
/*
* @RequestMapping
* value:所有请求的公共部分,叫做模块名称
* 位置:在类的上面
* */

@Controller
@RequestMapping("/user")
//或者@RequestMapping("/test")
public class MyController {

@RequestMapping(value = "/some.do")
public ModelAndView doSome(){
ModelAndView mv = new ModelAndView();
mv.addObject("msg","欢迎使用springmvc做web开发");
mv.addObject("fun","执行的是doSome方法");
mv.setViewName("show");
return mv;

}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

理解为在该方法在 user或者test模块下


method 属性 指定请求方式
@Controller
@RequestMapping("/test")
public class MyController {
/*
* @RequestMapping(value = "",method = )
* 属性:method 表示请求的方式,它的值是RequestMethod类枚举值
*
* get请求方式, method = RequestMethod.GET
* post请求方式,method = RequestMethod.POST
*
* */

//指定some.do用get请求获取
@RequestMapping(value = "/some.do",method = RequestMethod.GET)
public ModelAndView doSome(){
ModelAndView mv = new ModelAndView();
mv.addObject("msg","欢迎使用springmvc做web开发");
mv.addObject("fun","执行的是doSome方法");
mv.setViewName("show");
return mv;
}

//指定other.do 用post请求获取
@RequestMapping(value = "/other.do",method = RequestMethod.POST)
public ModelAndView doOther(){
ModelAndView mv = new ModelAndView();
mv.addObject("msg","---------欢迎使用springmvc做web开发----------");
mv.addObject("fun","执行的是doOther方法");
mv.setViewName("other");
return mv;
}

对应的index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<p>第一个springmvc项目</p>
<p><a href="test/some.do">发起some.do的get请求</a></p>
<br/>

<form action="test/other.do" method="post">
<input type="submit" value="post请求other.do">
</form>

<%-- <p><a href="user/other.do">发起other.do请求</a></p>--%>
</body>
</html>

原文地址:https://www.cnblogs.com/huaobin/p/14908507.html