zoukankan      html  css  js  c++  java
  • struts2的搭建(包括与spring整合)流程(转)

    一.非集成Spring

    1.创建javaWeb工程

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

    2.找到开发Struts2应用需要使用到的jar文件: 

    到http://struts.apache.org/download.cgi下载struts-2.x.x-all.zip,目前最新版为2.3.1.1。下载完后解压文件,开发struts2应用需要依赖的jar文件在解压目录的lib文件夹下。不同的应用需要的JAR包是不同的。下面给出了开发Struts2程序最少需要的JAR。

    导入需要的jar包
      struts2-core-2.3.1.1.jar:Struts 2框架的核心类库
      xwork-core-2.3.1.1.jar:Command模式框架,WebWork和Struts2都基于xwork 
      ognl-3.0.3.jar:对象图导航语言(Object Graph NavigationLanguage),struts2框架通过其读写对象的属性(不是struts框架的jar包)
      freemarker-2.3.18.jar:Struts 2的UI标签的模板使用FreeMarker编写(可以替代jsp页面)
      commons-fileupload-1.2.2.jar: 文件上传组件,2.1.6版本后需要加入此文件
      commons-io-2.0.1.jar:传文件依赖的jar包
      commons-lang-2.5.jar:对java.lang包的增强
      commons-logging-1.1.x.jar:ASF出品的日志包,Struts 2框架使用这个日志包来支持Log4J和JDK 1.4+的日志记录
      freemarker-2.3.18.jar:Struts 2的UI标签的模板使用FreeMarker编写
      asm-3.3.jar:提供了字节码的读写的功能,包含了核心的功能,而其他的jar包都是基于这个核心的扩展.
      asm-commons-3.3.jar:提供了基于事件的表现形式。
      asm-tree-3.3.jar:提供了基于对象的表现形式。 
      javassist-3.11.0.GA.jar:代码生成工具, struts2用它在运行时扩展 Java类

    这个是Struts2.0给的HelloWorld例子其中引入了。

    freemarker-2.3.16.jar

    ognl-3.0.jar

    struts2-core-2.2.1.1.jar

    xwork-core-2.2.1.1.jar

    commons-logging-1.0.4.jar

    这5个JAR包,但在2.2版本中少了下面三个JAR就会报错,所以要把这三个JAR包也要引入到项目中

    commons-fileupload-1.2.1.jar

    javassist-3.7.ga.jar

    commons-io-1.3.2.jar

    缺少javassist-3.7.ga.jar会报 找不到javassist.ClassPool 这个类。

    缺少了commons-fileupload-1.2.1.jar 会报如下错误

    Dispatcher initialization failed
    Unable to load configuration. - bean - jar:file:/E:/Program%20Files/tomcat6/tomcat6/webapps/Struts2ZeroC/WEB-INF/lib/struts2-core-2.2.1.1.jar!/struts-default.xml:48:178

    缺少了commons-io-1.3.2.jar会报Could not create JarEntryRevision for……

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

    3.web.xml中配置常量方法: 

    核心控制器就是在web.xml中配置的StrutsPrepareAndExecutorFilter;

    任何一个处理请求都会经过StrutsPrepareAndExecutorFilter,并由他进行分发;

    通过配置初始化参数的方式配置常量;

    [html] view plaincopy
     
     
     
    1. <pre class="html" name="code"><filter>  
    2.     <filter-name>struts2</filter-name>  
    3.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
    4.     <init-param>  
    5.         <param-name>struts.action.extension</param-name>  
    6.         <param-value>do</param-value>  
    7.     </init-param>  
    8. </filter>  
    9. <filter-mapping>  
    10.     <filter-name>struts2</filter-name>  
    11.     <url-pattern>/*</url-pattern>  
    12. </filter-mapping>  
    13.    



     

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

    4.编写Struts2的配置文件 : 

    编写完Action后需要在struts.xml中配置该Action,目的是将execute()方法的返回值(逻辑视图)和物理视图关联起来,简单地说就是确定返回某个值时跳转到某个页面;

    [html] view plaincopy
     
     
     
    1. <?xml version="1.0" encoding="GBK" ?>  
    2. <!DOCTYPE struts PUBLIC  
    3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
    4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
    5.   
    6. <struts>  
    7.     <constant name="struts.devMode" value="true"></constant>  
    8.       
    9.     <package name="MyPackage" extends="struts-default" namespace="/">  
    10.         <action name="loginAction" class="org.login.action.LoginAction">  
    11.             <result name="success">/success.jsp</result>  
    12.             <result name="error">/fail.jsp</result>  
    13.         </action>  
    14.     </package>  
    15. </struts>  

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

    5.创建action文件:

    自己编写的Action通常需要继承ActionSupport类,我们通常需要覆写 public String execute()throws Exception{}方法;

    因为此类规定了一些规范,比如在此类中定义了5个常量: SUCCESS、ERROR、LOGIN、INPUT、NONE;

    这5个常量的目的是规定execute方法的返回值;

    在Action中可以定义属性,此属性必须要规定setter和getter方法,此属性的用途是保存传递进来的数据;比如此登录应用中Action需要有两个属性:user属性和password属性,用来保存用户名和密码;

    LoginAction.java

    [java] view plaincopy
     
     
     
    1. package org.login.action;  
    2.   
    3. import com.opensymphony.xwork2.ActionSupport;  
    4.   
    5. public class LoginAction extends ActionSupport{  
    6.     private String user;  
    7.     private String password;  
    8.     public String execute()throws Exception{  
    9.         if(user.equals("xiazdong")&&password.equals("12345")){  
    10.             return SUCCESS;  
    11.         }  
    12.         else{  
    13.             return ERROR;  
    14.         }  
    15.     }  
    16.     public String getUser() {  
    17.         return user;  
    18.     }  
    19.     public void setUser(String user) {  
    20.         this.user = user;  
    21.     }  
    22.     public String getPassword() {  
    23.         return password;  
    24.     }  
    25.     public void setPassword(String password) {  
    26.         this.password = password;  
    27.     }  
    28.       
    29. }  

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

    6.创建jsp文件

    登录页面、登录成功页面、登录失败页面分别为login.jsp,success.jsp,fail.jsp

    login.jsp

    [html] view plaincopy
     
     
     
     
    1. <%@ page language="java" contentType="text/html; charset=utf-8"  
    2.     pageEncoding="utf-8"%>  
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    4. <html>  
    5. <head>  
    6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
    7. <title>登录界面</title>  
    8. </head>  
    9. <body>  
    10.     <form action="loginAction">  
    11.         用户名:<input type="text" name="user"/><br />  
    12.         密码:<input type="password" name="password"/><br />  
    13.         <input type="submit" value="登录"/>   
    14.     </form>  
    15. </body>  
    16. </html>  

    success.jsp

    [html] view plaincopy
     
     
     
     
    1. <%@ page language="java" contentType="text/html; charset=utf-8"  
    2.     pageEncoding="utf-8"%>  
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    4. <html>  
    5. <head>  
    6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
    7. <title>登录成功界面</title>  
    8. </head>  
    9. <body>  
    10.     登陆成功!!!  
    11. </body>  
    12. </html>  

    fail.jsp

    [html] view plaincopy
     
     
     
     
    1. <%@ page language="java" contentType="text/html; charset=utf-8"  
    2.     pageEncoding="utf-8"%>  
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    4. <html>  
    5. <head>  
    6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
    7. <title>登录失败界面</title>  
    8. </head>  
    9. <body>  
    10.     登陆失败!!!  
    11. </body>  
    12. </html> 

    二.集成Spring

    1、导入依赖包

    除了导入Struts2和Spring的核心库之外,还要导入commons-logging和struts2-spring-plugin包,否则启动会出异常

    2、web.xml的配置

    既然有Struts2,核心拦截器的配置是不可少的

    <filter>
         <filter-name>struts2</filter-name>
         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
         <filter-name>struts2</filter-name>
         <url-pattern>/*</url-pattern>
    </filter-mapping>

    通过配置ContextLoaderListener监听器,使容器启动时,自动加载applicationContext配置,

    因为它实现了ServletContextListener这个接口,容器启动时会自动执行它实现的方法。

    <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    默认情况下,会加载WEB-INF/applicationContext.xml这个文件,我们可以通过配置contextConfigLocation参数改变配置文件的路径

    <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>WEB-INF/classes/applicationContext.xml</param-value>
    </context-param>

    以上配置均在web.xml文件的<web-app></web-app>区域

    3、测试类

    在浏览器请求一个Action方法,在Action方法内向一个对象请求一个List,然后转到index.jsp页面,在页面中输出Action请求到的List。

    通过Spring依赖配置,控制Action请求的对象。

    首先要编写一个接口,Action方法依赖这个接口,通过调用接口中的方法获取List

    public interface IocTestInterface {
         public List getList();
    }

    下面编写Action类,这个类继承ActionSupport类,并覆盖其中的execute方法,

    execute方法执行时,调用实现了上述接口对象的getList方法获取List

    public class IocAction extends ActionSupport {
    	private IocTestInterface iti;
    	private List list;
    	
    	public List getList() {
    		return list;
    	}
    	public void setList(List list) {
    		this.list = list;
    	}
    	public IocTestInterface getIti() {
    		return iti;
    	}
    	public void setIti(IocTestInterface iti) {
    		this.iti = iti;
    	}
    	
    	public String execute() throws Exception {
    		this.setList(iti.getList());
    		return super.execute();
    	}
    }

    编写用来显示运行结果的jsp文件

    遍历list,并将每个元素作为一行来显示

    <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      
      <body>
        This is my JSP page. <br><br>
        
        <s:iterator value="list" id="current">
        	<li><s:property value="current"/></li>
        </s:iterator>
        
      </body>
    </html>

    系统的结构就是这样。下面编写两个实现IocTestInterface接口的类,用来提供数据

    public class IocTestImpl implements IocTestInterface {
    	public List getList() {
    		List l = new ArrayList();
    		l.add("abc");
    		l.add("def");
    		l.add("hig");
    		return l;
    	}
    }
    public class IocTest2Impl implements IocTestInterface {
    	public List getList() {
    		List l = new ArrayList();
    		l.add("123");
    		l.add("456");
    		l.add("789");
    		return l;
    	}
    }

    4、编写applicationContext.xml配置依赖关系

    <beans xmlns ="http://www.springframework.org/schema/beans" 
        xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation ="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
        
        <bean name="IocAction" class="sy.struts2.ioc.IocAction">
        	<property name="iti">
        		<bean class="sy.struts2.ioc.IocTestImpl"></bean>
        	</property>
        </bean>
        
    </beans>

    文件配置了id为IocAction的bean,路径为刚刚编写的Action类的路径,其中iti对象(请求数据的对象)配置为IocTestImpl(这里使用了匿名bean)

    5、编写struts.xml配置文件

    首先要告知Struts 2运行时使用Spring来创建对象

    在<struts></struts>区域加入以下配置

    <constant name="struts.objectFactory" value="spring" />

    创建package并配置Action

    <package name="hs" extends="struts-default">
         <action name="ioc" class="IocAction">
              <result>/index.jsp</result>
         </action>
    </package>

    6、发布并运行

    发布后启动Tomcat,用浏览器打开地址http://localhost:8080/StrutsIoc/ioc.action,获得了下面的页面

    image

    修改spring配置文件applicationContext.xml中配置

    <bean name="IocAction" class="sy.struts2.ioc.IocAction">
         <property name="iti">
              <bean class="sy.struts2.ioc.IocTest2Impl"></bean>
         </property>
    </bean>

    只是将注入到IocAction中的IocTestImpl修改为IocTest2Impl,也就是使用了另一个实现了IocTestInterface接口的类

    重启服务器,再次打开刚才的地址

    image

    这也就是spring的“控制反转”

  • 相关阅读:
    linux命令之------touch命令
    linux命令之------rm命令
    linux命令之------Mv命令
    linux命令之------Less命令
    linux命令之------More命令
    linux命令之------Find命令
    linux命令之------Chown命令
    linux命令之------Chmod命令
    linux命令之------Cat命令
    linux命令之------Wc命令(word count)
  • 原文地址:https://www.cnblogs.com/xingmeng/p/3056879.html
Copyright © 2011-2022 走看看