(三)springmvc之注解的基本使用
一、@Controller
- @Controller 标记一个类是Controller
二、RequestMapping 地址映射
2.1 Value的操作。
注解在类上面 (父)
注解在方法上面。 (子)
2.1.1 SpringMVC完整的请求,就是父子加请来的请求。
<a href="<%=request.getContextPath()%>/userController/list">list方法</a>
<a href="<%=request.getContextPath()%>/userController/save">save方法</a>
controller:
package com.controller; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping(value = "/userController")
public class UserController {
@RequestMapping(value = "/list")
public ModelAndView list(HttpServletRequest request,
HttpServletResponse response) throws Exception { System.out.println("r执行list方法"); ModelAndView mv = new ModelAndView();
mv.addObject("req_key", "Model Data");
mv.setViewName("succ");
return mv;
} @RequestMapping(value = "/save")
public ModelAndView save(HttpServletRequest request,
HttpServletResponse response) throws Exception { System.out.println("r执行save方法"); ModelAndView mv = new ModelAndView();
mv.addObject("req_key", "Model Data");
mv.setViewName("succ");
return mv;
} }
2.2 用于映射请求中的参数。
<a href="<%=request.getContextPath()%>/paramController?task=list">list方法</a>
<a href="<%=request.getContextPath()%>/paramController?task=save">save方法</a>
controller:
package com.controller; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping(value = "/paramController")
public class ParamController {
@RequestMapping(params = "task=list")
public ModelAndView list(HttpServletRequest request,
HttpServletResponse response) throws Exception { System.out.println("r执行list方法"); ModelAndView mv = new ModelAndView();
mv.addObject("req_key", "Model Data");
mv.setViewName("succ");
return mv;
} @RequestMapping(params = "task=save")
public ModelAndView save(HttpServletRequest request,
HttpServletResponse response) throws Exception { System.out.println("r执行save方法"); ModelAndView mv = new ModelAndView();
mv.addObject("req_key", "Model Data");
mv.setViewName("succ");
return mv;
} }
2.3 Method:表示是处理哪一种请求.GET/POST
<a href="<%=request.getContextPath()%>/methodController/add">list方法</a> <form method="post" action="<%=request.getContextPath()%>/methodController/add">
<input type="submit" value="提交"/>
</form>
controller:
package com.controller; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping(value = "/methodController")
public class MethodController { @RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) throws Exception { System.out.println("处理GET请求"); ModelAndView mv = new ModelAndView();
mv.addObject("req_key", "Model Data");
mv.setViewName("succ");
return mv;
} @RequestMapping(value = "/add", method = RequestMethod.POST)
// @RequestMapping(value = "/add", method = { RequestMethod.GET,
// RequestMethod.POST })
public ModelAndView save(HttpServletRequest request,
HttpServletResponse response) throws Exception { System.out.println("处理POST请求"); ModelAndView mv = new ModelAndView();
mv.addObject("req_key", "Model Data");
mv.setViewName("succ");
return mv;
} }
2.4 RequestParam:获取地址中的参数信息。
<a href="<%=request.getContextPath()%>/requestParamController/edit?userid=1">list方法</a>
<a href="<%=request.getContextPath()%>/requestParamController?task=edit&userid=2">list方法</a>
controller
package com.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping(value = "/requestParamController")
public class RequestParamController { @RequestMapping(value = "/edit", method = RequestMethod.GET)
public ModelAndView edit_1(
@RequestParam(name = "userid", required = false) String id)
throws Exception { System.out.println("userid = " + id); return null;
} @RequestMapping(params = "task=edit", method = RequestMethod.GET)
public ModelAndView edit_2(String userid) throws Exception { System.out.println("task=edit中的userid = " + userid); return null;
} }
2.5 PathVariable:REST风格.
<a href="<%=request.getContextPath()%>/pathVarController/edit/1/admin">edit方法</a>
<a href="<%=request.getContextPath()%>/pathVarController/edit/2/guest">edit方法</a>
<a href="<%=request.getContextPath()%>/pathVarController/edit/3/test">edit方法</a> <a href="<%=request.getContextPath()%>/pathVarController/3/admin/delete">通配符一个方法</a>
<a href="<%=request.getContextPath()%>/pathVarController/3/admin/test/xxxx/yyyy/remove">通配符多个方法</a>
controller
package com.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping(value = "/pathVarController")
public class PathVarController { @RequestMapping(value = "/edit/{userid}/{username}", method = RequestMethod.GET)
public ModelAndView edit_1(@PathVariable(value = "userid") String userid,
@PathVariable(value = "username") String username) throws Exception { System.out.println("userid = " + userid);
System.out.println("username = " + username); return null;
} /**
* 通配符的写法
*
* 1个*代表通配一个地址
*
* 2个*代表通配多个地址
*/
@RequestMapping(value = "{userid}/*/delete", method = RequestMethod.GET)
public ModelAndView delete_1(@PathVariable(value = "userid") String userid)
throws Exception { System.out.println("delete_1====>userid = " + userid); return null;
} @RequestMapping(value = "{userid}/**/remove", method = RequestMethod.GET)
public ModelAndView delete_2(@PathVariable(value = "userid") String userid)
throws Exception { System.out.println("remove_2====>userid = " + userid); return null;
}
}
2.6 header和cookie
<a href="<%=request.getContextPath()%>/headerController/header">header</a> <a href="<%=request.getContextPath()%>/headerController/cookie">cookie</a>
controller
package com.shyroke.controller; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping(value = "/headerController")
public class HeaderController { @RequestMapping(value = "/header", method = RequestMethod.GET)
public ModelAndView header(@RequestHeader(name = "Host") String Host,
@RequestHeader(name = "User-Agent") String agent, HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("header");
System.out.println("Host= " + Host);
System.out.println("agent= " + agent); System.out.println("RequestHeader = " + request.getHeader("Host")); Cookie cookie_a = new Cookie("cookie_a", "Admin/GTUeST/axxxx"); cookie_a.setPath("/");
response.addCookie(cookie_a);
return null;
} @RequestMapping(value = "/cookie", method = RequestMethod.GET)
public ModelAndView cookie(
@CookieValue(name = "JSESSIONID", required = false) String sessionID,
@CookieValue(name = "cookie_a", required = false) String cookie_a)
throws Exception {
System.out.println("cookie_1 = " + sessionID); System.out.println("cookie_a = " + cookie_a); return null;
} }
结果
(三)springmvc之注解的基本使用的更多相关文章
- 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s
1. Spring-mvc介绍 1.1市面上流行的框架 Struts2(比较多) Springmvc(比较多而且属于上升的趋势) Struts1(即将被淘汰) 其他 1.2 spring-mv ...
- springMVC的注解详解
springmvc常用注解标签详解 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业 ...
- springmvc常用注解与类型转换
springmvc常用注解与类型转换 一:前置 spring -servlet.xml 注入 <!-- 启用spring mvc 注解 --> <context:annotation ...
- springMvc的注解注入方式
springMvc的注解注入方式 最近在看springMvc的源码,看到了该框架的注入注解的部分觉的有点吃力,可能还是对注解的方面的知识还认识的不够深刻,所以特意去学习注解方面的知识.由于本人也是抱着 ...
- 一 : springmvc常用注解
springmvc常用注解详解1.@Controller在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层 ...
- 转:springmvc常用注解标签详解
Spring5:@Autowired注解.@Resource注解和@Service注解 - IT·达人 - 博客园--这篇顺序渐进,讲得超级好--此人博客很不错http://www.cnblogs.c ...
- springBoot注解大全JPA注解springMVC相关注解全局异常处理
https://www.cnblogs.com/tanwei81/p/6814022.html 一.注解(annotations)列表 @SpringBootApplication:包含了@Compo ...
- SpringMVC 常用注解 详解
SpringMVC 常用注解 详解 SpringMVC 常用注解 1.@RequestMapping 路径映射 2.@Requ ...
- SpringMVC常用注解實例詳解3:@ResponseBody
我的開發環境框架: springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...
- SpringMVC常用注解實例詳解2:@ModelAttribute
我的開發環境框架: springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...
随机推荐
- VM 15 永久激活密钥
VMware Workstation 15 永久激活密钥 YG5H2-ANZ0H-M8ERY-TXZZZ-YKRV8 UG5J2-0ME12-M89WY-NPWXX-WQH88 UA5 ...
- SQL优化 | Oracle 绑定变量
之前整理过一篇有关绑定变量的文章,不太详细,重新补充一下. Oracle 绑定变量 http://www.cndba.cn/Dave/article/1275 一.绑定变量 bind variable ...
- 亲历谷歌 Chrome 浏览器弹窗境外广告的解决方法(图) | 技术乐园
亲历谷歌 Chrome 浏览器弹窗境外广告的解决方法(图) | 技术乐园 转 https://www.hack520.com/338.html 谷歌的 Chrome 浏览器是我非常喜欢的一款的浏览器, ...
- [Java复习] 缓存Cache part1
1. 在项目中是如何使用缓存的?为什么要用?不用行不行?用了可能会有哪些不良后果? 结合项目业务,主要两个目的:高性能和高并发.缓存走内存,天然支持高并发. 不良后果: 缓存与DB双写不一致 缓存雪崩 ...
- Ubuntu + Apache2 环境下用C编写 一个简单的cgi脚本
我只学习过c语言,没有学习过prel,网上很多教程都是针对prel的,很少有针对c的.自己在Ubuntu下鼓捣了一下午,也总算是用c成功编写了一个helloworld的cgi,算是cgi入门的第一步. ...
- 123456123456#0#-----com.threeapp.JiQiRenDaZhan01----机器人大战恐龙
com.threeapp.JiQiRenDaZhan01----机器人大战恐龙
- (十)Centos之文件搜索命令find
1.1 find [搜索范围] [搜索条件](搜索文件) find是在系统当中搜索符合条件的文件名. 如果需要匹配,使用通配符匹配,通配符是完全匹配. * 匹配任意内容 ?匹配任意一个字符 []匹配任 ...
- vmware安装密钥
VMware虚拟机已升级至14版本,之前的12版本的秘钥已经无法使用,在此分享一下VMware Workstation 14永久激活密钥: CG54H-D8D0H-H8DHY-C6X7X-N2KG6 ...
- 上下文管理器之__enter__和__exit__
目录 前言 with as是如何工作的 自定制open方法 更多的示例 返回主目录 前言 回到顶部 有个学生在第四轮面试中被CTO问到:如何自定义实现with open的功能.然后就一脸懵逼的回来找我 ...
- 统一建模语言UML
目录 1. UML定义 2. UML结构 2.1 视图(View) 2.2 图(Diagram) 2.3 模型元素(Model element) 2.4 通用机制(General mechanism) ...