Inhaltsverzeichnis
1Ähnlichkeiten und Unterschiede zwischen Servlet und Filter
2 Zeichenkodierung im Speicher
3 Funktionen von GBK und UTF-8
4 Integration von Streben und Feder
5 Integrationsschritte von Streben und Feder
6 Die Verwendung von springIOC in Aktionsklassen
7 Anmerkungsüberprüfung
Filter kann anstelle von Servlet verwendet werden, aber einige Funktionen im Filter können nicht durch Servlet realisiert werden, während alle Funktionen des Servlets durch Filter realisiert werden können
Servlet ist ein Programm, das auf der Serverseite ausgeführt wird und dynamisch Webseiten
generiertFilter ist ein Teil des Codes, der wiederverwendet werden kann , keine Anfragen und Antworten generieren kann, aber Antworten und Anfragen entsprechend verarbeiten kann 1.2.2 Lebenszyklus
Servlet zu instanziieren, wenn der Webserver startet oder der Webserver die erste Anfrage empfängt, und ruft dann die Init-Methode zum Initialisieren und bei jeder weiteren Anfrage auf Ruft die Methode doGet oder doPost zur Verarbeitung auf; beim Schließen des Servers wird die Methode destroy aufgerufen, um die -Instanz zu zerstören Filter verwendet den Konstruktor
, umFilter zu instanziieren, wenn der Webserver startet, ruft dann die Init-Methode auf, um initialisieren, und jede nachfolgende Anfrage ruft die doFilter-Methode auf Rufen Sie beim Verarbeiten und Herunterfahren des Servers die Zerstörungsmethode auf, um die -Instanz zu zerstören
1.2.3 Verschiedene Typen
1.3 Programmierimplementierung von Servlet und 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效果图
字符在服务器内存中是16位的char,网络传输的单位是8为的byte,我们必须对数据进行拆分才能够进行传输,拆分的过程我们叫做编码
utf-8是国际化的最优编码方案,包括了所有的字符,中文占3字节
gbk是本土化的最优编码方案,包含的字符较少,中文占2字节
使用spring框架管理组件,实现注入,简化代码
导包(struts2 - spring - plugin)
配置文件(web.xml spring-struts.xml)
struts.xml配置文件
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>
<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>
所需类的位置
配置文件所处位置
所需类的位置
只需要在里面配置组件扫描即可
<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>
<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>
<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> }
<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>
项目结构图
例如:持久层的action类中通过依赖注入服务层的类,从而就可以使用服务层的相关属性和方法啦
<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> }
在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> }
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>
@Component@Controller@Service@Repository
@Resource@Scope
@Component Universal-Tag (nicht empfohlen, kann von Anfängern verwendet werden, [kann während der AOP-Programmierung verwendet werden])
Format 1: @Controller("angegebene ID")
Format 2: @Controller Bei dieser Methode wird die Standard-ID verwendet, also der Wert nach dem Klassennamen in Kleinbuchstaben
@Controller wirkt auf der Steuerungsebene
@Service Wirkt auf der Geschäftsschicht
@Repository wirkt auf der Persistenzschicht
@Scope vermeidet Thread-Sicherheitsprobleme und gibt den Wert als „Prototyp“ an, sodass jede Anfrage einer Aktionsverarbeitungsklasse entspricht; geschrieben, es wird ein Singleton-Modus sein (d. h.: Alle Anfragen entsprechen einer Aktionsverarbeitungsklasse)
Format: @Scope("prototype")
@Ressourcendeklaration während der Abhängigkeitsinjektion
Format: @Resource(name="Abhängigkeitsinjektions-ID")
Das obige ist der detaillierte Inhalt vonStruts2 Framework 04 Streben und Federintegration. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!