SpringMVC-2-(Controller)
一)参数类型
@RequestMapping("hello4")
@ResponseBody
public ModelAndView Hello4(
// Servlet的三个参数
HttpServletRequest request,
HttpServletResponse response,
HttpSession session,
// 传入接本数据类型和引用数据类型,注意要和前端同名
String name,
People people,
// 和前端不同命的解决办法
@RequestParam("address1")String address,
// 前段没有传值,设置默认值
@RequestParam(defaultValue = "27") int age,
// 设置这个值一定要有,没有就报错
@RequestParam(required = true) String email,
// 接前端传过来的复选框的值
@RequestParam("likes")List<String> list,
// 前端写法
// <input type="text" name="peo.name"/>
// <input type="text" name="peo.age"/>
// 另外再写一个实体类,属性就是前端传值的名
// public class Test {
// private People people;
Test test,
// 前端写法
// <input type="text" name="peo[1].name"/>
// <input type="text" name="peo[1].age"/>
// 另外要写一个实体类,属性就是前端传值的list集合
// public class Test {
// private People people;
// private List<People> peoples;
Test test1,
// 其实model和ModelAndView还有session都产不多用法,只是返回一个数据参见下边的代码
Model model
)
throws ServletException, IOException { ModelAndView hello = new ModelAndView("main");
Model model1 = model.addAttribute("name", "xupeilei");
return hello;
}
restful风格:
<a href="hello1?name=aaa&age=27">普通风格</a>
<a href="hello2/bbb/17">result风格</a>
@RequestMapping("hello1/{name}/{age}")
public ModelAndView Hello1(@PathVariable String name,@PathVariable String age) {
System.out.println(name+" "+age);
System.out.println("hello springMVC控制器");
ModelAndView hello = new ModelAndView("main");
return hello;
}
参数绑定到实体类:
有时间在写
自定义参数绑定
有事件在写
二)返回值类型及重定向和转发(默认方式都是请求转发)
理解重定向和转发:
重定向:
相当于是浏览器发出请求,返回一个url浏览器再去请求这个url,这时浏览器是知道他请求到哪里去了,所以地址栏可以看到url的改变
转发:
服务器的行为,浏览器发出请求之后,服务器可能又去请求了其他的资源最终只返回一个数据或者模型,这时浏览器并不知道服务器到底做了什么,他只能显示他请求的url;
@RequestMapping:会去找视图解析器
String
@RequestMapping("hello2")
public String hello2(){
// 转发
// return "forward:/html/err.html";
// return "forward:/hello1";
// 重定向
// return "redirect:/html/err.html";
return "redirect:/hello1";
// 通过视图解析器
// return "main";
}
ModelAndView:
@Controller
public class HelloController {
@RequestMapping("hello1")
public ModelAndView hello1(){ // 自定义视图解析器找到main.jsp
// ModelAndView modelAndView = new ModelAndView("main"); // 服务器转发,
// 跳转到err.html
// ModelAndView modelAndView = new ModelAndView("forward:/html/err.html");
// 跳转到其他controller(hello2)
// ModelAndView modelAndView = new ModelAndView("forward:/hello2"); // 浏览器重定向,
// 重定向到err.html
// ModelAndView modelAndView = new ModelAndView("redirect:/html/err.html");
// 重定向到其他controller(hello2)
ModelAndView modelAndView = new ModelAndView("redirect:/hello2"); // 会使用自定义视图解析器,结果找不到报错404
// ModelAndView modelAndView = new ModelAndView("hello2"); return modelAndView;
}
void(没有返回值,只能重定向和转发)
@RequestMapping("hello3")
public void hello3(HttpServletRequest request, HttpServletResponse response) throws IOException {
// 重定向
request.getRequestDispatcher("/html/err.html");
// request.getRequestDispatcher("/hello1");
// 转发
// response.sendRedirect("/html/err.html");
// response.sendRedirect("/hello1"); }
@RequestMapping:
@ResponseBody:
String
void
ModelAndView
对象:(坑!大坑!!巨坑!!!超级坑!!!)(使用jackSon的时候要用高版本不然报错406,无法转换json格式)
@RequestMapping("/hello4")
@ResponseBody
public People hello4(HttpServletResponse response){
People p=new People();
p.setName("sss");
p.setAddress("www");
p.setAge(1);
p.setEmail("ee");
return p;
}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
三)自定义视图解析器
表示在返回值的前边加上/路径和后边加上.jsp,表示所要找的jsp所在德位置,就不需要在controller中手写了,作用就是偷懒;
但是注意:自定义视图解析器controller中的返回值不能加前缀,加的话就走默认的视图解析器,
存在的意义:当我们需要转发的时候
看图:
图一: 图二:
看图一:因为前边没有加任何东西,并且我又自定义视图解析器,那他走的就是自定义视图解析器,回去找一个demo11.jsp的东西,但是会发现找不到报错404
看图二:因为前边加了forward:这时候自定义的视图解析器就失效了,走默认视图解析器,解析出来要转发到dome11,就不去找demo11.jsp了
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
四)请求方法限定
@RequestMapping(value = {"/hello123","/456",method = RequestMethod.POST)}
五)@ResponseBody注解
做了两件事情:
一:将返回值变成json字符串;
二:同时设置响应头为application/json
加上这个之后就不跳转了,以流的形式输出出去,也就不走视图解析器了,并且是直接填充到body中去了
String:
ModelAndView:
考虑一下返回值为ModelAndView 却得到一个json字符串的方式
Void:
对象:
六:拦截器
SpringMVC-2-(Controller)的更多相关文章
- 使用IntelliJ IDEA开发SpringMVC网站(二)框架配置
原文:使用IntelliJ IDEA开发SpringMVC网站(二)框架配置 摘要 讲解如何配置SpringMVC框架xml,以及如何在Tomcat中运行 目录[-] 文章已针对IDEA 15做了一定 ...
- springmvc小结(上)
1.springmvc的整体结构以及流程 ①.前端控制器:只需要在web.xml文件中配置即可 作用:接受请求,处理响应结果,转发器,中央处理器 ②.处理器映射器:根据请求的url找到相应的Handl ...
- 使用IntelliJ IDEA开发SpringMVC网站(五)博客文章管理
原文:使用IntelliJ IDEA开发SpringMVC网站(五)博客文章管理 摘要 通过对博客文章的管理,实现外键操作. 目录[-] 八.博客文章管理 1.查看文章 2.添加博客 3 ...
- 使用IntelliJ IDEA开发SpringMVC网站(四)用户管理
原文:使用IntelliJ IDEA开发SpringMVC网站(四)用户管理 摘要 通过对用户表的管理,更加深入地讲解SpringMVC的操作. 目录[-] 文章已针对IDEA 15做了一定的更新,部 ...
- 使用IntelliJ IDEA开发SpringMVC网站(三)数据库配置
原文:使用IntelliJ IDEA开发SpringMVC网站(三)数据库配置 摘要 讲解在IntelliJ IDEA中,如何进行Mysql数据库的配置 目录[-] 文章已针对IDEA 15做了一定的 ...
- SpringMVC起步(一)
SpringMVC起步(一) 笔记来源于慕课网:https://www.imooc.com/video/7126/0 MVC:Model-View-Controller Model:模型层,业务数据的 ...
- 手写SpringMVC框架(三)-------具体方法的实现
续接前文 手写SpringMVC框架(二)结构开发设计 本节我们来开始具体方法的代码实现. doLoadConfig()方法的开发 思路:我们需要将contextConfigLocation路径读取过 ...
- SpringMVC笔记(1)
一.SpringMVC简介 1.1 MVC模型 MVC模型 MVC全名是Model View Controller,是模型(model)- 视图(view)- 控制器(controller)的缩写,是 ...
- 使用IntelliJ IDEA开发SpringMVC网站(一)开发环境
使用IntelliJ IDEA开发SpringMVC网站(一)开发环境 摘要: 主要讲解初期的开发环境搭建,Maven的简单教学. 访问GitHub下载最新源码:https://github.com/ ...
- C#编译器优化那点事 c# 如果一个对象的值为null,那么它调用扩展方法时为甚么不报错 webAPI 控制器(Controller)太多怎么办? .NET MVC项目设置包含Areas中的页面为默认启动页 (五)Net Core使用静态文件 学习ASP.NET Core Razor 编程系列八——并发处理
C#编译器优化那点事 使用C#编写程序,给最终用户的程序,是需要使用release配置的,而release配置和debug配置,有一个关键区别,就是release的编译器优化默认是启用的.优化代码 ...
随机推荐
- python登录网页版微信发送消息
# coding=utf-8 import datetime import time from selenium import webdriver url = "https://wx2.qq ...
- 用Pytorch训练线性回归模型
假定我们要拟合的线性方程是:\(y=2x+1\) \(x\):[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] \(y\):[1, 3, 5, 7, ...
- dubbo接口demo开发
接口需求 客户端输入uncleyong(当然,也可以输入其它字符串),服务端返回hello uncleyong 开发环境 jdk + idea + maven + zookeeper jdk安装 id ...
- python学习day12 函数Ⅳ (闭包&内置模块)
函数Ⅳ (闭包&内置模块) 1.内置函数(补充) lambda表达式也叫匿名函数. 函数与函数之间的数据互不影响,每次运行函数都会开一个辟新的内存. item = 10 def func(): ...
- Django 中使用kindeditor
KindEditor 是一套开源的在线HTML编辑器,主要用于让用户在网站上获得所见即所得编辑效果,开发人员可以用 KindEditor 把传统的多行文本输入框(textarea)替换为可视化的富文本 ...
- 解决远程连接MongoDB出现错误
前言:最近准备学习下MongoDB,安装什么的都已经弄完了,想远程连接来管理MongoDB,用的软件是robo 3t 第一次连的时候就出错误了 大概意思是连接失败,解决如下 第一步,首先检查你的服务器 ...
- Memcached操作
标准协议和字段 Memcached的标准协议字段包含以下部分: 键,key,任意字符,最大250字节,不能有空格和换行 标志位,32比特,不能为0 超时时间,单位是秒,0代表永不超时,最长30天,30 ...
- 微信小程序无法定位
获取定位的时候报:errMsg:getLocation:fail:require permission desc 错 解决办法: 在app.js加入代码 //app.js新增如下代码 config = ...
- Django-ContentType的使用
一.神器ContentType 如果 继续增加课程 价格策略表还得增加字段 这样django自带一个contentType 帮助我们解决表之间的依赖关系: 1.从settings文件可以看到原生就支持 ...
- 使用select为描述符设置超时
int readable_timeo(int fd, int sec) { fd_set rset; struct timeval tv; FD_ZERO(&rset); FD_SET(fd, ...