首頁 >Java >java教程 >Struts2框架04 struts和spring整合

Struts2框架04 struts和spring整合

PHP中文网
PHP中文网原創
2017-07-09 18:12:461085瀏覽

 目錄

  1 servlet 與 filter 的異同

#  2 記憶體中的字元編碼

  3 gbk和utf-8的特徵

  4 struts和spring的整合

  5 struts和spring的整合步驟

  6 springIOC在action類別中的使用

  7 註解回顧

1 servlet 和 filter 的異同

  1.1 相同點

    filter完全可以代替servlet使用,但是filter中的某些功能servlet不能實現,而servlet的所有功能filter都能實現

  1.2 不同點

    1.2.1 概念上

      servlet 是運行在伺服器端的程序,動態產生web頁面

      filter 是一段可以復用的程式碼,不能產生請求和回應,但是可以對對應和請求做對應的處理

    1.2.2 生命週期上

      servlet 是在web伺服器啟動或web伺服器接收到第一次請求時利用構造器實例化servlet,然後調用init方法進行初始化,之後的每次請求會呼叫doGet或doPost方法進行處理;當關閉伺服器的時候,呼叫destroy方法銷毀實例

      filter 是在web服務器啟動時利用構造器實例化filter,然後調用init方法進行初始化,之後的每次請求調用doFilter方法進行處理,關閉伺服器的時候,呼叫destroy方法銷毀實例

    1.2.3 類型不同

      servlet是類別、filter是介面

  1.3 servlet 、filter的程式實作   

