目录
一、 InternalResourceViewResolver其他功能
一、 InternalResourceViewResolver其他功能
回顾以前是先通过index.jsp的form表单(action="/a/b/c")访问Controller【根据@RequestMapping("/a/b/c")注解确定】,依据Controller的返回值”success“,通过视图解析器InternalResourceViewResolver,跳转到success.jsp页面】
现在
通过index.jsp页面直接跳转到success.jsp页面
在mvc.xml文件中添加
<mvc:view-controller path="/hhh" view-name="success"/>
运行成功, 可以看到上面是view-name的属性值是success,其实这个当前<mvc:view-controller path="/hhh" view-name="success">也会被视图解析器InternalResourceViewResolver加上前缀和后缀。
注意:
以上mvc:view-controller,会让所有请求转入到mvc:view-controller中匹配的映射地址,而会忽略调@RequestMapping();我们发现再次请求Controller层中@RequestMapping(”/test/hello");会报错404.
如果想让@RequestMapping();与mvc:view-controller共存,则需要注解驱动:mvc-annotation-driven
二、访问静态资源
添加
<mvc:default-servlet-handler/>
三、类型转换器
3.1 定义类型转换器
将String类型转为对象类型Account
import com.lyx.entity.Account;
import org.springframework.core.convert.converter.Converter;
public class MyConverter implements Converter<String, Account> {
@Override
public Account convert(String source) {//source="100-Gavin-123456-9233.78"
String[] split = source.split("-");
//Account(int cardNo, String name, String password, Double balance)
Account account = new Account();
account.setCardNo(Integer.parseInt(split[0]));
account.setName(split[1]);
account.setPassword(split[2]);
account.setBalance(Double.parseDouble(split[3]));
return account;
}
}
3.2 mvc.xml文件
<?xml version="1.0" encoding="utf8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/p
http://www.springframework.org/schema/p/spring-p.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd "
>
<!--注解扫描-->
<context:component-scan base-package="com.lyx"/>
<!--内部资源视图解析-->
<bean id="xx" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--加载国际化资源文件-->
<!--
1. ResourceBundleMessageSource在程序加载时,加入springMvc,springMvc在启动时,
会自动查找一个id=“messageSource”的bean,如果由则自动加载
2. 如果配置了ResourceBundleMessageSource,则该类会在程序响应时介入
-->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="myproperties.i18n"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<mvc:view-controller path="/hhh" view-name="success"/>
<mvc:default-servlet-handler/>
<!--将自定义转换器纳入SpringIOC容器-->
<bean id="myConverter" class="com.lyx.converter.MyConverter"/>
<!--将上面定义的myConverter纳入SpringMVC提供的转换器Bean中-->
<bean id="myconversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="myConverter"/>
</set>
</property>
</bean>
<!--将上面的myconversionService注册到annotation-driven中-->
<mvc:annotation-driven conversion-service="myconversionService"/>
</beans>
3.3 测试
index.jsp
看到name=“accountInfo”
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/test/testMyConverter" method="post">
账户信息: <input type="text" name="accountInfo">
<input type="submit" value="点击提交数据">
</form>
</body>
</html>
Controller【重点:@RequestParam】
@RequestParam("accountInfo")触发转换器, @RequestParam接收到前端传递的数据:accountInfo="100-Gavin-123456-9233.78",并要把这个数据赋值给形参Account, SpringMVC可以发现接收的数据和目标数据Account类型不一致,并且这两种数据String类型和Account类型,正好符合转换器MyConverter.java[public class MyConverter implements Converter<String, Account>
@RequestMapping("/testMyConverter")
public String testMyConverter(@RequestParam("accountInfo") Account account){
System.out.println(account);
return "success";
}