SpringMVC笔记1_第1页
免费预览已结束,剩余17页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、springmvc笔记1springmvc笔记1springmvc是一个一种基于java的实现mvc设计模型的哀求驱动类型的轻量级web框架springmvc的入门案例 2.导入相关jar包 4.0.0 cn.itcast springmvc_day01_01_start 1.0-snapshot war springmvc_day01_01_start maven webapp utf-8 1.8 1.8 5.0.2.release org.springframework spring-context $spring.version org.springframework spring-we

2、b $spring.version org.springframework spring-webmvc $spring.version javax.servlet servlet-api 2.5 provided javax.servlet.jsp jsp-api 2.0 provided springmvc_day01_01_start maven-clean-plugin 3.1.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.8.0 maven-surefire-plugin 2.22.1 maven-war-plugin 3

3、.2.2 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2 3,在web.xml中配置前端控制器 archetype created web application dispatcherservlet org.springframework.web.servlet.dispatcherservlet contextconfiglocation classpath:springmvc.xml 1 dispatcherservlet / 4.编写控制器类package cn.itcast.controller;import org.sprin

4、gframework.stereotype.controller;import org.springframework.web.bind.annotation.requestmapping;/控制器类controllerpublic class hellocontroller /用来接收哀求 requestmapping(path = "/hello") public string sayhello() system.out.println("hello springmvc"); return "succ

5、ess" 5.开启扫描注解 6.编写两个jsp首页 title 入门程序 入门程序 胜利页面 title 入门胜利 入门案例的流程总结1.服务器启动,加载配置2.dispatcherservlet对象创建3.springmvc.xml会开启注解扫描4.找到hellocontroller,把他变成对象,加载进入ioc容器当中,5.internalresourceviewresolver视图解析器也会变成对象,谁调用该对象,就会帮他跳转页面requestmapping注解的作用:用于建立哀求url和处理哀求办法之间的对应关系,注解位置用在办法上或者类上哀求参数绑定入门1.哀求地址

6、携带参数username 哀求参数绑定 2.办法带有参数usernamepackage cn.itcast.controller;import org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.requestmapping;controllerrequestmapping("/param")public class paramcontroller /哀求参数绑定入门 requestmapping("/testpar

7、am") public string testparam(string username) system.out.println("执行了."); system.out.println(username); return "success" 3.自动给办法的参数赋值/输出结果/执行了./hehe配置解决中文乱码的过滤器 characterencodingfilter org.springframework.web.filter.characterencodingfilter encoding utf-8

8、characterencodingfilter /* 引用类型的封装public class user implements serializable private string uname; private string age; 集合类型封装编写自定义类型转换器/两个泛型,s表示字符串,t表示想要转换的类型public interface converter 1.实现接口converterpackage cn.itcast.utils;import org.springframework.core.convert.converter.converter;import java.text.

9、dateformat;import java.text.parseexception;import java.text.simpledateformat;import java.util.date;/字符串转日期public class stringtodateconverter implements converter override /实现接口办法 public date convert(string source) if(source = null) throw new runtimeexception("没有日期"); dateformat df

10、= new simpledateformat("yyyy-mm-dd"); try return df.parse(source); catch (exception e) throw new runtimeexception("日期转换异样"); 2.配置自定义类型转换器 3.开启springmvc框架的支持 常用注解requestparam注解:作用:把哀求中指定名称的参数给控制器中的形参赋值属性:value,哀求参数中的名称,required:哀求参数中是否必需提供此参数,默认值:truerequestbody注解作

11、用:用于猎取哀求体内容,挺直用法得到是key=value&key=value.结构的数据get哀求不适用(get哀求没有哀求体,把参数都封装到地址栏上)属性:required:是否必需有哀求体,默认值true,当取值为true时,get哀求会报错,假如取值为false,get哀求得到是nullpathvariable注解作用:用于绑定url中的占位符,例如:哀求url中/deleteid,这个id就是url占位符。url支持占位符是spring3.0之后加入的。是springmvc支持rest风格url的一个重要标记。属性:value:用于指定url中占位符名称required:是否必需提供占位符 requestmapping("/testpathvariable/sid") public string testpathvariable(pathvaria

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论