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 ...
随机推荐
- 解决 SQLPlus无法登陆oracle,PLSql可以登陆,报错ORA-12560
使用Oracle 11g 64位服务器,安装64位.32位客户端,出现SQLPlus无法连接数据库,PLSql可以连接问题. 网上查了很多,都不能解决问题,在下面提供一种. 环境变量 右击计算机属性- ...
- ORA-01846: 周中的日无效
参考这篇博客:https://blog.csdn.net/yabingshi_tech/article/details/8678218
- case函数,replace函数
(case '字段' when '数据1' then '输出1' when '数据2' then '输出2' when '数据3' then '输出3' else '其他数据输出一致' end) as ...
- TFS 由于服务器时钟设置可能不正确,无法更新数据。请与 Team Foundation Server 管理员联系。
原因:这个问题是由于修改安装TFS服务器的系统时间而导致的,因为TFS的Source Control会根据签入时间做判断,如果后续签入的时间小于变更集的最新版本的时间,就会报这个错误 解决办法:首先将 ...
- 配置 http 反向代理
[root@nginx ~]# cd /etc/nginx/ 1 [root@nginx nginx]# cp nginx.conf nginx.conf.bak #备份一个原配置文件 2 [root ...
- byteArray转换为double,int
/*将int转为低字节在前,高字节在后的byte数组 b[0] = 11111111(0xff) & 01100001 b[1] = 11111111(0xff) & (n & ...
- Instr()函数用法
返回 Variant (Long),指定一字符串在另一字符串中最先出现的位置. 语法 InStr([start, ]string1, string2[, compare]) InStr 函数的语法具有 ...
- java数据结构复习01
1.数组 package javaDataStruct.array01; public class MyArray { private int[] arr; // 表示有效数据的长度 private ...
- a标签前端下载火狐兼容和笔记
1.a标签实现前端下载的谷歌兼容 我们都知道,文件下载的一种实现方案就是后端返回文件流,然后前端进行生成a标签并触发点击来下载.但是在火狐浏览器的时候,需要注意一些兼容性问题.原因是火狐的同源策略.官 ...
- php关于jquery ajax json不返回数据的问题
分析原因: 1.php端代码有错导致echo输出错误,导致ajax返回函数不执行 2.jquery版本原因 3.juqery前端script代码问题