03SpringMvc_自定義的spring.xml配置文件和邏輯視圖名


 

這篇文章的目的是實現Struts2中一種形式(封裝視圖的邏輯名稱),在Struts2中Action處理后會返回"SUCCESS"這樣,然后根據"SUCCESS"跳轉到相對應的Jsp頁面,但是前一篇文章中直接配的是modelAndView.setViewName("/jsp/success.jsp");。所以這篇文章實現上面那個功能(封裝視圖的邏輯名稱)。

 

 

 

 

 

 

--------------------------------------------------------------------------------------------------------------------------------------------------------------

但是在實現這個功能之前,先講另外一個擴展。

我們在上篇文章中寫了一個入門小案例,其中在/WEB-INF下有一個DispatcherServlet-servlet.xml配置文件,那么我現在覺得DispatcherServlet-servlet.xml很難聽,想換個名字,且想換個地方,不放在/WEB-INF下面了,想放在src目錄下,怎么辦?

第1步.把這個文件剪切到src下面。重命名為springmvc.xml.

第2步.我既然改了名字又換了地方。那我要讓核心過濾器能找到這個問價才行啊,只要在web.xml中配置初始化參數就可以了。

在web.xml中修改如下。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringMvc_10day_self</display-name>
  <servlet>
 
  <servlet-name>DispatcherServlet</servlet-name>
  <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <!-- 通知DispatcherServlet去指定目錄下找到springmvc.xml配置文件 -->
 <!-- 
 注意這里的  <param-name>contextConfigLocation</param-name>一個字母都不能有錯
 一旦有錯就會去WEB-INF下面去找
  -->
          <init-param>
               <param-name>contextConfigLocation</param-name>
              <param-value>classpath:springmvc.xml</param-value>
          </init-param>
 </servlet>
 <servlet-mapping>
   <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>*.action</url-pattern>
 
 </servlet-mapping>
 
  <welcome-file-list>
   
    <welcome-file>index.jsp</welcome-file>
  
  </welcome-file-list>
</web-app>

 

------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

 

 

 

 

 

 

 

好,言歸正傳,我們實現"SUCCESS"這個功能(現在封裝視圖的邏輯名稱)。

第一步:修改HelloAction.java文件。如下

public class HelloAction implements Controller {
/**
 * 
 */
public HelloAction() {

System.out.print("創建了一個Action");
}
    @Override
    public ModelAndView handleRequest(HttpServletRequest arg0,HttpServletResponse arg1) throws Exception {
        System.out.println("this is HelloAction handleRequest");
        ModelAndView modelAndView=new ModelAndView();
          modelAndView.addObject("message","這是我得第一個SpringMvc應用程序");
          //原來封裝視圖的真實路徑
          // modelAndView.setViewName("/jsp/success.jsp");
          //現在封裝視圖的邏輯名稱。什么是邏輯名稱呢?就是Struts2中的"Success"這樣的。
          modelAndView.setViewName("success");
        return modelAndView;
    
    }

}

第二步:修改上面的由DispatcherServlet-servlet.xml修改過來的springmvc.xml。我們把springmvc.xml里面的內容拆來,各個功能模塊單獨配成xml配置文件。

具體做法如下:

總的案例結構圖:

          

springmvc.xml是總的Springmvx配置文件。而位於springmvc_001.xml是分配置文件。總的springmvc.xml文件包含分的springmvc_001.xml配置文件。

總的springmvc_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: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-3.0.xsd
      
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
       
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
>
<import resource="com/guigu/shen/Action/springmvc_001.xml"/>
</beans>

分的springmvc_001.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: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-3.0.xsd
      
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
       
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
>
     <!-- 控制器(程序員)(必須配置) -->
<bean name="/hello.action" class="com.guigu.shen.Action.HelloAction"></bean>
 <!-- 如果Action匯總書寫的是視圖邏輯名稱,那么視圖解析器就必須配置(解釋一下什么是視圖邏輯名稱:就是類似Struts2中的,"success")
               如果Action中配置的是視圖真實名稱,那么視圖解析器就可選配置(解釋一下什么是視圖真實名稱,就是"/jsp/success.jsp")
 -->
 <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <!-- 配置路徑前綴 -->
 <property name="prefix" value="/jsp/"></property>
 <!-- 配置路徑后綴 -->
 <property name="suffix" value=".jsp"></property>
 <!-- 上面的配置方法其實就是前綴+視圖邏輯名+后綴=真實路徑 -->
 </bean>
</beans>

第三步:在urL中輸入:http://127.0.0.1:8080/SpringMvc_10day_self/hello.action

 

 

運行結果:正確。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM