(三)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 ...
随机推荐
- XGBoost、LightGBM、Catboost总结
sklearn集成方法 bagging 常见变体(按照样本采样方式的不同划分) Pasting:直接从样本集里随机抽取的到训练样本子集 Bagging:自助采样(有放回的抽样)得到训练子集 Rando ...
- bootargs中的rootwait 与rootdelay有什么区别?
答: rootwait是无限期等待,而rootdelay可以指定等待的时间,更加灵活.
- osg::Camera example
#ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include<iostream> #include <osgV ...
- 123457123456#0#-----com.threeapp.XueShuXue013----儿童学数学(13种方法)
com.threeapp.XueShuXue013----儿童学数学(13种方法)
- 最新javascript自动按比例显示图片,按比例压缩图片显示
最新javascript自动按比例显示图片,按比例压缩图片显示 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...
- css3 animation 点亮灯光效果
<style> .wrap .bulb { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50% ...
- 极客时间-左耳听风-程序员攻略-UI/UX设计
程序员练级攻略:UI/UX设计 学习设计新手, 7 steps to become a UI/UX designer 学习设计的一些原则和套路,如配色.平衡.排版.一致性等. 用户体验的 4D 步骤- ...
- dozer工具类
jar:commons-beanutils-1.9.3.jar.commons-lang-2.6.jar.dozer-5.3.2.jar.jcl-over-slf4j-1.7.25.jar.slf4j ...
- 简单介绍shell编程四剑客之sed
概要:分别的作用 grep:文本过滤(模式:pattern)工具,grep,egrep,fgrep,擅长过滤. sed:stream editor 文本编辑工具:(流编辑器),擅长取行.替换. awk ...
- [转载]Oracle触发器详解
转载自http://blog.csdn.net/indexman/article/details/8023740/ 触发器是许多关系数据库系统都提供的一项技术.在oracle系统里,触发器类似过程和函 ...