#
<span style="color: #008080"> 1</span> <span style="color: #0000ff">package</span><span style="color: #000000"> cn.xiangxu;
</span><span style="color: #008080"> 2</span> 
<span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> java.io.IOException;
</span><span style="color: #008080"> 4</span> <span style="color: #0000ff">import</span><span style="color: #000000"> java.io.PrintWriter;
</span><span style="color: #008080"> 5</span> 
<span style="color: #008080"> 6</span> <span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.ServletException;
</span><span style="color: #008080"> 7</span> <span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.http.HttpServlet;
</span><span style="color: #008080"> 8</span> <span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.http.HttpServletRequest;
</span><span style="color: #008080"> 9</span> <span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.http.HttpServletResponse;
</span><span style="color: #008080">10</span> 
<span style="color: #008080">11</span> <span style="color: #008000">/**</span>
<span style="color: #008080">12</span> <span style="color: #008000"> * Servlet implementation class DemoServlet
</span><span style="color: #008080">13</span>  <span style="color: #008000">*/</span>
<span style="color: #008080">14</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> DemoServlet <span style="color: #0000ff">extends</span><span style="color: #000000"> HttpServlet {
</span><span style="color: #008080">15</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">final</span> <span style="color: #0000ff">long</span> serialVersionUID = 1L<span style="color: #000000">;
</span><span style="color: #008080">16</span>     
<span style="color: #008080">17</span>     <span style="color: #008000">/**</span>
<span style="color: #008080">18</span> <span style="color: #008000">     * </span><span style="color: #808080">@see</span><span style="color: #008000"> HttpServlet#HttpServlet()
</span><span style="color: #008080">19</span>      <span style="color: #008000">*/</span>
<span style="color: #008080">20</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> DemoServlet() {
</span><span style="color: #008080">21</span>         <span style="color: #0000ff">super</span><span style="color: #000000">();
</span><span style="color: #008080">22</span>         System.out.println("01 利用构造器实例化"<span style="color: #000000">);
</span><span style="color: #008080">23</span> <span style="color: #000000">    }
</span><span style="color: #008080">24</span>     
<span style="color: #008080">25</span> <span style="color: #000000">    @Override
</span><span style="color: #008080">26</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> init() <span style="color: #0000ff">throws</span><span style="color: #000000"> ServletException {
</span><span style="color: #008080">27</span>         <span style="color: #0000ff">super</span><span style="color: #000000">.init();
</span><span style="color: #008080">28</span>         System.out.println("02 利用init方法初始化"<span style="color: #000000">);
</span><span style="color: #008080">29</span> <span style="color: #000000">    }
</span><span style="color: #008080">30</span> 
<span style="color: #008080">31</span>     <span style="color: #008000">/**</span>
<span style="color: #008080">32</span> <span style="color: #008000">     * </span><span style="color: #808080">@see</span><span style="color: #008000"> HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
</span><span style="color: #008080">33</span>      <span style="color: #008000">*/</span>
<span style="color: #008080">34</span>     <span style="color: #0000ff">protected</span> <span style="color: #0000ff">void</span> doGet(HttpServletRequest request, HttpServletResponse response) <span style="color: #0000ff">throws</span><span style="color: #000000"> ServletException, IOException {
</span><span style="color: #008080">35</span>         System.out.println("03 调用处理方法"<span style="color: #000000">);
</span><span style="color: #008080">36</span>         
<span style="color: #008080">37</span>         response.setContentType("text/html;charset:utf-8"); <span style="color: #008000">//</span><span style="color: #008000"> 设置消息头部分信息</span>
<span style="color: #008080">38</span>         response.setCharacterEncoding("utf-8"<span style="color: #000000">);
</span><span style="color: #008080">39</span>         
<span style="color: #008080">40</span>         PrintWriter out =<span style="color: #000000"> response.getWriter();
</span><span style="color: #008080">41</span>         out.println("hello servlet"<span style="color: #000000">);
</span><span style="color: #008080">42</span> <span style="color: #000000">        out.close();
</span><span style="color: #008080">43</span> <span style="color: #000000">    } 
</span><span style="color: #008080">44</span>     
<span style="color: #008080">45</span> <span style="color: #000000">    @Override
</span><span style="color: #008080">46</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> destroy() {
</span><span style="color: #008080">47</span>         <span style="color: #0000ff">super</span><span style="color: #000000">.destroy();
</span><span style="color: #008080">48</span>         System.out.println("04 利用destroy方法销毁"<span style="color: #000000">);
</span><span style="color: #008080">49</span> <span style="color: #000000">    }
</span><span style="color: #008080">50</span> 
<span style="color: #008080">51</span>     <span style="color: #008000">/**</span>
<span style="color: #008080">52</span> <span style="color: #008000">     * </span><span style="color: #808080">@see</span><span style="color: #008000"> HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
</span><span style="color: #008080">53</span>      <span style="color: #008000">*/</span>
<span style="color: #008080">54</span>     <span style="color: #0000ff">protected</span> <span style="color: #0000ff">void</span> doPost(HttpServletRequest request, HttpServletResponse response) <span style="color: #0000ff">throws</span><span style="color: #000000"> ServletException, IOException {
</span><span style="color: #008080">55</span>         <span style="color: #008000">//</span><span style="color: #008000"> TODO Auto-generated method stub</span>
<span style="color: #008080">56</span> <span style="color: #000000">        doGet(request, response);
</span><span style="color: #008080">57</span> <span style="color: #000000">    }
</span><span style="color: #008080">58</span> 
<span style="color: #008080">59</span> }
DemoServlet
#
<span style="color: #008080"> 1</span> <span style="color: #0000ff">package</span><span style="color: #000000"> cn.xiangxu;
</span><span style="color: #008080"> 2</span> 
<span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> java.io.IOException;
</span><span style="color: #008080"> 4</span> <span style="color: #0000ff">import</span><span style="color: #000000"> java.io.PrintWriter;
</span><span style="color: #008080"> 5</span> 
<span style="color: #008080"> 6</span> <span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.Filter;
</span><span style="color: #008080"> 7</span> <span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.FilterChain;
</span><span style="color: #008080"> 8</span> <span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.FilterConfig;
</span><span style="color: #008080"> 9</span> <span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.ServletException;
</span><span style="color: #008080">10</span> <span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.ServletRequest;
</span><span style="color: #008080">11</span> <span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.ServletResponse;
</span><span style="color: #008080">12</span> <span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.http.HttpServletResponse;
</span><span style="color: #008080">13</span> 
<span style="color: #008080">14</span> <span style="color: #008000">/**</span>
<span style="color: #008080">15</span> <span style="color: #008000"> * Servlet Filter implementation class DemoFilter
</span><span style="color: #008080">16</span>  <span style="color: #008000">*/</span>
<span style="color: #008080">17</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> DemoFilter <span style="color: #0000ff">implements</span><span style="color: #000000"> Filter {
</span><span style="color: #008080">18</span> 
<span style="color: #008080">19</span>     <span style="color: #008000">/**</span>
<span style="color: #008080">20</span> <span style="color: #008000">     * Default constructor. 
</span><span style="color: #008080">21</span>      <span style="color: #008000">*/</span>
<span style="color: #008080">22</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> DemoFilter() {
</span><span style="color: #008080">23</span>         System.out.println("01 利用构造器实例化"<span style="color: #000000">);
</span><span style="color: #008080">24</span> <span style="color: #000000">    }
</span><span style="color: #008080">25</span>     
<span style="color: #008080">26</span>     <span style="color: #008000">/**</span>
<span style="color: #008080">27</span> <span style="color: #008000">     * </span><span style="color: #808080">@see</span><span style="color: #008000"> Filter#init(FilterConfig)
</span><span style="color: #008080">28</span>      <span style="color: #008000">*/</span>
<span style="color: #008080">29</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> init(FilterConfig fConfig) <span style="color: #0000ff">throws</span><span style="color: #000000"> ServletException {
</span><span style="color: #008080">30</span>         System.out.println("02 利用init方法初始化"<span style="color: #000000">);
</span><span style="color: #008080">31</span> <span style="color: #000000">    }
</span><span style="color: #008080">32</span>     
<span style="color: #008080">33</span>     <span style="color: #008000">/**</span>
<span style="color: #008080">34</span> <span style="color: #008000">     * </span><span style="color: #808080">@see</span><span style="color: #008000"> Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
</span><span style="color: #008080">35</span>      <span style="color: #008000">*/</span>
<span style="color: #008080">36</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> doFilter(ServletRequest request, ServletResponse resp, FilterChain chain) <span style="color: #0000ff">throws</span><span style="color: #000000"> IOException, ServletException {
</span><span style="color: #008080">37</span>         System.out.println("03 调用处理方法"<span style="color: #000000">);
</span><span style="color: #008080">38</span>         
<span style="color: #008080">39</span>         HttpServletResponse response =<span style="color: #000000"> (HttpServletResponse)resp;
</span><span style="color: #008080">40</span>         
<span style="color: #008080">41</span>         response.setContentType("text/html;charset:utf-8"<span style="color: #000000">);
</span><span style="color: #008080">42</span>         response.setCharacterEncoding("utf-8"<span style="color: #000000">);
</span><span style="color: #008080">43</span>         
<span style="color: #008080">44</span>         PrintWriter out =<span style="color: #000000"> response.getWriter();
</span><span style="color: #008080">45</span>         out.println("hello filter"<span style="color: #000000">);
</span><span style="color: #008080">46</span> <span style="color: #000000">        out.close();
</span><span style="color: #008080">47</span> <span style="color: #000000">    }
</span><span style="color: #008080">48</span>     
<span style="color: #008080">49</span>     <span style="color: #008000">/**</span>
<span style="color: #008080">50</span> <span style="color: #008000">     * </span><span style="color: #808080">@see</span><span style="color: #008000"> Filter#destroy()
</span><span style="color: #008080">51</span>      <span style="color: #008000">*/</span>
<span style="color: #008080">52</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> destroy() {
</span><span style="color: #008080">53</span>         System.out.println("04 利用destroy方法销毁"<span style="color: #000000">);
</span><span style="color: #008080">54</span> <span style="color: #000000">    }
</span><span style="color: #008080">55</span>     
<span style="color: #008080">56</span> }
DemoFilter
#
<span style="color: #008080"> 1</span> <span style="color: #0000ff"><?</span><span style="color: #ff00ff">xml version="1.0" encoding="UTF-8"</span><span style="color: #0000ff">?></span>
<span style="color: #008080"> 2</span> <span style="color: #0000ff"><</span><span style="color: #800000">web-app </span><span style="color: #ff0000">xmlns:xsi</span><span style="color: #0000ff">="http://www.w3.org/2001/XMLSchema-instance"</span><span style="color: #ff0000"> xmlns</span><span style="color: #0000ff">="http://java.sun.com/xml/ns/javaee"</span><span style="color: #ff0000"> xsi:schemaLocation</span><span style="color: #0000ff">="http://java.sun.com/xml/ns/javaee "</span><span style="color: #ff0000"> id</span><span style="color: #0000ff">="WebApp_ID"</span><span style="color: #ff0000"> version</span><span style="color: #0000ff">="2.5"</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 3</span>   <span style="color: #0000ff"><</span><span style="color: #800000">display-name</span><span style="color: #0000ff">></span>servletVsFilter<span style="color: #0000ff"></</span><span style="color: #800000">display-name</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 4</span>   <span style="color: #0000ff"><</span><span style="color: #800000">welcome-file-list</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 5</span>     <span style="color: #0000ff"><</span><span style="color: #800000">welcome-file</span><span style="color: #0000ff">></span>index.html<span style="color: #0000ff"></</span><span style="color: #800000">welcome-file</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 6</span>     <span style="color: #0000ff"><</span><span style="color: #800000">welcome-file</span><span style="color: #0000ff">></span>index.htm<span style="color: #0000ff"></</span><span style="color: #800000">welcome-file</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 7</span>     <span style="color: #0000ff"><</span><span style="color: #800000">welcome-file</span><span style="color: #0000ff">></span>index.jsp<span style="color: #0000ff"></</span><span style="color: #800000">welcome-file</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 8</span>     <span style="color: #0000ff"><</span><span style="color: #800000">welcome-file</span><span style="color: #0000ff">></span>default.html<span style="color: #0000ff"></</span><span style="color: #800000">welcome-file</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 9</span>     <span style="color: #0000ff"><</span><span style="color: #800000">welcome-file</span><span style="color: #0000ff">></span>default.htm<span style="color: #0000ff"></</span><span style="color: #800000">welcome-file</span><span style="color: #0000ff">></span>
<span style="color: #008080">10</span>     <span style="color: #0000ff"><</span><span style="color: #800000">welcome-file</span><span style="color: #0000ff">></span>default.jsp<span style="color: #0000ff"></</span><span style="color: #800000">welcome-file</span><span style="color: #0000ff">></span>
<span style="color: #008080">11</span>   <span style="color: #0000ff"></</span><span style="color: #800000">welcome-file-list</span><span style="color: #0000ff">></span>
<span style="color: #008080">12</span>   <span style="color: #0000ff"><</span><span style="color: #800000">servlet</span><span style="color: #0000ff">></span>
<span style="color: #008080">13</span>     <span style="color: #0000ff"><</span><span style="color: #800000">servlet-name</span><span style="color: #0000ff">></span>servlet<span style="color: #0000ff"></</span><span style="color: #800000">servlet-name</span><span style="color: #0000ff">></span>
<span style="color: #008080">14</span>     <span style="color: #0000ff"><</span><span style="color: #800000">servlet-class</span><span style="color: #0000ff">></span>cn.xiangxu.DemoServlet<span style="color: #0000ff"></</span><span style="color: #800000">servlet-class</span><span style="color: #0000ff">></span>
<span style="color: #008080">15</span>   <span style="color: #0000ff"></</span><span style="color: #800000">servlet</span><span style="color: #0000ff">></span>
<span style="color: #008080">16</span>   <span style="color: #0000ff"><</span><span style="color: #800000">servlet-mapping</span><span style="color: #0000ff">></span>
<span style="color: #008080">17</span>     <span style="color: #0000ff"><</span><span style="color: #800000">servlet-name</span><span style="color: #0000ff">></span>servlet<span style="color: #0000ff"></</span><span style="color: #800000">servlet-name</span><span style="color: #0000ff">></span>
<span style="color: #008080">18</span>     <span style="color: #0000ff"><</span><span style="color: #800000">url-pattern</span><span style="color: #0000ff">></span>/servlet.do<span style="color: #0000ff"></</span><span style="color: #800000">url-pattern</span><span style="color: #0000ff">></span>
<span style="color: #008080">19</span>   <span style="color: #0000ff"></</span><span style="color: #800000">servlet-mapping</span><span style="color: #0000ff">></span>
<span style="color: #008080">20</span>   
<span style="color: #008080">21</span>   <span style="color: #0000ff"><</span><span style="color: #800000">filter</span><span style="color: #0000ff">></span>
<span style="color: #008080">22</span>     <span style="color: #0000ff"><</span><span style="color: #800000">filter-name</span><span style="color: #0000ff">></span>filter<span style="color: #0000ff"></</span><span style="color: #800000">filter-name</span><span style="color: #0000ff">></span>
<span style="color: #008080">23</span>     <span style="color: #0000ff"><</span><span style="color: #800000">filter-class</span><span style="color: #0000ff">></span>cn.xiangxu.DemoFilter<span style="color: #0000ff"></</span><span style="color: #800000">filter-class</span><span style="color: #0000ff">></span>
<span style="color: #008080">24</span>   <span style="color: #0000ff"></</span><span style="color: #800000">filter</span><span style="color: #0000ff">></span>
<span style="color: #008080">25</span>   <span style="color: #0000ff"><</span><span style="color: #800000">filter-mapping</span><span style="color: #0000ff">></span>
<span style="color: #008080">26</span>     <span style="color: #0000ff"><</span><span style="color: #800000">filter-name</span><span style="color: #0000ff">></span>filter<span style="color: #0000ff"></</span><span style="color: #800000">filter-name</span><span style="color: #0000ff">></span>
<span style="color: #008080">27</span>     <span style="color: #0000ff"><</span><span style="color: #800000">url-pattern</span><span style="color: #0000ff">></span>/filter.do<span style="color: #0000ff"></</span><span style="color: #800000">url-pattern</span><span style="color: #0000ff">></span>
<span style="color: #008080">28</span>   <span style="color: #0000ff"></</span><span style="color: #800000">filter-mapping</span><span style="color: #0000ff">></span>
<span style="color: #008080">29</span> <span style="color: #0000ff"></</span><span style="color: #800000">web-app</span><span style="color: #0000ff">></span>
web.xml
#

