关键词:
一、SpringMVC注解入门
1. 创建web项目
2. 在springmvc的配置文件中指定注解驱动,配置扫描器
- <mvc:annotation-driven />
-
- <context:component-scan base-package="org.study1.mvc.controller" />
-
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
- <property name="prefix" value="/WEB-INF/view/"></property>
-
- <property name="suffix" value=".jsp"></property>
- </bean>
<context:component-scan/> 扫描指定的包中的类上的注解,常用的注解有:
@Controller 声明Action组件
@Service 声明Service组件 @Service("myMovieLister")
@Repository 声明Dao组件
@Component 泛指组件, 当不好归类时.
@RequestMapping("/menu") 请求映射
@Resource 用于注入,( j2ee提供的 ) 默认按名称装配,@Resource(name="beanName")
@Autowired 用于注入,(srping提供的) 默认按类型装配
@Transactional( rollbackFor={Exception.class}) 事务管理
@ResponseBody
@Scope("prototype") 设定bean的作用
3. @controller:标识当前类是控制层的一个具体的实现
4. @requestMapping:放在方法上面用来指定某个方法的路径,当它放在类上的时候相当于命名空间需要组合方法上的requestmapping来访问。
- @Controller
- @RequestMapping("/test")
- public class TestController {
-
-
- @RequestMapping("/hello.do")
- public String hello() {
- System.out.println("springmvc annotation... ");
- return "jsp1/index";
- }
-
-
- }
在本例中,项目部署名为mvc,tomcat url为 http://localhost,所以实际为:http://localhos/mvc
在本例中,因为有命名空间 /test,所以请求hello方法地址为:http://localhost/mvc/test/hello.do
输出:springmvc annotation...
二、注解形式的参数接收
1. HttpServletRequest可以直接定义在参数的列表,通过该请求可以传递参数
url:http://localhost/mvc/test/toPerson.do?name=zhangsan
- @RequestMapping("/toPerson.do")
- public String toPerson(HttpServletRequest request) {
- String result = request.getParameter("name");
- System.out.println(result);
- return "jsp1/index";
- }
可以从HttpServletRequest 取出“name”属性,然后进行操作!如上,可以取出 “name=zhangsan”
输出:zhangsan
2. 在参数列表上直接定义要接收的参数名称,只要参数名称能匹配的上就能接收所传过来的数据, 可以自动转换成参数列表里面的类型,注意的是值与类型之间是可以转换的
2.1传递多种不同类型的参数:
url:http://localhost/mvc/test/toPerson1.do?name=zhangsan&age=14&address=china&birthday=2000-2-11
- @RequestMapping("/toPerson1.do")
- public String toPerson1(String name, Integer age, String address,
- Date birthday) {
- System.out.println(name + " " + age + " " + address + " " + birthday);
- return "jsp1/index";
- }
-
- @InitBinder
- public void initBinder(ServletRequestDataBinder binder) {
- binder.registerCustomEditor(Date.class, new CustomDateEditor(
- new SimpleDateFormat("yyyy-MM-dd"), true));
- }
输出:zhangsan 14 china Fri Feb 11 00:00:00 CST 2000
2.2传递数组:
url:http://localhost/mvc/test/toPerson2.do?name=tom&name=jack
- @RequestMapping("/toPerson2.do")
- public String toPerson2(String[] name) {
- for (String result : name) {
- System.out.println(result);
- }
- return "jsp1/index";
- }
输出:tom jack
2.3传递自定义对象(可多个):
url:http://localhost/mvc/test/toPerson3.do?name=zhangsan&age=14&address=china&birthday=2000-2-11
User 定义的属性有:name,age,并且有各自属性的对应的set方法以及toString方法
Person定义的属性有:name,age.address,birthday,并且有各自属性的对应的set方法以及toString方法
- @RequestMapping("/toPerson3.do")
- public String toPerson3(Person person, User user) {
- System.out.println(person);
- System.out.println(user);
- return "jsp1/index";
- }
输出:
Person [name=zhangsan, age=14, address=china, birthday=Fri Feb 11 00:00:00 CST 2000]
User [name=zhangsan, age=14]
自动封装了对象,并且被分别注入进来!
三、注解形式的结果返回
1. 数据写到页面,方法的返回值采用ModelAndView, new ModelAndView("index", map);,相当于把结果数据放到response里面
url:http://localhost/mvc/test/toPerson41.do
url:http://localhost/mvc/test/toPerson42.do
url:http://localhost/mvc/test/toPerson43.do
url:http://localhost/mvc/test/toPerson44.do
- @RequestMapping("/toPerson41.do")
- public String toPerson41(HttpServletRequest request) throws Exception {
- request.setAttribute("p", newPesion());
- return "index";
- }
-
- @RequestMapping("/toPerson42.do")
- public ModelAndView toPerson42() throws Exception {
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("p", newPesion());
- return new ModelAndView("index", map);
- }
-
- @RequestMapping("/toPerson43.do")
- public String toPerson43(Map<String, Object> map) throws Exception {
- map.put("p", newPesion());
- return "index";
- }
-
- @RequestMapping("/toPerson44.do")
- public String toPerson44(Model model) throws Exception {
-
- model.addAttribute("p", newPesion());
- return "index";
- }
-
- public Person newPesion(){
- Person person = new Person();
- person.setName("james");
- person.setAge(29);
- person.setAddress("maami");
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- Date date = format.parse("1984-12-28");
- person.setBirthday(date);
- return person;
- }
以上四种方式均能达到相同的效果,但在参数列表中直接定义Model,model.addAttribute("p", person);把参数值放到request类里面去,建议使用
2. Ajax调用springmvc的方法:直接在参数的列表上定义PrintWriter,out.write(result);把结果写到页面,建议使用的
url:http://localhost/mvc/test/toAjax.do
-
- @RequestMapping("/ajax1.do")
- public void ajax1(String name, HttpServletResponse response) {
- String result = "hello " + name;
- try {
- response.getWriter().write(result);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
-
- @RequestMapping("/ajax2.do")
- public void ajax2(String name, PrintWriter out) {
- String result = "hello " + name;
- out.write(result);
- }
-
-
- @RequestMapping("/toAjax.do")
-
- public String toAjax() {
- return "ajax";
- }
ajax页面代码如下:
- <script type="text/javascript" src="js/jquery-1.6.2.js"></script>
- <script type="text/javascript">
- $(function(){
- $("#mybutton").click(function(){
- $.ajax({
- url:"test/ajax1.do",
- type:"post",
- dataType:"text",
- data:{
- name:"zhangsan"
- },
- success:function(responseText){
- alert(responseText);
- },
- error:function(){
- alert("system error");
- }
- });
- });
- });
- </script>
- </head>
- <body>
- <input id="mybutton" type="button" value="click">
- </body>
-
四、表单提交和重定向
1、表单提交:
请求方式的指定:@RequestMapping( method=RequestMethod.POST )可以指定请求方式,前台页面就必须要以它制定好的方式来访问,否则出现405错误
表单jsp页面:
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>SpringMVC Form</title>
- </head>
- <body>
- <form action="test/toPerson5.do" method="post">
- name:<input name="name" type="text"><br>
- age:<input name="age" type="text"><br>
- address:<input name="address" type="text"><br>
- birthday:<input name="birthday" type="text"><br>
- <input type="submit" value="submit"><br>
- </form>
- </body>
- </html>
对应方法为:
- @RequestMapping("/toform.do")
- public String toForm() {
- return "form";
- }
-
- @RequestMapping(value = "/toPerson5.do", method = RequestMethod.POST)
- public String toPerson5(Person person) {
- System.out.println(person);
- return "jsp1/index";
- }
url:
http://localhost/mvc/test/toform.do
2. 重定向:controller内部重定向,redirect:加上同一个controller中的requestMapping的值,controller之间的重定向:必须要指定好controller的命名空间再指定requestMapping的值,redirect:后必须要加/,是从根目录开始
- @RequestMapping("/redirectToForm.do")
- public String redirectToForm() {
- return "redirect:toform.do";
- }
-
- @RequestMapping("/redirectToForm1.do")
- public String redirectToForm1() {
-
- return "redirect:/test1/toForm.do";
- }
参考资料: http://elf8848.iteye.com/blog/875830/
springmvc学习笔记-----注解和非注解入门小程序
...的处理器和映射器三:注解的处理器映射器和适配器四:springmvc.xml中的内容以及项目工程目录 最近一直在做一个电商的项目,周末加班,忙的都没有时间更新博客了。终于在上周五上线了,可以轻松几天了。闲话不扯淡了,... 查看详情
springmvc验证框架validation自定义注解实现传递参数到国际化资源文件(代码片段)
关于SpringMVC验证框架Validation的使用方法,不是本篇的重点,可参见博文SpringMVC介绍之Validation在使用Validation时,一定有朋友遇到过一个问题,那就是:无法传递参数到国际化资源文件properties错误描述中。举个... 查看详情
springboot2从入门到入坟|请求参数处理篇:常用参数注解之@requestbody(代码片段)
...的参数注解,即@RequestBody,关于该注解,只要是学过一点SpringMVC的人,都会对它无比的熟悉,因为它无非就是用于获取请求体的。既然是获取请求体,那么必然是来获取post方式的请求的请求体的,因为只有post方式的请求才会有... 查看详情
springboot2从入门到入坟|请求参数处理篇:常用参数注解之@requestbody(代码片段)
...的参数注解,即@RequestBody,关于该注解,只要是学过一点SpringMVC的人,都会对它无比的熟悉,因为它无非就是用于获取请求体的。既然是获取请求体,那么必然是来获取post方式的请求的请求体的,因为只有post方式的请求才会有... 查看详情
springboot2从入门到入坟|请求参数处理篇:源码分析之各种类型参数解析原理
在前面几讲,我花了些时间为大家详细介绍了一些SpringMVC底层常用的参数注解,想必大家也能熟练使用它们了。在使用这些常用参数注解的过程中,不知大家有没有过这样一个疑惑,即为何我们只需要给Controller中... 查看详情
springboot2从入门到入坟|请求参数处理篇:源码分析之各种类型参数解析原理
在前面几讲,我花了些时间为大家详细介绍了一些SpringMVC底层常用的参数注解,想必大家也能熟练使用它们了。在使用这些常用参数注解的过程中,不知大家有没有过这样一个疑惑,即为何我们只需要给Controller中... 查看详情
springmvc验证框架validation自定义注解实现传递参数到国际化资源文件(代码片段)
关于SpringMVC验证框架Validation的使用方法,不是本篇的重点,可参见博文SpringMVC介绍之Validation在使用Validation时,一定有朋友遇到过一个问题,那就是:无法传递参数到国际化资源文件properties错误描述中。举个... 查看详情
mybatis多参数传递之注解方式示例--转
...适用于需要传递多个参数的情况,今天就来介绍如何使用注解传递多个参数(示例源码下载地址:http://down.51cto.com/data/537051)。一、使用 查看详情
springshell入门介绍(代码片段)
目录SpringShell是什么入门实践基础配置简单示例注解@ShellMethod注解@ShellOption自定义参数名称设置参数默认值为一个参数传递多个值对布尔参数的特殊处理带空格的参数处理参数校验动态命令可用性为单一命令提供动态可用性为多... 查看详情
java之方法的参数传递(值传递和引用传递)
...的实参如何传入方法呢?首先要明确:变量分为两大类:基础数据类型、引用数据类型。基础数据类型参数传递方式只有一种:值传递。即将实际参数值的副本(复制品)传入方法内,而参数本身不受影响;publicclassTest{publicstati... 查看详情
springmvc注解之@controller和@restcontroller注解(代码片段)
一、使用说明官方文档:@RestControllerisastereotypeannotationthatcombines@ResponseBodyand@Controller.意思是:@RestController注解相当于@ResponseBody+@Controller合在一起的作用。@Cont 查看详情
springmvc之@requestparam@requestbody@requestheader等详解
转自原文SpringMVC之@RequestParam@RequestBody@RequestHeader等详解引言:接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request数据到handlermethod参数数据的绑定所用到的注解和什么情形下使用;简介:handlermethod参数绑定... 查看详情
@requestparam注解详细使用
前言在SpringMvc后台进行获取数据,一般有三种:1.request.getParameter(“参数名”)2.用@RequestParam注解获取3.Springmvc默认支持的数据类型接收参数,可直接通过controller方法参数对应jsp中请求参数name直接获取关于springmvc前后台获取... 查看详情
免费下载全套最新006springmvc视频教程+教学资料+学习课件+源代码+软件开发工具
006SpringMVC视频教程网盘地址:链接:https://pan.baidu.com/s/1LbKIui6W2sRZ3Kt-2RsZPw提取码:nvre加公众号获取更多新教程教程目录大纲./006SpringMVC├──10.SpringMVC_RequestHeader注解.zip├──11.SpringMVC_CookieValue注解.zip├──12.SpringMVC_使用POJO作为... 查看详情
springboot2从入门到入坟|请求参数处理篇:常用参数注解之@requestattribute(代码片段)
在本讲,我将会再来为大家介绍一个常用的参数注解,即@RequestAttribute,该注解从字面意义上翻译过来就是请求属性的意思,那么该注解的作用是什么呢?这里我也不卖关子了,直接告诉大家吧,该注解是用于获取request域中所保... 查看详情
springboot2从入门到入坟|请求参数处理篇:常用参数注解之@requestattribute(代码片段)
在本讲,我将会再来为大家介绍一个常用的参数注解,即@RequestAttribute,该注解从字面意义上翻译过来就是请求属性的意思,那么该注解的作用是什么呢?这里我也不卖关子了,直接告诉大家吧,该注解是用于获取request域中所保... 查看详情
《c#零基础入门之百识百例》(三十四)方法参数--可选参数和形参数组--n数的最大公约数
C#零基础入门函数--方法参数--可选参数和形参数组前言一,可选参数1.1使用方式1.2使用规则二,形参数组2.1解释说明2.2params关键字三,实例练习--N数的最大公约数3.1题目描述3.2问题分析3.3参考代码前言本文属于C#零基础入门之百... 查看详情
springboot2从入门到入坟|请求参数处理篇:常用参数注解之@requestparam(代码片段)
大家应该知道吧!就是发送请求时,我们是可以在后面带上请求参数的,就像如下页面中的超链接那样。<!DOCTYPEhtml><htmllang="en"><head><metacharset 查看详情