目录
@RequestParam:
Pojo : required false 表示不用必须带 defaulValue 表示默认值
然后输入这个网址就可得到结果了
@PathVariable:与上面对比
也是接收 但是 他的 RequestMapping里面的地址不一样 必须是这样:
网址得这样:
不然要么报错 要么就是这样:
两者还是取别挺大的。
mvc:view-controller
在dispatcher-servlet.xml配置!
作用直接跳转页面 :
没这个标签之前 :
添加标签后:
标签的配置:
<mvc:view-controller path="/success" view-name="success"></mvc:view-controller>
path 对应你的搜索栏输入啥 view-name 对应要转的那个地方 应为我前面有:
<bean id="resolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
可以得到位置
但是这样设置后原本的 的 requsetMapping 无法用需要在加一个标签:
<mvc:annotation-driven></mvc:annotation-driven>
springMvc下的转发和重定向
很简单 在 对应的方法下 return 加上
forward:url 即为转发
redirect:url 即为 重定向
注 WEB-INF下的需要转发才可以访问 重定向不行。
或者用最开始的那种也行。
form标签:
首先导入 对应的网址
注意表头:的格式:
modelAttribute 表示这个表单对应的类 path 相当于name。
再看header类的配置:
goupdate: 是在获取user后传给form 达到一个回显的目的
update :保存数据 用了modelAttribute 是题目要求 某些字段不修改
getuser: 提前获取数据然后合并 form得到的 构成新的 user :
注意: form 会自动把 里面的数值 以?id=X …(就是表单里面的 path 输入)
比如如图 可以得到传过来的id 和age等。
自定义类转换器:
配置转换类:
@Component
public class UserContler implements Converter<String, User> {
// name-email-sex-age
@Override
public User convert(String s) {
User user=null;
if(s!=null){
String[] split = s.split("-");
if(split!=null&&split.length==4){
user=new User(null,split[0],split[1],split[2],Integer.parseInt(split[3]));
}
}
return user;
}
}
注意他的implement 类即可 我传过来的数据是 以 “-” 分割的
接着配置 handler:
然后配置dispacher.xml 把他们串联起来:
然后再随便 配置 jsp文件 就可以启动了:
如果出现了这种类似的错误:
就是你的mvc:annotation-driven 写 conversion-service 冒红的话 你需要 配置一下 dispacher.xml 的头部 加一个这个 :
xmlns:mvc="http://www.springframework.org/schema/mvc"
然后再在 xsi:schemaLocation:后面的网址加一个:
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
我的 到目前为止 dispacher.xml 配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- xsi:schemaLocation="http://www.springframework.org/schema/beans-->
<!-- http://www.springframework.org/schema/beans/spring-beans.xsd -->
<!-- http://www.springframework.org/schema/context -->
<!-- https://www.springframework.org/schema/context/spring-context.xsd -->
<!-- http://www.alibaba.com/schema/stat -->
<!-- http://www.alibaba.com/schema/stat.xsd"-->
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>
<!-- 数据库连接池的链接-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${prope.url}" />
<property name="username" value="${prope.username}" />
<property name="password" value="${prope.password}" />
<property name="driverClassName" value="${prope.driverClass}" />
</bean>
<!-- 配置jdbctemplate 类-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- zyc.handlers,zyc.pojo,zyc.service,zyc.dao-->
<context:component-scan base-package="zyc"></context:component-scan>
<bean id="Resolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven conversion-service="conversionService"/>
<!-- 配置ConversionService-->
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters" >
<set>
<ref bean="userContler"></ref>
</set>
</property>
</bean>
</beans>
在handler 类中配置即可:
注意
• @InitBinder方法不能有返回值,它必须声明为void。
• @InitBinder方法的参数通常是是 WebDataBinder
用处: 后面可以用在 数据转换上面 比如输入字符串 帮忙转换成 日期 等等。(后面再补充)
@ResponseBody与@RequestBody(实现文件上传)(一般成对)
第一步:在index 页面写好表单:
<form action="/hi/textfiledown" method="post" enctype="multipart/form-data">
File:<input type="file" name="file">
<br>
Desc:<input type="text" name="desc">
<br>
<input type="submit" name="Submit">
</form>
第二步: 配置对应的方法即可:
@ResponseBody
@RequestMapping(value = "/textfiledown")
public String textfiledown(@RequestBody String body){
System.out.println(body);
return "hello"+new Date();
}
结果会把 返回值打赢打客户端 然后内容输入到 控制台
注意默认的接收方法是 post
fileupload 实现文件的上传
第一步 导入对应的 jar包
第二步: 编写xml:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 编译语言-->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 文件的限制大小-->
<property name="maxUploadSize" value="1024000"></property>
</bean>
第三步 写好对应的方法 和 index :
@RequestMapping("/textFileupload")
public String textFileupload(@RequestParam("desc") String desc ,@RequestParam("file") MultipartFile file){
System.out.println("desc:"+desc);
System.out.println("name"+file.getOriginalFilename());
InputStream inputStream = null;
try {
inputStream = file.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(inputStream);
return SUCCESS;
}
index:
<form action="/hi/textFileupload" method="post" enctype="multipart/form-data">
File:<input type="file" name="file">
<br>
Desc:<input type="text" name="desc">
<br>
<input type="submit" name="Submit">
</form>
第四步: 跑起来:
自定义 一个拦截器:
第一步:创建好拦截器 这个类:
就记得实现好对应的方法:然后重载对应的三个方法
!!!注意 记得返回true 不然 后面的方法不会执行
第二步:再对应的servlet.xml 里面配置好:
<mvc:interceptors>
<bean class="zyc.FirstInitercroptor"></bean>
</mvc:interceptors>
第三步:跑起来顺序介绍:
首先 先是prehandle 先执行 随后转到对应的方法里面去 执行对应的方法
然后是 posthandle 在执行 随后转到对应的视图层去(jsp)
最后执行 afterCompletion 。
补充:
mvc:interceptor标签:
<mvc:interceptors>
<bean class="zyc.FirstInitercroptor"></bean>
<mvc:interceptor>
<mvc:mapping path="/hi/textmap"/>
<bean class="zyc.FirstInitercroptor2"></bean>
</mvc:interceptor>
</mvc:interceptors>
表示 路径包含了 texttmap 才会执行这个 拦截器。
多个拦截器的执行顺序:
如果出现一个return false 后的执行顺序:
springMvc的执行顺序:
口头描述:
首先请求转到DispatcherServlet 进行判断 是否存在对应的 映射(就是那个requestmapping)
如果没有对应的映射 查看是否配置了 mvc:default-servlet-handler
(比如请求是XX.html 没配置这个的 <mvc:default-servlet-handler> 的话 直接就404) 如果存在对应的静态资源转到目标资源 ,没有就进入404
如果有对应的映射
由handlerMapping 获取handlerExecutionChain 对象 在获取 handlerAdapter对象
在调用 拦截器(你没设置就是默认的)的Perhandle方法 。。。。剩下的看图就差不多了。
Spring 与 SpringMvc 整合:
spring 对应的xml(默认的话:applicationContext.xml) 负责配置一些数据源,事务等
springMvc 的xml (默认的话:dispatcher-servlet.xml) 负责配置handler就可以了
配置:
错误演示:
首先创建一个service 和一个 controller 来测试:
然后再 dispatcher-servlet.xml和applicationContext.xml 配置扫描的:
<context:component-scan base-package="zyc"></context:component-scan>
扫描的都是 所有类的包
到那时这样 会产生:
他让每个都 建立了两次 这是不行的
正确演示:
再dispatcher-servlet.xml配置:
<context:component-scan base-package="zyc" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
applicationContext.xml :
<context:component-scan base-package="zyc" use-default-filters="true">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
注意 貌似 用了 exclude 的 后面这个得是true use-default-filters=“true” 不然他 扫描不到 包 :
这是 true:
这是改成false:
这样就会影响 @autowrite :
然后跑起来;
IOC之间引用:
springMvc 可以用spring 的类容 即:
反之则不可:
就是 controller 和ControllerAdvice 属于springMvc
然后 其他的 属于 spring(前提是 按照我的那个配置来)