项目结构图

  

servlet效果图

  

filter效果图

  

 

2 为什么对内存中的字符进行编码

  字符在服务器内存中是16位的char,网络传输的单位是8为的byte,我们必须对数据进行拆分才能够进行传输,拆分的过程我们叫做编码

 

3 描述一下GBK和UTF-8的特点

  utf-8是国际化的最优编码方案,包括了所有的字符,中文占3字节
  gbk是本土化的最优编码方案,包含的字符较少,中文占2字节

 

4 struts2 和 spring 整合

  4.1 为什么要进行整合

    使用spring框架管理组件,实现注入,简化代码

  4.2 怎么整合

    导包(struts2 - spring - plugin)

    配置文件(web.xml  spring-struts.xml)

    struts.xml配置文件

 

5 利用 struts + spring 实现 hello world 案例

  5.1 导包

    struts2核心包、struts和spring的整合插件包

    

<span style="color: #008080"> 1</span> <span style="color: #0000ff"><</span><span style="color: #800000">project </span><span style="color: #ff0000">xmlns</span><span style="color: #0000ff">="http://maven.apache.org/POM/4.0.0"</span><span style="color: #ff0000"> xmlns:xsi</span><span style="color: #0000ff">="http://www.w3.org/2001/XMLSchema-instance"</span><span style="color: #ff0000"> xsi:schemaLocation</span><span style="color: #0000ff">="http://maven.apache.org/POM/4.0.0 "</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 2</span>   <span style="color: #0000ff"><</span><span style="color: #800000">modelVersion</span><span style="color: #0000ff">></span>4.0.0<span style="color: #0000ff"></</span><span style="color: #800000">modelVersion</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 3</span>   <span style="color: #0000ff"><</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>cn.xiangxu<span style="color: #0000ff"></</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 4</span>   <span style="color: #0000ff"><</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>ssh02<span style="color: #0000ff"></</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 5</span>   <span style="color: #0000ff"><</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>0.0.1-SNAPSHOT<span style="color: #0000ff"></</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 6</span>   <span style="color: #0000ff"><</span><span style="color: #800000">packaging</span><span style="color: #0000ff">></span>war<span style="color: #0000ff"></</span><span style="color: #800000">packaging</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 7</span>   <span style="color: #0000ff"><</span><span style="color: #800000">dependencies</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 8</span>       <span style="color: #0000ff"><</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 9</span>           <span style="color: #0000ff"><</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>org.apache.struts<span style="color: #0000ff"></</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>
<span style="color: #008080">10</span>           <span style="color: #0000ff"><</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>struts2-core<span style="color: #0000ff"></</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>
<span style="color: #008080">11</span>           <span style="color: #0000ff"><</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>2.3.8<span style="color: #0000ff"></</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>
<span style="color: #008080">12</span>       <span style="color: #0000ff"></</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080">13</span>       <span style="color: #0000ff"><</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080">14</span>           <span style="color: #0000ff"><</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>org.apache.struts<span style="color: #0000ff"></</span><span style="color: #800000">groupId</span><span style="color: #0000ff">></span>
<span style="color: #008080">15</span>           <span style="color: #0000ff"><</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>struts2-spring-plugin<span style="color: #0000ff"></</span><span style="color: #800000">artifactId</span><span style="color: #0000ff">></span>
<span style="color: #008080">16</span>           <span style="color: #0000ff"><</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>2.3.8<span style="color: #0000ff"></</span><span style="color: #800000">version</span><span style="color: #0000ff">></span>
<span style="color: #008080">17</span>       <span style="color: #0000ff"></</span><span style="color: #800000">dependency</span><span style="color: #0000ff">></span>
<span style="color: #008080">18</span>   <span style="color: #0000ff"></</span><span style="color: #800000">dependencies</span><span style="color: #0000ff">></span>
<span style="color: #008080">19</span> <span style="color: #0000ff"></</span><span style="color: #800000">project</span><span style="color: #0000ff">></span>
maven依赖文件代码

  5.2 配置web.xml

