controller大全(推荐)
@Controller
@RequestMapping("/router")
@SessionAttributes(value = { "username" })
public class RouterController {
@RequestMapping(value = "/index", params = { "username", "age!=18" })
public String index() {
return "index";
} // 通配符的使用
@RequestMapping(value = "/testantpath/*/abc")
public String testantpath() {
return "index";
} // pathVariable
@RequestMapping(value = "/testpathvariable/{userId}")
public String testpathvariable(
@PathVariable(value = "userId") Integer userId) {
System.out.println(userId);
return "index";
} // RESTFUL
@RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
public String testgetmethod(@PathVariable(value = "userId") Integer userId) {
System.out.println("GET:" + userId);
return "index";
} @RequestMapping(value = "/user/{userId}", method = RequestMethod.POST)
public String testPostmethod(@PathVariable(value = "userId") Integer userId) {
System.out.println("POST:" + userId);
return "index";
} @RequestMapping(value = "/user/{userId}", method = RequestMethod.PUT)
public String testputmethod(@PathVariable(value = "userId") Integer userId) {
System.out.println("PUT:" + userId);
return "index";
} @RequestMapping(value = "/user/{userId}", method = RequestMethod.DELETE)
public String testdeletemethod(
@PathVariable(value = "userId") Integer userId) {
System.out.println("DELETE:" + userId);
return "index";
} // @RequestParam
@RequestMapping(value = "/tesrequestparam")
public String tesrequestparam(
@RequestParam(value = "username", required = false) String username) {
System.out.println(username);
return "index";
} // @RequestHeader
@RequestMapping(value = "/testrequestheader")
public String testrequestheader(
@RequestHeader(value = "Accept-Language") String al) {
System.out.println(al);
return "index";
} // @CookieValue
@RequestMapping(value = "/testcookievalue")
public String testcookievalue(
@CookieValue(value = "JSESSIONID") String cookie) {
System.out.println(cookie);
return "index";
} /*
* pojo的 获取
*/
@RequestMapping(value = "/testuser")
public String testpojo(TUser user) {
System.out.println(user);
// 数据库的操作
return "index";
} /*
* ModelAndView
*/
@RequestMapping(value = "/testmodelandview")
public ModelAndView testmodelandview() {
ModelAndView modelAndView = new ModelAndView("index");
modelAndView.addObject("username", "lisi");
return modelAndView;
} /*
* Map
*/
@RequestMapping(value = "/testmap")
public String testmap(Map<String, Object> map) {
map.put("age", 18);
return "index";
} /*
* Model
*/
@RequestMapping(value = "/testmodel")
public String testModel(Model model) {
model.addAttribute("email", "1@1.com");
return "index";
} /*
* @SessionAttributes
*/
@RequestMapping(value = "/testsession")
public String testsession(Model model) {
model.addAttribute("username", "wangwu");
return "index";
} /*
* @ModelAttribute
*/
@ModelAttribute
public void model(
@RequestParam(value = "userId", required = false) Integer userId,
Model model) {
if (userId != null) {
// 从数据库获取对象
TUser user = new TUser(userId, "wangwu", "123456", "1@1.com", 18);
model.addAttribute("user", user);
}
} @RequestMapping(value = "/testmodelattribute")
public String testmodelattribute(@ModelAttribute(value = "user") TUser user) {
System.out.println(user);
// 数据库的操作
return "index";
} /*
* 获取JSON数据
*/
@ResponseBody
@RequestMapping(value = "/testjson")
public TUser testjson() {
TUser user = new TUser(5, "wangwu", "123456", "1@1.com", 18);
return user; } /*
* 文件上传
*/
@RequestMapping(value = "/testupload")
public void upload(@RequestParam(name = "file1") MultipartFile file) {
File newFile = new File("E:\\" + file.getOriginalFilename());
if (!newFile.exists()) {
newFile.mkdirs();
}
try {
file.transferTo(newFile);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /*
* servlet api
*/
@RequestMapping(value = "/testhttpservletrequest")
public String testHttpServletRequest(HttpServletRequest request) {
request.setAttribute("username", "admin");
return "index";
} /*
* redirect
*/
@RequestMapping(value = "/testredirect")
public String testRedirect() {
return "redirect:/router/testhttpservletrequest";
} /*
* testValidator
*/
@RequestMapping(value = "/testvalidator")
public String testValidator(@Valid TUser user, BindingResult result,
HttpServletRequest request) {
if (result.hasErrors()) {
List<FieldError> fieldErrors = result.getFieldErrors();
for (FieldError fieldError : fieldErrors) {
request.setAttribute("_error" + fieldError.getField(),
fieldError.getDefaultMessage());
}
}
return "index";
} }
controller大全(推荐)的更多相关文章
- JavaScript数组方法大全(推荐)
原网址:http://www.jb51.net/article/87930.htm 数组在笔试中经常会出现的面试题,javascript中的数组与其他语言中的数组有些不同,为了方便之后数组的方法学习, ...
- JS鼠标事件大全 推荐收藏
一般事件 事件 浏览器支持 描述 onClick HTML: 2 | 3 | 3.2 | 4 Browser: IE3 | N2 | O3 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击 onDb ...
- Redis高级命令操作大全--推荐
redis安装和使用 redis安装 wget http://download.redis.io/redis-stable.tar.gz tar zxvf redis-stable.tar.gz cd ...
- SharePoint 入门书籍推荐 转载来源http://www.cnblogs.com/jianyus/p/3513238.html
最近,总有人说刚入门SharePoint,没有好的资料或者电子书,资料推荐大家多看看博客园和CSDN的博客.对于看博客,我一般是两个思路,要么找一个人的从头到尾看一遍,觉得有意义的,就把地址加收藏:或 ...
- SharePoint 入门书籍推荐
最近,总有人说刚入门SharePoint,没有好的资料或者电子书,资料推荐大家多看看博客园和CSDN的博客.对于看博客,我一般是两个思路,要么找一个人的从头到尾看一遍,觉得有意义的,就把地址加收藏:或 ...
- 开发必备linux命令大全-稳赚不亏
我们的服务一般都是在linux系统运行,因此了解一些关于linux命令是必须.接下来将一一详细介绍一些常用的linux的命令 文件操作 远程登录与操作 磁盘挂载 进程管理 启动和结束 系统性能参数查看 ...
- fir.im Weekly - 2016 开年技术干货分享
开年上班,北上广的技术er 陆续重返"人间".看到别人已返工写代码,竟然有种慌慌的感觉(ง •̀_•́)ง 勤奋好学如你,fir.im weekly 送上最新一波技术分享供你 &q ...
- emberjs学习二(ember-data和localstorage_adapter)
emberjs学习二(ember-data和localstorage_adapter) 准备工作 首先我们加入ember-data和ember-localstorage-adapter两个依赖项,使用 ...
- mvc5+ef6+Bootstrap 项目心得--身份验证和权限管理
1.mvc5+ef6+Bootstrap 项目心得--创立之初 2.mvc5+ef6+Bootstrap 项目心得--身份验证和权限管理 3.mvc5+ef6+Bootstrap 项目心得--WebG ...
随机推荐
- qt QAbstractItemModel一些方法介绍
一. virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::Edit ...
- 吴恩达深度学习:2.12向量化logistic回归
1.不使用任何for循环用梯度下降实现整个训练集的一步迭代. (0)我们已经讨论过向量化如何显著加速代码,在这次视频中我们会设计向量化是如何实现logistic回归,这样酒桶同时处理m个训练集,来实现 ...
- 3-关于ES的几个小疑问和解答
1.ES如何实现分布式 2.ES如何实现高实时 3.ES如何实现高扩展 4.ES7.x版本为何废弃type 5.搜索原理--知乎es
- 关于Pandas中Dataframe的操作(一)
1.如何实现两个dataframe去重()? 假设df1是所有的数据,现在想去除与df2中重复的数据,也就是实现对df1进行操作,让他的数据不再包括df2. 方法一:先把需要剔除的df2的某一列(如i ...
- 微信小程序自定义字体及自定义图标问题说明
自定义图标及自定义字体,一直是很多小程序开发者的心病,其实本站是很多解决方案的,为了集中起来,方便直接跳过此坑,我特别做了这次针对字体及字体图标的跳坑: 相关文章:微信小程序添加并使用外部字体(成功添 ...
- iptables-restore - 恢复 IP Tables
总览 SYNOPSIS iptables-restore [-c] [-n] 描述 DESCRIPTION iptables-restore 用来从 STDIN 给出的数据中恢复 IP Tables. ...
- Linux日常之数据重定向
Linux在启动后,会默认打开3个文件操作符,分别是 (1)标准输入0,standard input (2)正确输出1,standard output (3)错误输出2,standard error ...
- linux 文件查找 find命令详解
一,从索引库查找文件:locate 索引库:操作系统会周期性的遍历根文件系统,然后生成索引库 手动更新索引库:updatedb 语法:locate [OPTION]... PATTERN... 只匹配 ...
- python基础练习题4
题目:现有一个数据库记录文件(0005.txt)保证了学生课程签到的数据记录('2017-03-13 11:50:09',271,131),('2017-03-14 11:52:19',273,131 ...
- 编译安装cmake
安装cmake 1.为什么用cmake? mysql部分版本安装前编译需要用软件cmake,而不是我们之前通常使用的make! 百度百科:CMake 可以编译源代码.制作程式库.产生适配器(wr ...