RequestMapping注解
使用说明
-
作用:用于建立请求URL和处理请求方法之间的对应关系。
-
出现位置:
-
类上:
请求 URL的第一级访问目录。此处不写的话,就相当于应用的根目录。写的话需要以/开头。它出现的目的是为了使我们的 URL 可以按照模块化管理,例如:
账户模块:
/account/add
/account/update
/account/delete ...
订单模块:
/order/add
/order/update
/order/delete
红色的部分就是把RequsetMappding写在类上,使我们的URL更加精细。
-
方法上:
请求URL的第二级访问目录,可以窄化请求路径
-
-
属性:
value:用于指定请求的URL。它和path属性的作用是一样的。
method:用于指定请求的方式。
注意:以上属性只要出现2个或以上时,他们的关系是与的关系。
窄化路径示例
-
使用二级目录访问
<span style="background-color:#f8f8f8"><span style="color:#555555">@Controller</span> <span style="color:#555555">@RequestMapping</span>(<span style="color:#aa1111">"/account"</span>) <span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">AccountController</span> { <span style="color:#555555">@RequestMapping</span>(<span style="color:#aa1111">"/findAccount"</span>)<span style="color:#770088">public</span> <span style="color:#000000">ModelAndView</span> <span style="color:#000000">findAccount</span>() {<span style="color:#000000">ModelAndView</span> <span style="color:#000000">mv</span> <span style="color:#981a1a">=</span> <span style="color:#770088">new</span> <span style="color:#000000">ModelAndView</span>();<span style="color:#000000">mv</span>.<span style="color:#000000">addObject</span>(<span style="color:#aa1111">"msg"</span>, <span style="color:#aa1111">"欢迎你 springmvc"</span>);<span style="color:#000000">mv</span>.<span style="color:#000000">setViewName</span>(<span style="color:#aa1111">"success"</span>);<span style="color:#770088">return</span> <span style="color:#000000">mv</span>;} }</span>
-
在index.jsp里面定义超链接
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">a</span> <span style="color:#0000cc">href</span>=<span style="color:#aa1111">"/account/findAccount"</span><span style="color:#117700">></span>窄化路径<span style="color:#117700"></</span><span style="color:#117700">a</span><span style="color:#117700">></span></span>
method属性示例
-
描述需要使用指定的请求方式来请求该方法
<span style="background-color:#f8f8f8"><span style="color:#555555">@Controller</span> <span style="color:#555555">@RequestMapping</span>(<span style="color:#aa1111">"/account"</span>) <span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">AccountController</span> {<span style="color:#aa5500">//指定的请求方式</span><span style="color:#555555">@RequestMapping</span>(<span style="color:#000000">value</span> <span style="color:#981a1a">=</span> <span style="color:#aa1111">"/findAccount1"</span>, <span style="color:#000000">method</span> <span style="color:#981a1a">=</span> <span style="color:#000000">RequestMethod</span>.<span style="color:#000000">POST</span>)<span style="color:#770088">public</span> <span style="color:#000000">ModelAndView</span> <span style="color:#000000">findAccount1</span>() {<span style="color:#000000">ModelAndView</span> <span style="color:#000000">mv</span> <span style="color:#981a1a">=</span> <span style="color:#770088">new</span> <span style="color:#000000">ModelAndView</span>();<span style="color:#000000">mv</span>.<span style="color:#000000">addObject</span>(<span style="color:#aa1111">"msg"</span>, <span style="color:#aa1111">"欢迎你 springmvc"</span>);<span style="color:#000000">mv</span>.<span style="color:#000000">setViewName</span>(<span style="color:#aa1111">"success"</span>);<span style="color:#770088">return</span> <span style="color:#000000">mv</span>;} }</span>
-
测试:在index.jsp里使用get方式请求
<span style="background-color:#f8f8f8"><span style="color:#117700"><</span><span style="color:#117700">a</span> <span style="color:#0000cc">href</span>=<span style="color:#aa1111">"/account/findAccount1"</span><span style="color:#117700">></span>请求方式<span style="color:#117700"></</span><span style="color:#117700">a</span><span style="color:#117700">></span></span>
结果:
-
我们再换一种请求方式
<span style="background-color:#f8f8f8"> <span style="color:#117700"><</span><span style="color:#117700">form</span> <span style="color:#0000cc">action</span>=<span style="color:#aa1111">"account/findAccount1"</span> <span style="color:#0000cc">method</span>=<span style="color:#aa1111">"post"</span><span style="color:#117700">></span><span style="color:#117700"><</span><span style="color:#117700">input</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"submit"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"保存账户,post 请求"</span><span style="color:#117700">></span><span style="color:#117700"></</span><span style="color:#117700">form</span><span style="color:#117700">></span></span>
结果: