Spring MVC之视图解析器和URL-Pattern的配置方案

上期讲解了第一入门案例之后接下来了解一下视图解析器与URL-Pattern的配置方案

先来说视图解析器,在上次博客文章中我们完成了入门案例,接下来我们就在上一个例子中完善一下体出视图解析器

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
"> <!-- 注册视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀后缀 -->
<property name="prefix" value="WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 注册控制器--> <bean id="/hello.do" class="cn.lxp.controller.MyController"></bean> </beans>

配置好视图解析器之后我们的控制器类中的锁定页面就可以直接写页面名称不用管后缀与前缀了

public class MyController implements Controller {

    /**
* handleRequest 处理请求
* ModelAndView 返回的类型
*/
@Override
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception { ModelAndView mav=new ModelAndView();
mav.addObject("msg", "我们天生爱分享!!");
//进行处理一道
mav.setViewName("index"); return mav;
} }

我们重启服务器再来访问,结果如图显示,说明我们的视图解析器起到了一定的作用!

Spring MVC之视图解析器和URL-Pattern的配置方案

下面我们来讲解url-pattern

Spring MVC之视图解析器和URL-Pattern的配置方案

Spring MVC之视图解析器和URL-Pattern的配置方案

配置成/的话会拦截到静态资源所以我们来介绍以下三种解决方案,当然不止三种

Spring MVC之视图解析器和URL-Pattern的配置方案

Spring MVC之视图解析器和URL-Pattern的配置方案

Spring MVC之视图解析器和URL-Pattern的配置方案

上一篇:【Java学习笔记之三十四】超详解Java多线程基础


下一篇:kali linux 2.0安装sublime text 2