上期講解了第一入門案例之后接下來了解一下視圖解析器與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;
}
}
我們重啟服務器再來訪問,結果如圖顯示,說明我們的視圖解析器起到了一定的作用!
下面我們來講解url-pattern
配置成/的話會攔截到靜態資源所以我們來介紹以下三種解決方案,當然不止三種