<span style="color: #008080"> 1</span> <?xml version="1.0" encoding="UTF-8"?>
<span style="color: #008080"> 2</span> <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 " version="2.5">
<span style="color: #008080"> 3</span>   <display-name>ssh02</display-name>
<span style="color: #008080"> 4</span>   <welcome-file-list>
<span style="color: #008080"> 5</span>     <welcome-file>index.html</welcome-file>
<span style="color: #008080"> 6</span>     <welcome-file>index.htm</welcome-file>
<span style="color: #008080"> 7</span>     <welcome-file>index.jsp</welcome-file>
<span style="color: #008080"> 8</span>     <welcome-file><span style="color: #0000ff">default</span>.html</welcome-file>
<span style="color: #008080"> 9</span>     <welcome-file><span style="color: #0000ff">default</span>.htm</welcome-file>
<span style="color: #008080">10</span>     <welcome-file><span style="color: #0000ff">default</span>.jsp</welcome-file>
<span style="color: #008080">11</span>   </welcome-file-list>
<span style="color: #008080">12</span>   
<span style="color: #008080">13</span>   <!--<span style="color: #000000"> 配置spring监听
</span><span style="color: #008080">14</span>               目的:容器启动时自动加载一下东西到缓存中 -->
<span style="color: #008080">15</span>   <listener>
<span style="color: #008080">16</span>       <listener-<span style="color: #0000ff">class</span>>org.springframework.web.context.ContextLoaderListener</listener-<span style="color: #0000ff">class</span>>
<span style="color: #008080">17</span>   </listener>
<span style="color: #008080">18</span>   
<span style="color: #008080">19</span>   <!-- 配置Spring配置文件的位置 -->
<span style="color: #008080">20</span>   <context-param>
<span style="color: #008080">21</span>       <param-name>contextConfigLocation</param-name>
<span style="color: #008080">22</span>       <param-value>classpath:spring_*.xml</param-value>
<span style="color: #008080">23</span>   </context-param>
<span style="color: #008080">24</span>   
<span style="color: #008080">25</span>   <!-- 配置主控制器和过滤条件 -->
<span style="color: #008080">26</span>   <filter>
<span style="color: #008080">27</span>       <filter-name>mvc</filter-name>
<span style="color: #008080">28</span>       <filter-<span style="color: #0000ff">class</span>>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-<span style="color: #0000ff">class</span>>
<span style="color: #008080">29</span>   </filter>
<span style="color: #008080">30</span>   <filter-mapping>
<span style="color: #008080">31</span>       <filter-name>mvc</filter-name>
<span style="color: #008080">32</span>       <url-pattern><span style="color: #008000">/*</span><span style="color: #008000"></url-pattern>
</span><span style="color: #008080">33</span> <span style="color: #008000">  </filter-mapping>
</span><span style="color: #008080">34</span>   
<span style="color: #008080">35</span>   
<span style="color: #008080">36</span> <span style="color: #008000"></web-app></span>
web.xml配置文件源代码

    5.2.1 配置spring监听

      

      所需类的位置

        

    5.2.2 配置Spring配置文件的位置

      

      配置文件所处位置

        

    5.2.3 配置主控制器和过滤条件

      

      所需类的位置

        

  5.3 配置spring_context.xml

     只需要在里面配置组件扫描即可

<span style="color: #008080"> 1</span> <span style="color: #0000ff"><?</span><span style="color: #ff00ff">xml version="1.0" encoding="UTF-8"</span><span style="color: #0000ff">?></span>
<span style="color: #008080"> 2</span> <span style="color: #0000ff"><</span><span style="color: #800000">beans </span><span style="color: #ff0000">xmlns</span><span style="color: #0000ff">="http://www.springframework.org/schema/beans"</span>
<span style="color: #008080"> 3</span> <span style="color: #ff0000">    xmlns:xsi</span><span style="color: #0000ff">="http://www.w3.org/2001/XMLSchema-instance"</span><span style="color: #ff0000"> xmlns:context</span><span style="color: #0000ff">="http://www.springframework.org/schema/context"</span>
<span style="color: #008080"> 4</span> <span style="color: #ff0000">    xmlns:jdbc</span><span style="color: #0000ff">="http://www.springframework.org/schema/jdbc"</span><span style="color: #ff0000"> xmlns:jee</span><span style="color: #0000ff">="http://www.springframework.org/schema/jee"</span>
<span style="color: #008080"> 5</span> <span style="color: #ff0000">    xmlns:tx</span><span style="color: #0000ff">="http://www.springframework.org/schema/tx"</span><span style="color: #ff0000"> xmlns:aop</span><span style="color: #0000ff">="http://www.springframework.org/schema/aop"</span>
<span style="color: #008080"> 6</span> <span style="color: #ff0000">    xmlns:mvc</span><span style="color: #0000ff">="http://www.springframework.org/schema/mvc"</span><span style="color: #ff0000"> xmlns:util</span><span style="color: #0000ff">="http://www.springframework.org/schema/util"</span>
<span style="color: #008080"> 7</span> <span style="color: #ff0000">    xmlns:jpa</span><span style="color: #0000ff">="http://www.springframework.org/schema/data/jpa"</span>
<span style="color: #008080"> 8</span> <span style="color: #ff0000">    xsi:schemaLocation</span><span style="color: #0000ff">="
</span><span style="color: #008080"> 9</span> <span style="color: #0000ff">         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
</span><span style="color: #008080">10</span> <span style="color: #0000ff">         http://www.springframework.org/schema/context/spring-context-3.0.xsd
</span><span style="color: #008080">11</span> <span style="color: #0000ff">         http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
</span><span style="color: #008080">12</span> <span style="color: #0000ff">         http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
</span><span style="color: #008080">13</span> <span style="color: #0000ff">         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
</span><span style="color: #008080">14</span> <span style="color: #0000ff">         http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
</span><span style="color: #008080">15</span> <span style="color: #0000ff">         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
</span><span style="color: #008080">16</span> <span style="color: #0000ff">         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
</span><span style="color: #008080">17</span> <span style="color: #0000ff">         http://www.springframework.org/schema/util/spring-util-3.0.xsd"</span><span style="color: #0000ff">></span>
<span style="color: #008080">18</span> 
<span style="color: #008080">19</span>     <span style="color: #008000"><!--</span><span style="color: #008000"> 配置组件扫描 </span><span style="color: #008000">--></span>
<span style="color: #008080">20</span>     <span style="color: #0000ff"><</span><span style="color: #800000">context:component-scan </span><span style="color: #ff0000">base-package</span><span style="color: #0000ff">="cn.xiangxu"</span> <span style="color: #0000ff">/></span>
<span style="color: #008080">21</span>     
<span style="color: #008080">22</span> <span style="color: #0000ff"></</span><span style="color: #800000">beans</span><span style="color: #0000ff">></span>
spring配置文件代码

  5.4 配置struts.xml

<span style="color: #008080"> 1</span> <span style="color: #0000ff"><?</span><span style="color: #ff00ff">xml version="1.0" encoding="UTF-8"</span><span style="color: #0000ff">?></span>
<span style="color: #008080"> 2</span> <span style="color: #0000ff"><!</span><span style="color: #ff00ff">DOCTYPE struts PUBLIC
</span><span style="color: #008080"> 3</span> <span style="color: #ff00ff">    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
</span><span style="color: #008080"> 4</span> <span style="color: #ff00ff">    "http://struts.apache.org/dtds/struts-2.3.dtd"</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 5</span> <span style="color: #0000ff"><</span><span style="color: #800000">struts</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 6</span> 
<span style="color: #008080"> 7</span>     <span style="color: #0000ff"><</span><span style="color: #800000">package </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="test"</span><span style="color: #ff0000"> namespace</span><span style="color: #0000ff">="/test"</span><span style="color: #ff0000"> extends</span><span style="color: #0000ff">="struts-default"</span><span style="color: #0000ff">></span> <span style="color: #008000"><!--</span><span style="color: #008000"> namespace是配置访问路径,extends是配置继承默认struts文件 </span><span style="color: #008000">--></span>
<span style="color: #008080"> 8</span>         <span style="color: #0000ff"><</span><span style="color: #800000">action </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="demo"</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="cn.xiangxu.TestAction"</span><span style="color: #0000ff">></span> <span style="color: #008000"><!--</span><span style="color: #008000"> name是配置访问网名,class是配置action类 </span><span style="color: #008000">--></span>
<span style="color: #008080"> 9</span>             <span style="color: #0000ff"><</span><span style="color: #800000">result </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="success"</span><span style="color: #0000ff">></span>
<span style="color: #008080">10</span> <span style="color: #000000">                /WEB-INF/jsp/msg.jsp
</span><span style="color: #008080">11</span>             <span style="color: #0000ff"></</span><span style="color: #800000">result</span><span style="color: #0000ff">></span>
<span style="color: #008080">12</span>         <span style="color: #0000ff"></</span><span style="color: #800000">action</span><span style="color: #0000ff">></span>
<span style="color: #008080">13</span>     <span style="color: #0000ff"></</span><span style="color: #800000">package</span><span style="color: #0000ff">></span>
<span style="color: #008080">14</span>     
<span style="color: #008080">15</span> <span style="color: #0000ff"></</span><span style="color: #800000">struts</span><span style="color: #0000ff">></span>
struts配置文件

  5.5 编写action类

<span style="color: #008080">1</span> <span style="color: #0000ff">package</span><span style="color: #000000"> cn.xiangxu;
</span><span style="color: #008080">2</span> 
<span style="color: #008080">3</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> TestAction {
</span><span style="color: #008080">4</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> String execute() {
</span><span style="color: #008080">5</span>         System.out.println("struts整合spring"<span style="color: #000000">);
</span><span style="color: #008080">6</span>         <span style="color: #0000ff">return</span> "success"<span style="color: #000000">;
</span><span style="color: #008080">7</span> <span style="color: #000000">    }
</span><span style="color: #008080">8</span> }
action类代码

  5.6 编写所需的JSP文件

<span style="color: #008080"> 1</span> <span style="color: #0000ff"><</span><span style="color: #800000">%@ page </span><span style="color: #ff0000">language</span><span style="color: #0000ff">="java"</span><span style="color: #ff0000"> contentType</span><span style="color: #0000ff">="text/html; charset=utf-8"</span>
<span style="color: #008080"> 2</span> <span style="color: #ff0000">    pageEncoding</span><span style="color: #0000ff">="utf-8"</span><span style="color: #ff0000">%</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 3</span> <span style="color: #0000ff"><!</span><span style="color: #ff00ff">DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 4</span> <span style="color: #0000ff"><</span><span style="color: #800000">html</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 5</span> <span style="color: #0000ff"><</span><span style="color: #800000">head</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 6</span> <span style="color: #0000ff"><</span><span style="color: #800000">meta </span><span style="color: #ff0000">http-equiv</span><span style="color: #0000ff">="Content-Type"</span><span style="color: #ff0000"> content</span><span style="color: #0000ff">="text/html; charset=utf-8"</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 7</span> <span style="color: #0000ff"><</span><span style="color: #800000">title</span><span style="color: #0000ff">></span>test<span style="color: #0000ff"></</span><span style="color: #800000">title</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 8</span> <span style="color: #0000ff"></</span><span style="color: #800000">head</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 9</span> <span style="color: #0000ff"><</span><span style="color: #800000">body</span><span style="color: #0000ff">></span>
<span style="color: #008080">10</span>     <span style="color: #0000ff"><</span><span style="color: #800000">h2</span><span style="color: #0000ff">></span>恭喜你,struts整合spring成功!<span style="color: #0000ff"></</span><span style="color: #800000">h2</span><span style="color: #0000ff">></span>
<span style="color: #008080">11</span> <span style="color: #0000ff"></</span><span style="color: #800000">body</span><span style="color: #0000ff">></span>
<span style="color: #008080">12</span> <span style="color: #0000ff"></</span><span style="color: #800000">html</span><span style="color: #0000ff">></span>
jsp文件代码

  项目结构图

    

 

6 struts整合spring后就可以使用springIOC啦

  例如:持久层的action类中通过依赖注入服务层的类,从而就可以使用服务层的相关属性和方法啦

  6.1 案例改进所需的业务层类

<span style="color: #008080"> 1</span> <span style="color: #0000ff">package</span><span style="color: #000000"> cn.xiangxu.service;
</span><span style="color: #008080"> 2</span> 
<span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.stereotype.Service;
</span><span style="color: #008080"> 4</span> 
<span style="color: #008080"> 5</span> <span style="color: #000000">@Service
</span><span style="color: #008080"> 6</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> TestService {
</span><span style="color: #008080"> 7</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> test() {
</span><span style="color: #008080"> 8</span>         System.out.println("我是业务层的数据哟。"<span style="color: #000000">);
</span><span style="color: #008080"> 9</span> <span style="color: #000000">    }
</span><span style="color: #008080">10</span> }
增加的service类代码

  6.2 案例改进一

    在action类上添加@Controller注解,这样spring容器就会自动给其增加一个bean

    在action类上添加@Scope("prototype")注解,这样就能让每一个请求都有一个action处理类,如果不写就是所有的请求公用一个action处理类(即:默认时使用单例模式)

    在action类中依赖注入业务层,从而实现控制反转

<span style="color: #008080"> 1</span> <span style="color: #0000ff">package</span><span style="color: #000000"> cn.xiangxu.action;
</span><span style="color: #008080"> 2</span> 
<span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> javax.annotation.Resource;
</span><span style="color: #008080"> 4</span> 
<span style="color: #008080"> 5</span> <span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.context.annotation.Scope;
</span><span style="color: #008080"> 6</span> <span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.stereotype.Controller;
</span><span style="color: #008080"> 7</span> 
<span style="color: #008080"> 8</span> <span style="color: #0000ff">import</span><span style="color: #000000"> cn.xiangxu.service.TestService;
</span><span style="color: #008080"> 9</span> 
<span style="color: #008080">10</span> <span style="color: #000000">@Controller
</span><span style="color: #008080">11</span> @Scope("prototype"<span style="color: #000000">)
</span><span style="color: #008080">12</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> TestAction {
</span><span style="color: #008080">13</span>     
<span style="color: #008080">14</span>     @Resource(name="testService"<span style="color: #000000">)
</span><span style="color: #008080">15</span>     <span style="color: #0000ff">private</span><span style="color: #000000"> TestService testService;
</span><span style="color: #008080">16</span>     
<span style="color: #008080">17</span>     <span style="color: #0000ff">public</span><span style="color: #000000"> String execute() {
</span><span style="color: #008080">18</span>         System.out.println("struts整合spring"<span style="color: #000000">);
</span><span style="color: #008080">19</span> <span style="color: #000000">        testService.test();
</span><span style="color: #008080">20</span>         <span style="color: #0000ff">return</span> "success"<span style="color: #000000">;
</span><span style="color: #008080">21</span> <span style="color: #000000">    }
</span><span style="color: #008080">22</span> }
action类改进

  6.3 案例改进二

    struts中的action标签中的class属性值可以用相应bean的id代替

<span style="color: #008080"> 1</span> <span style="color: #0000ff"><?</span><span style="color: #ff00ff">xml version="1.0" encoding="UTF-8"</span><span style="color: #0000ff">?></span>
<span style="color: #008080"> 2</span> <span style="color: #0000ff"><!</span><span style="color: #ff00ff">DOCTYPE struts PUBLIC
</span><span style="color: #008080"> 3</span> <span style="color: #ff00ff">    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
</span><span style="color: #008080"> 4</span> <span style="color: #ff00ff">    "http://struts.apache.org/dtds/struts-2.3.dtd"</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 5</span> <span style="color: #0000ff"><</span><span style="color: #800000">struts</span><span style="color: #0000ff">></span>
<span style="color: #008080"> 6</span> 
<span style="color: #008080"> 7</span>     <span style="color: #0000ff"><</span><span style="color: #800000">package </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="test"</span><span style="color: #ff0000"> namespace</span><span style="color: #0000ff">="/test"</span><span style="color: #ff0000"> extends</span><span style="color: #0000ff">="struts-default"</span><span style="color: #0000ff">></span> <span style="color: #008000"><!--</span><span style="color: #008000"> namespace是配置访问路径,extends是配置继承默认struts文件 </span><span style="color: #008000">--></span>
<span style="color: #008080"> 8</span>         <span style="color: #0000ff"><</span><span style="color: #800000">action </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="demo"</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="testAction"</span><span style="color: #0000ff">></span> <span style="color: #008000"><!--</span><span style="color: #008000"> name是配置访问网名,class是配置action类 </span><span style="color: #008000">--></span>
<span style="color: #008080"> 9</span>             <span style="color: #0000ff"><</span><span style="color: #800000">result </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="success"</span><span style="color: #0000ff">></span>
<span style="color: #008080">10</span> <span style="color: #000000">                /WEB-INF/jsp/msg.jsp
</span><span style="color: #008080">11</span>             <span style="color: #0000ff"></</span><span style="color: #800000">result</span><span style="color: #0000ff">></span>
<span style="color: #008080">12</span>         <span style="color: #0000ff"></</span><span style="color: #800000">action</span><span style="color: #0000ff">></span>
<span style="color: #008080">13</span>     <span style="color: #0000ff"></</span><span style="color: #800000">package</span><span style="color: #0000ff">></span>
<span style="color: #008080">14</span>     
<span style="color: #008080">15</span> <span style="color: #0000ff"></</span><span style="color: #800000">struts</span><span style="color: #0000ff">></span>
sturts配置文件代码改进

  

#7 註解回顧

  @Component@Controller@Service@Repository

#   @Resource@Scope

  @Component 通用的標記(不建議使用,菜鳥可以使用,【AOP編程時可以使用】)

    格式1:@Controller("指定id")   

#    格式2:@Controller  這種方式時使用預設的id,即:類別名稱小寫後的值
  @Controller 作用於控制層
  @Service 作用於業務層#Service 作用於業務層
  @Repository 作用於持久層

  @Scope 避免線程的安全問題,指定值為「prototype",這樣每個請求都會對應一個action處理類別;如果不寫的話就是單例模式(即:所有請求對應一個action處理類別)

    格式:@Scope("prototype")

#  @Resource 依賴注入時的宣告  

    格式:@Resource(name="依賴注入的id")

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

    

以上是Struts2框架04 struts和spring整合的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn