>  기사  >  Java  >  Struts2 프레임워크 04 Struts 및 스프링 통합

Struts2 프레임워크 04 Struts 및 스프링 통합

PHP中文网
PHP中文网원래의
2017-07-09 18:12:461067검색

카탈로그

 1 서블릿과 필터의 유사점과 차이점

 2 메모리 내 문자 인코딩

 3 gbk 및 utf-8의 기능

 4 스트럿과 스프링의 통합

 5 Struts 및 스프링 통합 단계

 6 액션 클래스에서 springIOC 사용

 7 주석 검토

1 서블릿과 필터의 유사점과 차이점

 1.1 유사점

  필터를 서블릿 대신 사용할 수 있지만, 필터의 일부 기능은 서블릿으로 구현할 수 없지만 서블릿의 모든 기능은 필터로 구현할 수 있습니다

 1.2 차이점

  1.2.1 컨셉

   서블릿은 서버 측에서 실행되고 동적으로 웹 페이지

를 생성하는 프로그램입니다.

   필터는 재사용할 수 있는 코드 조각입니다. 요청과 응답을 생성할 수는 없지만응답과 요청을 그에 따라 처리할 수 있습니다

  1.2.2 수명주기

   서블릿은 웹 서버가 시작되거나 웹 서버가 첫 번째 요청을 받을 때 생성자

를 사용하여 servlet을 인스턴스화한 다음 init 메서드를 호출하여 initialize를 호출합니다. 각 후속 요청은 처리를 위해 doGet 또는 doPost 메서드를 호출합니다. ;서버를 종료할 때 destroy 메소드 를 호출하여 인스턴스를 삭제합니다.   Filter는 생성자

를 사용하여 웹 서버가 시작될 때

filter를 인스턴스화한 다음 init 메서드를 호출하여 initialize를 수행합니다. 각 후속 요청 은 서버가 종료되면 처리를 위해 doFilter 메서드를 호출합니다. destroy 예제 를 위해 호출됩니다.  1.2.3 다양한 유형

   서블릿은 클래스이고, 필터는 인터페이스입니다

 1.3 서블릿 및 필터 프로그래밍 구현 

으아악 데모서블릿
으아악 데모필터
으아악 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는 비즈니스 레이어에서 작동합니다.
@Repository는 지속성 레이어

@Scope 스레드 안전 문제를 방지하려면 값을 "프로토타입"으로 지정하세요. 그러면 각 요청이 기록되지 않은 경우 작업 처리 클래스에 해당하고 싱글톤 모드가 됩니다. 액션처리수업)

 형식: @Scope("프로토타입")

  종속성 주입 중 @Resource 선언

 형식: @Resource(name="의존성 주입 ID")

  

위 내용은 Struts2 프레임워크 04 Struts 및 스프링 통합의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.