目录
一、 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,会让所有请求转入到mvc:view-controller中匹配的映射地址,而会忽略调@RequestMapping();我们发现再次请求Controller层中@RequestMapping(”/test/hello");会报错404.
如果想让@RequestMapping();与mvc:view-controller共存,则需要注解驱动:mvc-annotation-driven
添加
将String类型转为对象类型Account
- import com.lyx.entity.Account;
- import org.springframework.core.convert.converter.Converter;
-
- public class MyConverter implements Converter
{ - @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;
- }
- }
- "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>
-
-
-
- <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/>
-
-
- <bean id="myConverter" class="com.lyx.converter.MyConverter"/>
-
- <bean id="myconversionService"
- class="org.springframework.context.support.ConversionServiceFactoryBean">
- <property name="converters">
- <set>
- <ref bean="myConverter"/>
- set>
- property>
- bean>
-
- <mvc:annotation-driven conversion-service="myconversionService"/>
- beans>
看到name=“accountInfo”
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- <form action="${pageContext.request.contextPath}/test/testMyConverter" method="post">
- 账户信息: <input type="text" name="accountInfo">
- <input type="submit" value="点击提交数据">
- form>
- body>
- html>
@RequestParam("accountInfo")触发转换器, @RequestParam接收到前端传递的数据:accountInfo="100-Gavin-123456-9233.78",并要把这个数据赋值给形参Account, SpringMVC可以发现接收的数据和目标数据Account类型不一致,并且这两种数据String类型和Account类型,正好符合转换器MyConverter.java[public class MyConverter implements Converter
- @RequestMapping("/testMyConverter")
- public String testMyConverter(@RequestParam("accountInfo") Account account){
- System.out.println(account);
- return "success";
- }